Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added __enter__ and __exit__ methods to oandapyV20 #170

Merged
merged 4 commits into from
Sep 27, 2020

Conversation

ricfaith
Copy link
Contributor

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)))

Closed requests.Session on __exit__
@coveralls
Copy link

coveralls commented Sep 23, 2020

Coverage Status

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

@hootnot
Copy link
Owner

hootnot commented Sep 23, 2020

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

close method to explicitly close the requests session
remove white space in docstring
removed another series of white space
@hootnot hootnot merged commit 616e3c8 into hootnot:master Sep 27, 2020
@hootnot
Copy link
Owner

hootnot commented Sep 27, 2020

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants