Fom

Merge lp:~ntoll/fom/requests-branch-978804 into lp:fom

Proposed by Nicholas Tollervey
Status: Merged
Merge reported by: Nicholas Tollervey
Merged at revision: not available
Proposed branch: lp:~ntoll/fom/requests-branch-978804
Merge into: lp:fom
Diff against target: 215 lines (+33/-35)
8 files modified
build.bu (+1/-1)
fom/db.py (+13/-17)
fom/tx.py (+3/-2)
fom/version.py (+1/-1)
setup.py (+1/-1)
tests/_base.py (+12/-11)
tests/test_base.py (+1/-1)
tests/test_tx.py (+1/-1)
To merge this branch: bzr merge lp:~ntoll/fom/requests-branch-978804
Reviewer Review Type Date Requested Status
PA Parent Approve
Review via email: mp+101551@code.launchpad.net

Description of the change

This branch introduces the following changes:

* All reference httplib2 is removed.
* The requests library is used instead to make calls to Fluidinfo.
* Changes in the test suite to take into account the above.

Also, given that requests simplifies the HTTP connectivity with Fluidinfo there is opportunity to simplify the whole db.py module in a later ticket. (The focus of this branch is to simply move from httplib2->requests).

To post a comment you must log in.
Revision history for this message
Nicholas Tollervey (ntoll) wrote :

By the way, once reviewed and merged I'll push to pypi.python.org.

N.

Revision history for this message
PA Parent (paparent) wrote :

+1 LGTM, all tests pass!

review: Approve
lp:~ntoll/fom/requests-branch-978804 updated
160. By Nicholas Tollervey

