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
=== modified file 'build.bu'
--- build.bu 2010-09-18 01:23:10 +0000
+++ build.bu 2012-04-11 13:45:33 +0000
@@ -10,7 +10,7 @@
10ve.install deps=ve.new:10ve.install deps=ve.new:
11 {{ pip }} sphinx11 {{ pip }} sphinx
12 {{ pip }} sphinx-pypi-upload12 {{ pip }} sphinx-pypi-upload
13 {{ pip }} httplib213 {{ pip }} requests
14 {{ pip }} py14 {{ pip }} py
15 {{ pip }} pytest-xdist15 {{ pip }} pytest-xdist
16 {{ pip }} --no-deps -U .16 {{ pip }} --no-deps -U .
1717
=== modified file 'fom/db.py'
--- fom/db.py 2011-12-03 13:11:07 +0000
+++ fom/db.py 2012-04-11 13:45:33 +0000
@@ -32,7 +32,7 @@
32import types32import types
33import urllib33import urllib
3434
35import httplib235import requests
3636
37try:37try:
38 import json38 import json
@@ -107,7 +107,7 @@
107 These are generally created by the API, and returned, and there is little107 These are generally created by the API, and returned, and there is little
108 use to create them manually.108 use to create them manually.
109109
110 :param response: An httplib2 response instance, which is a dict with an110 :param response: A response instance, which is a dict with an
111 additional status attribute.111 additional status attribute.
112 :param content: The body of the HTTP response.112 :param content: The body of the HTTP response.
113 :param is_value: A boolean flag to indicate whether the response is from a113 :param is_value: A boolean flag to indicate whether the response is from a
@@ -139,7 +139,7 @@
139 """139 """
140140
141 def __init__(self, response, content, is_value):141 def __init__(self, response, content, is_value):
142 self.content_type = response['content-type']142 self.content_type = response.headers['content-type']
143 if ((is_value and self.content_type == PRIMITIVE_CONTENT_TYPE) or143 if ((is_value and self.content_type == PRIMITIVE_CONTENT_TYPE) or
144 (self.content_type in DESERIALIZABLE_CONTENT_TYPES)):144 (self.content_type in DESERIALIZABLE_CONTENT_TYPES)):
145 try:145 try:
@@ -148,11 +148,11 @@
148 self.value = content148 self.value = content
149 else:149 else:
150 self.value = content150 self.value = content
151 self.status = response.status151 self.status = response.status_code
152 self.response = response152 self.response = response
153 self.content = content153 self.content = content
154 self.request_id = self.response.get('x-fluiddb-request-id')154 self.request_id = self.response.headers.get('x-fluiddb-request-id')
155 self.error = self.response.get('x-fluiddb-error-class')155 self.error = self.response.headers.get('x-fluiddb-error-class')
156 if self.status >= 400:156 if self.status >= 400:
157 raise_error(self)157 raise_error(self)
158158
@@ -184,11 +184,11 @@
184 raise ValueError('The domain for FluidDB must *not* end with'\184 raise ValueError('The domain for FluidDB must *not* end with'\
185 ' "/". Correct example:'\185 ' "/". Correct example:'\
186 ' https://fluiddb.fluidinfo.com')186 ' https://fluiddb.fluidinfo.com')
187 self._http = httplib2.Http()
188 self.base_url = base_url187 self.base_url = base_url
189 self.headers = {188 self.headers = {
190 'User-agent': 'fom/%s' % version,189 'User-agent': 'fom/%s' % version,
191 }190 }
191 self.session = requests.session(headers=self.headers)
192 # XXX Backwards compat192 # XXX Backwards compat
193 self.client = self193 self.client = self
194194
@@ -212,20 +212,16 @@
212 `application/vnd.fluiddb.value+json` even if they are of a212 `application/vnd.fluiddb.value+json` even if they are of a
213 deserializable content type such as `application/json`213 deserializable content type such as `application/json`
214 """214 """
215 req, params = self._build_request(method, path, payload, urlargs,
216 content_type)
217 fom_request_sent.send(self, request=params)
218 response, content = req(*params)
219 fom_response_received.send(self, response=(response.status,
220 content, response.copy()))
221 return FluidResponse(response, content, is_value)
222
223 def _build_request(self, method, path, payload, urlargs, content_type):
224 payload, content_type = _get_body_and_type(payload, content_type)215 payload, content_type = _get_body_and_type(payload, content_type)
225 urlargs = urlargs or {}216 urlargs = urlargs or {}
226 headers = self._get_headers(content_type)217 headers = self._get_headers(content_type)
227 url = self._get_url(path, urlargs)218 url = self._get_url(path, urlargs)
228 return self._http.request, (url, method, payload, headers)219 fom_request_sent.send(self, request=(url, method, payload, headers))
220 response = self.session.request(method, url, data=payload,
221 headers=headers)
222 fom_response_received.send(self, response=(response.status_code,
223 response.text, None))
224 return FluidResponse(response, response.text, is_value)
229225
230 def _get_headers(self, content_type):226 def _get_headers(self, content_type):
231 headers = self.headers.copy()227 headers = self.headers.copy()
232228
=== modified file 'fom/tx.py'
--- fom/tx.py 2010-11-29 12:52:12 +0000
+++ fom/tx.py 2012-04-11 13:45:33 +0000
@@ -73,9 +73,10 @@
73 """73 """
7474
75 def __init__(self, response):75 def __init__(self, response):
76 self.status = response.code76 self.status_code = response.code
77 self.headers = {}
77 for k, v in response.headers.getAllRawHeaders():78 for k, v in response.headers.getAllRawHeaders():
78 self[k.lower()] = v[0]79 self.headers[k.lower()] = v[0]
7980
8081
81class TxFluidDB(FluidDB):82class TxFluidDB(FluidDB):
8283
=== modified file 'fom/version.py'
--- fom/version.py 2011-12-12 11:38:16 +0000
+++ fom/version.py 2012-04-11 13:45:33 +0000
@@ -9,5 +9,5 @@
9 :license: MIT, see LICENSE for more information.9 :license: MIT, see LICENSE for more information.
10"""10"""
1111
12version = '0.9.7'12version = '0.9.8'
1313
1414
=== modified file 'setup.py'
--- setup.py 2012-03-05 16:51:35 +0000
+++ setup.py 2012-04-11 13:45:33 +0000
@@ -13,6 +13,6 @@
13 url='http://packages.python.org/Fom/',13 url='http://packages.python.org/Fom/',
14 packages=['fom'],14 packages=['fom'],
15 scripts=['bin/fdbc'],15 scripts=['bin/fdbc'],
16 install_requires=['httplib2', 'blinker'],16 install_requires=['requests', 'blinker'],
17)17)
1818
1919
=== modified file 'tests/_base.py'
--- tests/_base.py 2010-11-30 14:59:41 +0000
+++ tests/_base.py 2012-04-11 13:45:33 +0000
@@ -12,15 +12,17 @@
12from collections import deque12from collections import deque
1313
1414
15from fom.db import FluidDB, _generate_endpoint_url15from fom.db import FluidDB, _generate_endpoint_url, NO_CONTENT, FluidResponse
1616
1717
18class FakeHttpLibResponse(dict):18class FakeHttpLibResponse(dict):
1919
20 def __init__(self, status, content_type):20 def __init__(self, status, content_type, content=None):
21 # yeah, I know, blame httplib2 for this API21 # yeah, I know, blame httplib2 for this API
22 self.status = status22 self.status_code = status
23 self['content-type'] = content_type23 self.headers = {}
24 self.headers['content-type'] = content_type
25 self.text = content
2426
2527
26class FakeHttpLibRequest(object):28class FakeHttpLibRequest(object):
@@ -39,14 +41,14 @@
39 FluidDB.__init__(self, 'http://testing')41 FluidDB.__init__(self, 'http://testing')
40 self.reqs = []42 self.reqs = []
41 self.resps = deque()43 self.resps = deque()
42 self.default_response = (44 self.default_response = FakeHttpLibResponse(200, 'text/plain', 'empty')
43 FakeHttpLibResponse(200, 'text/plain'), 'empty')
4445
45 def add_resp(self, status, content_type, content):46 def add_resp(self, status, content_type, content):
46 hresp = FakeHttpLibResponse(status, content_type)47 hresp = FakeHttpLibResponse(status, content_type, content)
47 self.resps.append((hresp, content))48 self.resps.append(hresp)
4849
49 def _build_request(self, method, path, payload, urlargs, content_type):50 def __call__(self, method, path, payload=NO_CONTENT, urlargs=None,
51 content_type=None, is_value=False):
50 path = _generate_endpoint_url('', path, '')52 path = _generate_endpoint_url('', path, '')
51 req = (method, path, payload, urlargs, content_type)53 req = (method, path, payload, urlargs, content_type)
52 self.reqs.append(req)54 self.reqs.append(req)
@@ -54,5 +56,4 @@
54 resp = self.resps.popleft()56 resp = self.resps.popleft()
55 except IndexError:57 except IndexError:
56 resp = self.default_response58 resp = self.default_response
57 return FakeHttpLibRequest(resp), (method, path, payload, urlargs,59 return FluidResponse(resp, resp.text, is_value)
58 content_type)
5960
=== modified file 'tests/test_base.py'
--- tests/test_base.py 2010-12-24 15:30:04 +0000
+++ tests/test_base.py 2012-04-11 13:45:33 +0000
@@ -22,5 +22,5 @@
22 payload = 'fluiddb'22 payload = 'fluiddb'
23 urlargs = {'ham': 'eggs'}23 urlargs = {'ham': 'eggs'}
24 content_type = 'text/plain'24 content_type = 'text/plain'
25 fake._build_request(method, path, payload, urlargs, content_type)25 fake(method, path, payload, urlargs, content_type)
26 self.assertEqual('/foo%2Fbar/baz/qux', fake.reqs[0][1])26 self.assertEqual('/foo%2Fbar/baz/qux', fake.reqs[0][1])
2727
=== modified file 'tests/test_tx.py'
--- tests/test_tx.py 2011-01-19 10:26:21 +0000
+++ tests/test_tx.py 2012-04-11 13:45:33 +0000
@@ -8,7 +8,7 @@
8class TestTxFluidDB(unittest.TestCase):8class TestTxFluidDB(unittest.TestCase):
99
10 def setUp(self):10 def setUp(self):
11 self.db = TxFluidDB('https://sandbox.fluidinfo.com')11 self.db = TxFluidDB('http://sandbox.fluidinfo.com')
1212
13 @defer.inlineCallbacks13 @defer.inlineCallbacks
14 def testRequest(self):14 def testRequest(self):

Subscribers

People subscribed via source and target branches

to all changes: