IBM / python-sdk-core

The python-sdk-core repository contains core functionality required by Python code generated by the IBM OpenAPI SDK Generator.
Apache License 2.0
20 stars 27 forks source link

feat(BaseService): Use a `requests.Session` object to configure retries and other configuration #84

Closed illeatmyhat closed 3 years ago

illeatmyhat commented 4 years ago

Closes #83

Summary: Using a requests.Session object allows the client to apply flexible global configuration, i.e. exponential backoff retries, timeouts, proxies, etc. to every request using a Transport Adapter such as HTTPAdapter.

Usage:

service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
retry_strategy = Retry(
    total=3,
    backoff_factor=1,
    status_forcelist=[429, 500, 502, 503, 504],
    method_whitelist=["HEAD", "GET", "OPTIONS", "POST"])
adapter = HTTPAdapter(max_retries=retry_strategy)
service.http_client.mount('https://', adapter)
prepped = service.prepare_request('GET', url='')
detailed_response = service.send(prepped)

An implementer service could also apply this in their __init__() function.

Unfortunately I could not introduce a new test case, because the responses library does not support testing retries. https://github.com/getsentry/responses/issues/135

CLAassistant commented 4 years ago

CLA assistant check
All committers have signed the CLA.

codecov[bot] commented 4 years ago

Codecov Report

Merging #84 (5bfaf47) into master (2dee018) will increase coverage by 0.00%. The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #84   +/-   ##
=======================================
  Coverage   98.19%   98.19%           
=======================================
  Files          18       18           
  Lines         665      666    +1     
=======================================
+ Hits          653      654    +1     
  Misses         12       12           
Impacted Files Coverage Δ
ibm_cloud_sdk_core/base_service.py 95.15% <100.00%> (+0.02%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 2dee018...5bfaf47. Read the comment docs.

padamstx commented 3 years ago

@illeatmyhat Thank you for opening this PR. We'll try to get this reviewed and get you some feedback soon.

rmkeezer commented 3 years ago

I botched this PR when trying to update it with master, I remade the PR here: https://github.com/IBM/python-sdk-core/pull/101 using your commit on my fork