|
35 | 35 | import logging |
36 | 36 | import dateutil.parser |
37 | 37 | import requests |
| 38 | +import requests.models |
38 | 39 | import requests.exceptions |
39 | 40 |
|
40 | 41 | from plus import config, account, util |
|
48 | 49 | 1 |
49 | 50 | ) # protects access to the session token which is manipulated only here |
50 | 51 |
|
| 52 | +# request timeout |
| 53 | + |
| 54 | +request_read_timeout_step:Final[int] = 2 # step size to decrease request_read_timeout on success in seconds |
| 55 | +request_read_timeout:int = config.read_timeout # dynamic read_timeout, updated on successful communication and timeouts |
| 56 | + |
| 57 | +def getReadTimeout() -> int: |
| 58 | + return request_read_timeout |
| 59 | +def updateReadTimeoutOnSuccess() -> None: |
| 60 | + global request_read_timeout # pylint:disable=global-statement |
| 61 | + request_read_timeout = max(config.read_timeout, request_read_timeout - request_read_timeout_step) |
| 62 | +def updateReadTimeoutOnTimeout() -> None: |
| 63 | + global request_read_timeout # pylint:disable=global-statement |
| 64 | + request_read_timeout = config.read_timeout_max |
| 65 | + |
| 66 | +# |
| 67 | + |
51 | 68 | def getToken() -> str|None: |
52 | 69 | try: |
53 | 70 | token_semaphore.acquire(1) |
@@ -376,95 +393,111 @@ def sendData( |
376 | 393 | verb: str, # POST or PUT |
377 | 394 | authorized: bool = True, |
378 | 395 | compress: bool = config.compress_posts, |
379 | | -) -> Any: |
| 396 | +) -> requests.models.Response: |
380 | 397 | # don't log POST data as it might contain credentials! |
381 | 398 | _log.debug('sendData(%s,_data_,%s,%s)', url, verb, authorized) |
382 | 399 | jsondata = json.dumps(data, indent=None, separators=(',', ':'), ensure_ascii=False).encode('utf8') |
383 | 400 | _log.debug('-> size %s', len(jsondata)) |
384 | 401 | # _log.debug("PRINT jsondata: %s",jsondata) |
385 | 402 | headers, postdata = getHeadersAndData(authorized, compress, jsondata, verb) |
386 | | - if verb == 'POST': |
387 | | - r = requests.post( |
388 | | - url, |
389 | | - headers=headers, |
390 | | - data=postdata, |
391 | | - verify=config.verify_ssl, |
392 | | - timeout=(config.connect_timeout, config.read_timeout), |
393 | | - ) |
394 | | - else: |
395 | | - r = requests.put( |
396 | | - url, |
397 | | - headers=headers, |
398 | | - data=postdata, |
399 | | - verify=config.verify_ssl, |
400 | | - timeout=(config.connect_timeout, config.read_timeout), |
401 | | - ) |
402 | | - _log.debug('-> status %s, time %s', r.status_code, r.elapsed.total_seconds()) |
403 | | - if authorized and r.status_code == 401: # authorisation failed |
404 | | - _log.debug('-> session token outdated (401)') |
405 | | - # we re-authentify by renewing the session token and try again |
406 | | - if authentify(): |
407 | | - time.sleep(0.3) # a little delay not to stress out the server too much |
408 | | - headers, postdata = getHeadersAndData( |
409 | | - authorized, compress, jsondata, verb |
410 | | - ) # recreate header with new token |
411 | | - if verb == 'POST': |
412 | | - r = requests.post( |
413 | | - url, |
414 | | - headers=headers, |
415 | | - data=postdata, |
416 | | - verify=config.verify_ssl, |
417 | | - timeout=(config.connect_timeout, config.read_timeout), |
418 | | - ) |
419 | | - else: |
420 | | - r = requests.put( |
421 | | - url, |
422 | | - headers=headers, |
423 | | - data=postdata, |
424 | | - verify=config.verify_ssl, |
425 | | - timeout=(config.connect_timeout, config.read_timeout), |
426 | | - ) |
427 | | - _log.debug('-> status %s, time %s', r.status_code, r.elapsed.total_seconds()) |
428 | | - return r |
| 403 | + |
| 404 | + try: |
| 405 | + if verb == 'POST': |
| 406 | + r = requests.post( |
| 407 | + url, |
| 408 | + headers=headers, |
| 409 | + data=postdata, |
| 410 | + verify=config.verify_ssl, |
| 411 | + timeout=(config.connect_timeout, getReadTimeout()), |
| 412 | + ) |
| 413 | + else: |
| 414 | + r = requests.put( |
| 415 | + url, |
| 416 | + headers=headers, |
| 417 | + data=postdata, |
| 418 | + verify=config.verify_ssl, |
| 419 | + timeout=(config.connect_timeout, getReadTimeout()), |
| 420 | + ) |
| 421 | + updateReadTimeoutOnSuccess() |
| 422 | + _log.debug('-> status %s, time %s', r.status_code, r.elapsed.total_seconds()) |
| 423 | + if authorized and r.status_code == 401: # authorisation failed |
| 424 | + _log.debug('-> session token outdated (401)') |
| 425 | + # we re-authentify by renewing the session token and try again |
| 426 | + if authentify(): |
| 427 | + time.sleep(0.3) # a little delay not to stress out the server too much |
| 428 | + headers, postdata = getHeadersAndData( |
| 429 | + authorized, compress, jsondata, verb |
| 430 | + ) # recreate header with new token |
| 431 | + if verb == 'POST': |
| 432 | + r = requests.post( |
| 433 | + url, |
| 434 | + headers=headers, |
| 435 | + data=postdata, |
| 436 | + verify=config.verify_ssl, |
| 437 | + timeout=(config.connect_timeout, getReadTimeout()), |
| 438 | + ) |
| 439 | + else: |
| 440 | + r = requests.put( |
| 441 | + url, |
| 442 | + headers=headers, |
| 443 | + data=postdata, |
| 444 | + verify=config.verify_ssl, |
| 445 | + timeout=(config.connect_timeout, getReadTimeout()), |
| 446 | + ) |
| 447 | + updateReadTimeoutOnSuccess() |
| 448 | + _log.debug('on retry: -> status %s, time %s', r.status_code, r.elapsed.total_seconds()) |
| 449 | + return r |
| 450 | + except requests.exceptions.Timeout as e: |
| 451 | + _log.error(e) |
| 452 | + updateReadTimeoutOnTimeout() |
| 453 | + raise e |
429 | 454 |
|
430 | 455 |
|
431 | | -def getData(url: str, authorized: bool = True, params:dict[str,str]|None = None) -> Any: |
| 456 | +def getData(url: str, authorized: bool = True, params:dict[str,str]|None = None) -> requests.models.Response|None: |
432 | 457 | _log.debug('getData(%s,%s,%s)', url, authorized, params) |
433 | 458 | headers = getHeaders(authorized) |
434 | 459 | params = params or {} |
435 | 460 | # _log.debug("-> request headers %s",headers) |
436 | | - r = requests.get( |
437 | | - url, |
438 | | - headers=headers, |
439 | | - verify=config.verify_ssl, |
440 | | - params=params, |
441 | | - timeout=(config.connect_timeout, config.read_timeout), |
442 | | - ) |
443 | | - _log.debug('-> status %s', r.status_code) |
444 | | - # _log.debug("-> headers %s",r.headers) |
445 | | - _log.debug('-> time %s', r.elapsed.total_seconds()) |
446 | | - if authorized and r.status_code == 401: # authorisation failed |
447 | | - _log.debug( |
448 | | - '-> session token outdated (404) - re-authentify' |
449 | | - ) |
450 | | - # we re-authentify by renewing the session token and try again |
451 | | - authentify() |
452 | | - headers = getHeaders(authorized) # recreate header with new token |
453 | | - r = requests.get( |
| 461 | + try: |
| 462 | + r:requests.models.Response = requests.get( |
454 | 463 | url, |
455 | 464 | headers=headers, |
456 | 465 | verify=config.verify_ssl, |
457 | 466 | params=params, |
458 | | - timeout=(config.connect_timeout, config.read_timeout), |
| 467 | + timeout=(config.connect_timeout, getReadTimeout()), |
459 | 468 | ) |
| 469 | + updateReadTimeoutOnSuccess() |
460 | 470 | _log.debug('-> status %s', r.status_code) |
461 | | - # _log.debug("-> headers %s",r.headers) |
462 | | - _log.debug( |
463 | | - '-> time %s', r.elapsed.total_seconds() |
464 | | - ) |
465 | | - try: |
466 | | - _log.debug('-> size %s', len(r.content)) |
467 | | -# _log.debug("-> data %s",r.json()) |
468 | | - except Exception: # pylint: disable=broad-except |
469 | | - pass |
470 | | - return r |
| 471 | + # _log.debug("-> headers %s",r.headers) |
| 472 | + _log.debug('-> time %s', r.elapsed.total_seconds()) |
| 473 | + if authorized and r.status_code == 401: # authorisation failed |
| 474 | + _log.debug( |
| 475 | + '-> session token outdated (404) - re-authentify' |
| 476 | + ) |
| 477 | + # we re-authentify by renewing the session token and try again |
| 478 | + if authentify(): |
| 479 | + time.sleep(0.3) # a little delay not to stress out the server too much |
| 480 | + headers = getHeaders(authorized) # recreate header with new token |
| 481 | + r = requests.get( |
| 482 | + url, |
| 483 | + headers=headers, |
| 484 | + verify=config.verify_ssl, |
| 485 | + params=params, |
| 486 | + timeout=(config.connect_timeout, getReadTimeout()), |
| 487 | + ) |
| 488 | + updateReadTimeoutOnSuccess() |
| 489 | + _log.debug('-> status %s', r.status_code) |
| 490 | + # _log.debug("-> headers %s",r.headers) |
| 491 | + _log.debug( |
| 492 | + 'on retry: -> time %s', r.elapsed.total_seconds() |
| 493 | + ) |
| 494 | + try: |
| 495 | + _log.debug('-> size %s', len(r.content)) |
| 496 | + # _log.debug("-> data %s",r.json()) |
| 497 | + except Exception: # pylint: disable=broad-except |
| 498 | + pass |
| 499 | + return r |
| 500 | + except requests.exceptions.Timeout as e: |
| 501 | + _log.error(e) |
| 502 | + updateReadTimeoutOnTimeout() |
| 503 | + return None |
0 commit comments