hootnot / oanda-api-v20

OANDA REST-V20 API wrapper. Easy access to OANDA's REST v20 API with oandapyV20 package. Checkout the Jupyter notebooks!
MIT License
398 stars 107 forks source link

Added __enter__ and __exit__ methods to oandapyV20 #170

Closed ricfaith closed 3 years ago

ricfaith commented 3 years ago

Support with statement from PEP343 Close requests.Session on exit

The unclosed requests.Session was throwing an unclosed socket in my unit tests. Similar findings were found in https://stackoverflow.com/questions/48160728/resourcewarning-unclosed-socket-in-python-3-unit-test The stackoverflow link shows some other suitable workarounds.

with API(access_token=access_token, environment=env) as client:
    r = trades.TradesList(accountID)
    rv = client.request(r)
    print("RESPONSE:\n{}".format(json.dumps(rv, indent=2)))
coveralls commented 3 years ago

Coverage Status

Coverage decreased (-0.3%) to 98.976% when pulling 83a1abcf67a1d4fc0615e80c0f893142420785d5 on ricfaith:close-requests-session-with-exit into 3ec71d331b509750c4985db0a9b73bec6bf5f9f7 on hootnot:master.

hootnot commented 3 years ago

@ricfaith I'll have a look at it this weekend. Sounds like a good change

hootnot commented 3 years ago

@ricfaith merged your code with a small change: I added a close method allowing to explicitly close the session also. exiting code can be extended that way by simply adding the line:


client = API(...)
client.request(...)

client.close()

thanks!