Skip to content

Commit

Permalink
v0.1.2, Add async support based on asynchttp
Browse files Browse the repository at this point in the history
- Attempt to use the asynchttp module in place of httplib2.
- Add async kwarg to Request and Session run() methods.
- Remove unneeded imports from version specific session modules.
- Update docs with changelog.
- Bump to version 0.1.2
  • Loading branch information
casebeer committed Jun 21, 2012
1 parent d67ad86 commit d29e518
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -147,6 +147,13 @@ See also the Python documentation for session.Session and requests.Read and [Fac
- Write support for v3, when available
- Multiple search filters (search filters currently replace one another)

## Changelog

- v0.1.2 - Add async option to Request.run() to permit delaying the processing of the HTTP response.
If the asynchttp module is installed, this will cause the initial call to run(async=True)
to immediately return a get_response function, allowing you to defer the blocking
call until the results are needed.

[factual_docs]: http://developer.factual.com/display/docs/Factual+Developer+APIs+Version+3
[factual_requests_KEY]: http://developer.factual.com/display/docs/Core+API+-+Oauth#CoreAPI-Oauth-KeysandSecrets
[factual_signup]: https://www.factual.com/api-keys/request
19 changes: 17 additions & 2 deletions factual/common/requests.py
Expand Up @@ -16,8 +16,23 @@ def __init__(self, session, table):
self.session = session
# instantiate the table class if we're passed a type rather than an instance (or a string)
self.table = table() if isinstance(table, type) else table
def run(self):
return self.session.run(self)
def run(self, async=False):
'''
Perform the Factual API HTTP request.
By default, process and return the API response wrapped in a
FactualResponse object.
Pass `async = True` to perform the request asynchronously; in
async mode, `run` returns a function, get_response, that will
process and return the response when called.
Additionally, if the asynchttp module is available and async is
True, the initial HTTP request will not block, and get_response
will be returned immediately. get_response function will block
when called if the HTTP requst is not yet complete.
'''
return self.session.run(self, async)
def make_response(self, *args, **kwargs):
response = self.response_type(*args, **kwargs)
return response
Expand Down
36 changes: 29 additions & 7 deletions factual/common/session.py
@@ -1,5 +1,9 @@
import logging
import httplib2
try:
import asynchttp as http
except ImportError:
import httplib2 as http

try:
import simplejson as json
except ImportError:
Expand Down Expand Up @@ -39,17 +43,35 @@ def get_url(self, request):
}
def get_headers(self, request, defaults={}):
return {}.update(defaults)
def run(self, request):
def run(self, request, async=False):
url = self.get_url(request)
headers = self.get_headers(request)

logging.debug(url)
logging.debug(headers)

h = httplib2.Http()
h = http.Http()
http_response, http_body = h.request(url, headers=headers)
# todo: timing and other metrics
meta = {}
response = request.make_response(json.loads(http_body), meta=meta)
return response

def get_response():
'''
Process and return the HTTP response from Factual.
Performs the post-request processing needed to handle a Factual
response. Broken into a separate function so the post processing
can be deferred, e.g. for use with asynchttp in place of
httplib2.
When the asynchttp module is installed, this call will block
if the HTTP request to Factual is not yet complete.
'''
# todo: timing and other metrics
meta = {}
response = request.make_response(json.loads(str(http_body)), meta=meta)
return response

if async:
return get_response
else:
return get_response()

5 changes: 0 additions & 5 deletions factual/v2/session.py
@@ -1,9 +1,4 @@
import logging
import httplib2
try:
import simplejson as json
except ImportError:
import json

import requests
import tables
Expand Down
5 changes: 0 additions & 5 deletions factual/v3/session.py
@@ -1,10 +1,5 @@
import logging
try:
import simplejson as json
except ImportError:
import json

import httplib2
import oauth2

import requests
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -14,7 +14,7 @@

setup(
name="factual",
version="0.1.1",
version="0.1.2",
description="",
author="Christopher H. Casebeer",
author_email="",
Expand Down

0 comments on commit d29e518

Please sign in to comment.