From 19a479dad0e3d73ff114ff4a6e67c13c62cb86a8 Mon Sep 17 00:00:00 2001 From: Robert Nilsson Date: Wed, 2 May 2012 11:37:31 +0200 Subject: [PATCH 1/4] Added "Accept" header to outgoing request Some services supports several return formats and use the "Accept" HTTP header to differentiate. Added header to make sure the expected format is returned with these. --- wellrested/connections/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/wellrested/connections/__init__.py b/wellrested/connections/__init__.py index c22a9b5..34c613e 100644 --- a/wellrested/connections/__init__.py +++ b/wellrested/connections/__init__.py @@ -13,6 +13,7 @@ class RestClient(object): content_type = None + accept = None def __init__(self, base_url, username=None, password=None, connection_class=None, **kwargs): @@ -43,7 +44,8 @@ def _request(self, resource, method, args=None, data=None, headers=None): response_headers, response_content = \ self._connection.request(resource, method, args=args, body=request_body, headers=headers, - content_type=self.content_type) + content_type=self.content_type, + accept=self.accept) if response_headers.get('status') == HTTP_STATUS_OK: response_data = self._deserialize(response_content) return Response(response_headers, response_content, response_data) @@ -57,6 +59,7 @@ def _deserialize(self, data): class JsonRestClient(RestClient): content_type = 'application/json' + accept = 'application/json' def _serialize(self, data): if data: @@ -87,7 +90,7 @@ def _deserialize(self, data): class XmlRestClient(RestClient): content_type = 'text/xml' - + accept = 'text/xml' class Response(object): def __init__(self, headers, content, data): @@ -115,7 +118,7 @@ def _get_content_type(self, filename): return mimetypes.guess_type(filename)[0] or 'application/octet-stream' def request(self, resource, method="get", args=None, body=None, - headers=None, content_type=None): + headers=None, content_type=None, accept=None): raise NotImplementedError @@ -135,7 +138,7 @@ def __init__(self, *args, **kwargs): self._conn.add_credentials(self.username, self.password) def request(self, resource, method, args=None, body=None, headers=None, - content_type=None): + content_type=None, accept=None): if headers is None: headers = {} @@ -143,6 +146,9 @@ def request(self, resource, method, args=None, body=None, headers=None, path = resource headers['User-Agent'] = 'Basic Agent' + if accept is not None: + headers["Accept"] = accept + BOUNDARY = mimetools.choose_boundary() CRLF = u'\r\n' From fd618bddd98628f7b654679e17c57c590252ff5e Mon Sep 17 00:00:00 2001 From: Robert Nilsson Date: Wed, 2 May 2012 11:39:33 +0200 Subject: [PATCH 2/4] Bumped version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 9e6fb30..52fd452 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -version = '0.1.1' +version = '0.1.4' setup( name='python-wellrested', From 9013ffe2d9f55cfd98699f32c563efbc6a978585 Mon Sep 17 00:00:00 2001 From: Robert Nilsson Date: Wed, 2 May 2012 11:43:37 +0200 Subject: [PATCH 3/4] Indentation issue fixed. --- wellrested/connections/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/wellrested/connections/__init__.py b/wellrested/connections/__init__.py index 34c613e..69623a9 100644 --- a/wellrested/connections/__init__.py +++ b/wellrested/connections/__init__.py @@ -13,7 +13,7 @@ class RestClient(object): content_type = None - accept = None + accept = None def __init__(self, base_url, username=None, password=None, connection_class=None, **kwargs): @@ -45,7 +45,7 @@ def _request(self, resource, method, args=None, data=None, headers=None): self._connection.request(resource, method, args=args, body=request_body, headers=headers, content_type=self.content_type, - accept=self.accept) + accept=self.accept) if response_headers.get('status') == HTTP_STATUS_OK: response_data = self._deserialize(response_content) return Response(response_headers, response_content, response_data) @@ -59,7 +59,7 @@ def _deserialize(self, data): class JsonRestClient(RestClient): content_type = 'application/json' - accept = 'application/json' + accept = 'application/json' def _serialize(self, data): if data: @@ -90,7 +90,7 @@ def _deserialize(self, data): class XmlRestClient(RestClient): content_type = 'text/xml' - accept = 'text/xml' + accept = 'text/xml' class Response(object): def __init__(self, headers, content, data): @@ -146,8 +146,8 @@ def request(self, resource, method, args=None, body=None, headers=None, path = resource headers['User-Agent'] = 'Basic Agent' - if accept is not None: - headers["Accept"] = accept + if accept is not None: + headers["Accept"] = accept BOUNDARY = mimetools.choose_boundary() CRLF = u'\r\n' From efd501853772be6666b4fe9ed65ba957c49ea2a6 Mon Sep 17 00:00:00 2001 From: Robert Nilsson Date: Wed, 2 May 2012 12:10:23 +0200 Subject: [PATCH 4/4] Fixed path problem Urls like http://xyz.com/ caused the resource part in the request to be ignored. --- wellrested/connections/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wellrested/connections/__init__.py b/wellrested/connections/__init__.py index 69623a9..b75b656 100644 --- a/wellrested/connections/__init__.py +++ b/wellrested/connections/__init__.py @@ -114,6 +114,9 @@ def __init__(self, base_url, username=None, password=None): self.host = netloc self.path = path + if self.path == "/": + self.path = "" + def _get_content_type(self, filename): return mimetypes.guess_type(filename)[0] or 'application/octet-stream'