Fixed mention of httplib2 in build.bu

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'build.bu'
2--- build.bu 2010-09-18 01:23:10 +0000
3+++ build.bu 2012-04-11 13:45:33 +0000
4@@ -10,7 +10,7 @@
5 ve.install deps=ve.new:
6 {{ pip }} sphinx
7 {{ pip }} sphinx-pypi-upload
8- {{ pip }} httplib2
9+ {{ pip }} requests
10 {{ pip }} py
11 {{ pip }} pytest-xdist
12 {{ pip }} --no-deps -U .
13
14=== modified file 'fom/db.py'
15--- fom/db.py 2011-12-03 13:11:07 +0000
16+++ fom/db.py 2012-04-11 13:45:33 +0000
17@@ -32,7 +32,7 @@
18 import types
19 import urllib
20
21-import httplib2
22+import requests
23
24 try:
25 import json
26@@ -107,7 +107,7 @@
27 These are generally created by the API, and returned, and there is little
28 use to create them manually.
29
30- :param response: An httplib2 response instance, which is a dict with an
31+ :param response: A response instance, which is a dict with an
32 additional status attribute.
33 :param content: The body of the HTTP response.
34 :param is_value: A boolean flag to indicate whether the response is from a
35@@ -139,7 +139,7 @@
36 """
37
38 def __init__(self, response, content, is_value):
39- self.content_type = response['content-type']
40+ self.content_type = response.headers['content-type']
41 if ((is_value and self.content_type == PRIMITIVE_CONTENT_TYPE) or
42 (self.content_type in DESERIALIZABLE_CONTENT_TYPES)):
43 try:
44@@ -148,11 +148,11 @@
45 self.value = content
46 else:
47 self.value = content
48- self.status = response.status
49+ self.status = response.status_code
50 self.response = response
51 self.content = content
52- self.request_id = self.response.get('x-fluiddb-request-id')
53- self.error = self.response.get('x-fluiddb-error-class')
54+ self.request_id = self.response.headers.get('x-fluiddb-request-id')
55+ self.error = self.response.headers.get('x-fluiddb-error-class')
56 if self.status >= 400:
57 raise_error(self)
58
59@@ -184,11 +184,11 @@
60 raise ValueError('The domain for FluidDB must *not* end with'\
61 ' "/". Correct example:'\
62 ' https://fluiddb.fluidinfo.com')
63- self._http = httplib2.Http()
64 self.base_url = base_url
65 self.headers = {
66 'User-agent': 'fom/%s' % version,
67 }
68+ self.session = requests.session(headers=self.headers)
69 # XXX Backwards compat
70 self.client = self
71
72@@ -212,20 +212,16 @@
73 `application/vnd.fluiddb.value+json` even if they are of a
74 deserializable content type such as `application/json`
75 """
76- req, params = self._build_request(method, path, payload, urlargs,
77- content_type)
78- fom_request_sent.send(self, request=params)
79- response, content = req(*params)
80- fom_response_received.send(self, response=(response.status,
81- content, response.copy()))
82- return FluidResponse(response, content, is_value)
83-
84- def _build_request(self, method, path, payload, urlargs, content_type):
85 payload, content_type = _get_body_and_type(payload, content_type)
86 urlargs = urlargs or {}
87 headers = self._get_headers(content_type)
88 url = self._get_url(path, urlargs)
89- return self._http.request, (url, method, payload, headers)
90+ fom_request_sent.send(self, request=(url, method, payload, headers))
91+ response = self.session.request(method, url, data=payload,
92+ headers=headers)
93+ fom_response_received.send(self, response=(response.status_code,
94+ response.text, None))
95+ return FluidResponse(response, response.text, is_value)
96
97 def _get_headers(self, content_type):
98 headers = self.headers.copy()
99
100=== modified file 'fom/tx.py'
101--- fom/tx.py 2010-11-29 12:52:12 +0000
102+++ fom/tx.py 2012-04-11 13:45:33 +0000
103@@ -73,9 +73,10 @@
104 """
105
106 def __init__(self, response):
107- self.status = response.code
108+ self.status_code = response.code
109+ self.headers = {}
110 for k, v in response.headers.getAllRawHeaders():
111- self[k.lower()] = v[0]
112+ self.headers[k.lower()] = v[0]
113
114
115 class TxFluidDB(FluidDB):
116
117=== modified file 'fom/version.py'
118--- fom/version.py 2011-12-12 11:38:16 +0000
119+++ fom/version.py 2012-04-11 13:45:33 +0000
120@@ -9,5 +9,5 @@
121 :license: MIT, see LICENSE for more information.
122 """
123
124-version = '0.9.7'
125+version = '0.9.8'
126
127
128=== modified file 'setup.py'
129--- setup.py 2012-03-05 16:51:35 +0000
130+++ setup.py 2012-04-11 13:45:33 +0000
131@@ -13,6 +13,6 @@
132 url='http://packages.python.org/Fom/',
133 packages=['fom'],
134 scripts=['bin/fdbc'],
135- install_requires=['httplib2', 'blinker'],
136+ install_requires=['requests', 'blinker'],
137 )
138
139
140=== modified file 'tests/_base.py'
141--- tests/_base.py 2010-11-30 14:59:41 +0000
142+++ tests/_base.py 2012-04-11 13:45:33 +0000
143@@ -12,15 +12,17 @@
144 from collections import deque
145
146
147-from fom.db import FluidDB, _generate_endpoint_url
148+from fom.db import FluidDB, _generate_endpoint_url, NO_CONTENT, FluidResponse
149
150
151 class FakeHttpLibResponse(dict):
152
153- def __init__(self, status, content_type):
154+ def __init__(self, status, content_type, content=None):
155 # yeah, I know, blame httplib2 for this API
156- self.status = status
157- self['content-type'] = content_type
158+ self.status_code = status
159+ self.headers = {}
160+ self.headers['content-type'] = content_type
161+ self.text = content
162
163
164 class FakeHttpLibRequest(object):
165@@ -39,14 +41,14 @@
166 FluidDB.__init__(self, 'http://testing')
167 self.reqs = []
168 self.resps = deque()
169- self.default_response = (
170- FakeHttpLibResponse(200, 'text/plain'), 'empty')
171+ self.default_response = FakeHttpLibResponse(200, 'text/plain', 'empty')
172
173 def add_resp(self, status, content_type, content):
174- hresp = FakeHttpLibResponse(status, content_type)
175- self.resps.append((hresp, content))
176+ hresp = FakeHttpLibResponse(status, content_type, content)
177+ self.resps.append(hresp)
178
179- def _build_request(self, method, path, payload, urlargs, content_type):
180+ def __call__(self, method, path, payload=NO_CONTENT, urlargs=None,
181+ content_type=None, is_value=False):
182 path = _generate_endpoint_url('', path, '')
183 req = (method, path, payload, urlargs, content_type)
184 self.reqs.append(req)
185@@ -54,5 +56,4 @@
186 resp = self.resps.popleft()
187 except IndexError:
188 resp = self.default_response
189- return FakeHttpLibRequest(resp), (method, path, payload, urlargs,
190- content_type)
191+ return FluidResponse(resp, resp.text, is_value)
192
193=== modified file 'tests/test_base.py'
194--- tests/test_base.py 2010-12-24 15:30:04 +0000
195+++ tests/test_base.py 2012-04-11 13:45:33 +0000
196@@ -22,5 +22,5 @@
197 payload = 'fluiddb'
198 urlargs = {'ham': 'eggs'}
199 content_type = 'text/plain'
200- fake._build_request(method, path, payload, urlargs, content_type)
201+ fake(method, path, payload, urlargs, content_type)
202 self.assertEqual('/foo%2Fbar/baz/qux', fake.reqs[0][1])
203
204=== modified file 'tests/test_tx.py'
205--- tests/test_tx.py 2011-01-19 10:26:21 +0000
206+++ tests/test_tx.py 2012-04-11 13:45:33 +0000
207@@ -8,7 +8,7 @@
208 class TestTxFluidDB(unittest.TestCase):
209
210 def setUp(self):
211- self.db = TxFluidDB('https://sandbox.fluidinfo.com')
212+ self.db = TxFluidDB('http://sandbox.fluidinfo.com')
213
214 @defer.inlineCallbacks
215 def testRequest(self):

Subscribers

People subscribed via source and target branches

to all changes: