Skip to content
Snippets Groups Projects
Commit 82cb7261 authored by gabriele-h's avatar gabriele-h
Browse files

Extract methods for getting and generating xml

parent fe9d4ff7
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,7 @@ information:
Possible formats are documented here:
https://developers.exlibrisgroup.com/alma/integrations/SRU/
The Library of Congrass has general information on SRU:
The Library of Congress has general information on SRU:
http://www.loc.gov/standards/sru/
"""
......@@ -108,14 +108,7 @@ class RecordRetriever:
first_page = self._get_first_page(alma_query)
num_recs = self._get_num_records(first_page)
xml_list = []
for startrecord in range(1, int(num_recs), 10):
sru_url = self._create_url(alma_query, startrecord)
sru_xml = self._response_to_xml(sru_url)
self._raise_error_sru(sru_xml)
xml_list.extend(self._extract_marc_xml(sru_xml))
xml_list = self._get_xml_list(alma_query, num_recs)
return xml_list
def _get_first_page(self, alma_query):
......@@ -131,45 +124,52 @@ class RecordRetriever:
num_recs = int(num_recs_element.text)
return num_recs
def _create_url(self, alma_query: str, startrecord: int) -> str:
def _get_xml_list(self, alma_query: str, num_recs: int):
xml_list = []
for xml in self._gen_xml(alma_query, num_recs):
xml_list.extend(self._extract_marc_xml(xml))
return xml_list
def _gen_xml(self, alma_query: str, num_recs: int):
print(num_recs)
for startrecord in range(1, num_recs+1, 10):
print(startrecord)
sru_url = self._create_url(alma_query, startrecord)
sru_xml = self._response_to_xml(sru_url)
self._raise_error_sru(sru_xml)
yield sru_xml
def _create_url(self, alma_query: str, startrecord: int) -> str:
self.url_query['startRecord'] = str(startrecord)
self.url_query['maximumRecords'] = '10'
self.url_query['query'] = alma_query
url_query_string = parse.urlencode(self.url_query)
sru_url = self.url_base + url_query_string
return sru_url
@staticmethod
def _response_to_xml(url: str) -> Element:
response = request.urlopen(url).read()
xml = fromstring(response)
return xml
@staticmethod
def _raise_error_unique(num_recs: int):
if num_recs > 1:
raise NotUnique('More than one record found while exactly one was expected.')
@staticmethod
def _raise_error_no_match(num_recs_element: Element):
if (num_recs_element is not None and
num_recs_element.text == '0'):
raise NoRecord('No matching records found.')
@staticmethod
def _raise_error_too_many_recs(num_recs_element: Element):
if int(num_recs_element.text) > 10000:
raise TooManyRecords('SRU searches are limited to 10.000 records.')
def _raise_error_sru(self, sru_xml: Element):
if sru_xml.find("srw:diagnostics", self.namespaces):
xpath = "srw:diagnostics/diag:diagnostic/diag:message"
sru_message = sru_xml.find(xpath, self.namespaces).text
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment