Skip to content

Commit

Permalink
Implemented Context Manager for Requests
Browse files Browse the repository at this point in the history
  • Loading branch information
hcwong committed Apr 8, 2018
1 parent dbf7014 commit a3af49d
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions agithub/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,15 @@ def request(self, method, url, bodyData, headers):
if 'content-type' in headers:
del headers['content-type']

#TODO: Context manager
requestBody = RequestBody(bodyData, headers)
conn = self.get_connection()
conn.request(method, url, requestBody.process(), headers)
response = conn.getresponse()
status = response.status
content = ResponseBody(response)
self.headers = response.getheaders()

conn.close()
# Context manager to handle requests
with connectionManager(self) as conn:
requestBody = RequestBody(bodyData, headers)
conn.request(method, url, requestBody.process(), headers)
response = conn.getresponse()
status = response.status
content = ResponseBody(response)
self.headers = response.getheaders()

return status, content.processBody()

def _fix_headers(self, headers):
Expand Down Expand Up @@ -411,3 +410,19 @@ def _filterEmptyHeaders(self, headers):
newHeaders[header] = headers[header]

return newHeaders

class connectionManager(object):
'''
Context manager for handling connections in client.request
'''
def __init__(self, client):
self.conn = client.get_connection()

def __enter__(self):
return self.conn

def __exit__(self, exc_type, exc_value, traceback):
self.conn.close()



0 comments on commit a3af49d

Please sign in to comment.