Skip to content

Commit f45974a

Browse files
committed
Merge pull request #53 from Javinator9889/patch-2
HTTP Request timeout
2 parents 33c0203 + 94511e4 commit f45974a

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

acoustid.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,11 @@ def __call__(self, *args, **kwargs):
168168

169169

170170
@_rate_limit
171-
def _api_request(url, params):
171+
def _api_request(url, params, timeout=None):
172172
"""Makes a POST request for the URL with the given form parameters,
173173
which are encoded as compressed form data, and returns a parsed JSON
174174
response. May raise a WebServiceError if the request fails.
175+
If the specified timeout passes, then raises a TimeoutError.
175176
"""
176177
headers = {
177178
'Accept-Encoding': 'gzip',
@@ -181,9 +182,11 @@ def _api_request(url, params):
181182
with requests.Session() as session:
182183
session.mount('http://', CompressedHTTPAdapter())
183184
try:
184-
response = session.post(url, data=params, headers=headers)
185+
response = session.post(url, data=params, headers=headers, timeout=timeout)
185186
except requests.exceptions.RequestException as exc:
186187
raise WebServiceError("HTTP request failed: {0}".format(exc))
188+
except requests.exceptions.ReadTimeout:
189+
raise WebServiceError("HTTP timed out ({0}s)".format(timeout))
187190

188191
try:
189192
return response.json()
@@ -218,7 +221,7 @@ def fingerprint(samplerate, channels, pcmiter, maxlength=MAX_AUDIO_LENGTH):
218221
raise FingerprintGenerationError("fingerprint calculation failed")
219222

220223

221-
def lookup(apikey, fingerprint, duration, meta=DEFAULT_META):
224+
def lookup(apikey, fingerprint, duration, meta=DEFAULT_META, timeout=None):
222225
"""Look up a fingerprint with the Acoustid Web service. Returns the
223226
Python object reflecting the response JSON data.
224227
"""
@@ -229,7 +232,7 @@ def lookup(apikey, fingerprint, duration, meta=DEFAULT_META):
229232
'fingerprint': fingerprint,
230233
'meta': meta,
231234
}
232-
return _api_request(_get_lookup_url(), params)
235+
return _api_request(_get_lookup_url(), params, timeout)
233236

234237

235238
def parse_lookup_result(data):
@@ -329,7 +332,7 @@ def fingerprint_file(path, maxlength=MAX_AUDIO_LENGTH, force_fpcalc=False):
329332
return _fingerprint_file_fpcalc(path, maxlength)
330333

331334

332-
def match(apikey, path, meta=DEFAULT_META, parse=True, force_fpcalc=False):
335+
def match(apikey, path, meta=DEFAULT_META, parse=True, force_fpcalc=False, timeout=None):
333336
"""Look up the metadata for an audio file. If ``parse`` is true,
334337
then ``parse_lookup_result`` is used to return an iterator over
335338
small tuple of relevant information; otherwise, the full parsed JSON
@@ -338,14 +341,14 @@ def match(apikey, path, meta=DEFAULT_META, parse=True, force_fpcalc=False):
338341
true, only the latter will be used.
339342
"""
340343
duration, fp = fingerprint_file(path, force_fpcalc=force_fpcalc)
341-
response = lookup(apikey, fp, duration, meta)
344+
response = lookup(apikey, fp, duration, meta, timeout)
342345
if parse:
343346
return parse_lookup_result(response)
344347
else:
345348
return response
346349

347350

348-
def submit(apikey, userkey, data):
351+
def submit(apikey, userkey, data, timeout=None):
349352
"""Submit a fingerprint to the acoustid server. The ``apikey`` and
350353
``userkey`` parameters are API keys for the application and the
351354
submitting user, respectively.
@@ -383,7 +386,7 @@ def submit(apikey, userkey, data):
383386
for k, v in d.items():
384387
args["%s.%s" % (k, i)] = v
385388

386-
response = _api_request(_get_submit_url(), args)
389+
response = _api_request(_get_submit_url(), args, timeout)
387390
if response.get('status') != 'ok':
388391
try:
389392
code = response['error']['code']
@@ -394,7 +397,7 @@ def submit(apikey, userkey, data):
394397
return response
395398

396399

397-
def get_submission_status(apikey, submission_id):
400+
def get_submission_status(apikey, submission_id, timeout=None):
398401
"""Get the status of a submission to the acoustid server.
399402
``submission_id`` is the id of a fingerprint submission, as returned
400403
in the response object of a call to the ``submit`` endpoint.
@@ -404,4 +407,4 @@ def get_submission_status(apikey, submission_id):
404407
'client': apikey,
405408
'id': submission_id,
406409
}
407-
return _api_request(_get_submission_status_url(), params)
410+
return _api_request(_get_submission_status_url(), params, timeout)

0 commit comments

Comments
 (0)