Skip to content

Commit

Permalink
Retry SUSHI reports if "Report Queued" message is returned.
Browse files Browse the repository at this point in the history
(This is kind of an ugly hack that looks for this string in the
raw XML. A nicer fix will be possible with a fix for #3 )
  • Loading branch information
Wooble committed Aug 1, 2018
1 parent 2ac6d55 commit 56f48e1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 9 deletions.
28 changes: 21 additions & 7 deletions pycounter/sushi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import collections
import datetime
import logging
import time
import uuid


Expand All @@ -25,7 +26,8 @@
def get_sushi_stats_raw(wsdl_url, start_date, end_date, requestor_id=None,
requestor_email=None, requestor_name=None,
customer_reference=None, customer_name=None,
report="JR1", release=4, sushi_dump=False):
report="JR1", release=4, sushi_dump=False,
):
"""Get SUSHI stats for a given site in raw XML format.
:param wsdl_url: URL to SOAP WSDL for this provider
Expand Down Expand Up @@ -110,9 +112,18 @@ def get_report(*args, **kwargs):
returns a :class:`pycounter.report.CounterReport` object.
parameters: see get_sushi_stats_raw
:param no_delay: don't delay in retrying Report Queued
"""
raw_report = get_sushi_stats_raw(*args, **kwargs)
return _raw_to_full(raw_report)
no_delay = kwargs.pop('no_delay', False)
delay_amount = 0 if no_delay else 60
while True:
try:
raw_report = get_sushi_stats_raw(*args, **kwargs)
return _raw_to_full(raw_report)
except pycounter.exceptions.ServiceBusyError:
print("Service busy, retrying in %d seconds" % delay_amount)
time.sleep(delay_amount)


def _ns(namespace, name):
Expand Down Expand Up @@ -146,10 +157,13 @@ def _raw_to_full(raw_report):
try:
c_report = rep.Report[_ns('counter', 'Reports')].Report
except AttributeError:
logger.error("report not found in XML: %s", raw_report)
raise pycounter.exceptions.SushiException(
message="report not found in XML",
raw=raw_report, xml=o_root)
if b'Report Queued' in raw_report:
raise pycounter.exceptions.ServiceBusyError('Report Queued')
else:
logger.error("report not found in XML: %s", raw_report)
raise pycounter.exceptions.SushiException(
message="report not found in XML",
raw=raw_report, xml=o_root)
logger.debug("COUNTER report: %s", etree.tostring(c_report))
start_date = datetime.datetime.strptime(
root.find('.//%s' % _ns('sushi', 'Begin')).text,
Expand Down
6 changes: 4 additions & 2 deletions pycounter/sushiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
help='Output file to write (will be overwritten)',
type=click.Path(writable=True))
@click.option('--dump', '-d', is_flag=True)
@click.option('--no-delay', is_flag=True, )
def main(url, report, release, start_date, end_date, requestor_id,
requestor_email, requestor_name, customer_name,
customer_reference, format_, output_file, dump):
customer_reference, format_, output_file, dump, no_delay):
"""Main function for the SUSHI client."""
click.echo("pycounter SUSHI client for URL %s (%s R%s)"
% (url, report, release))
Expand All @@ -70,7 +71,8 @@ def main(url, report, release, start_date, end_date, requestor_id,
customer_name=customer_name,
start_date=converted_start_date,
end_date=converted_end_date,
sushi_dump=dump)
sushi_dump=dump,
no_delay=no_delay)
output_file = output_file % format_
report.write_to_file(output_file, format_)

Expand Down
35 changes: 35 additions & 0 deletions pycounter/test/data/sushi_queued.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" ?>
<!--suppress CheckTagEmptyBody -->
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns3:ReportResponse xmlns="http://www.niso.org/schemas/counter"
xmlns:ns2="http://www.niso.org/schemas/sushi"
xmlns:ns3="http://www.niso.org/schemas/sushi/counter"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns2:Exception>
<ns2:Number>4001</ns2:Number>
<ns2:Severity>Info</ns2:Severity>
<ns2:Message>Report Queued</ns2:Message>
</ns2:Exception>
<!--suppress CheckTagEmptyBody -->
<ns2:Requestor>
<ns2:ID>exampleRequestor</ns2:ID>
<ns2:Name></ns2:Name>
<ns2:Email></ns2:Email>
</ns2:Requestor>
<ns2:CustomerReference>
<ns2:ID>exampleReference</ns2:ID>
<ns2:Name></ns2:Name>
</ns2:CustomerReference>
<ns2:ReportDefinition Release="4" Name="JR1">
<ns2:Filters>
<ns2:UsageDateRange>
<ns2:Begin>2013-01-01</ns2:Begin>
<ns2:End>2013-01-31</ns2:End>
</ns2:UsageDateRange>
</ns2:Filters>
</ns2:ReportDefinition>
<ns3:Report xsi:nil="true"/>
</ns3:ReportResponse>
</S:Body>
</S:Envelope>
29 changes: 29 additions & 0 deletions pycounter/test/test_sushi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
import pycounter.exceptions


@urlmatch(netloc=r'(.*\.)?example\.com$')
def report_queued_mock(url_unused, request_unused):
if report_queued_mock.first_request:
path = os.path.join(os.path.dirname(__file__), 'data', 'sushi_queued.xml')
report_queued_mock.first_request = False
else:
path = os.path.join(os.path.dirname(__file__), 'data', 'sushi_simple.xml')
with open(path, 'rb') as datafile:
return datafile.read().decode('utf-8')


report_queued_mock.first_request = True


@urlmatch(netloc=r'(.*\.)?example\.com$')
def sushi_mock(url_unused, request_unused):
path = os.path.join(os.path.dirname(__file__),
Expand Down Expand Up @@ -312,6 +326,20 @@ def test_explicit_dates(self):
result = runner.invoke(sushiclient.main, arglist)
self.assertEqual(result.exit_code, 0)

def test_queued_report(self):
"""Test that queued report is retried"""
arglist = [
'http://www.example.com/Sushi',
'-s', '2013-01-01',
'-e', '2013-01-31',
'--no-delay',
]
with HTTMock(report_queued_mock):
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(sushiclient.main, arglist)
self.assertEqual(result.exit_code, 0)


class TestMissingItemIdentifier(unittest.TestCase):
"""Test converting simple SUSHI response"""
Expand All @@ -325,3 +353,4 @@ def setUp(self):
def test_issn(self):
publication = next(iter(self.report))
self.assertEqual(publication.issn, "")

0 comments on commit 56f48e1

Please sign in to comment.