Skip to content

Commit

Permalink
Start writing the documentation
Browse files Browse the repository at this point in the history
 * add bootstrap theme and configuration
 * update the synopsis for HTTPClient
 * start to write a tutorial on how to use the library and how to write tests
   with a custom useragent
  • Loading branch information
fcuny committed Feb 10, 2012
1 parent e5280d7 commit 96a7ae5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 7 deletions.
Binary file added docs/_themes/bootstrap.zip
Binary file not shown.
4 changes: 3 additions & 1 deletion docs/conf.py
Expand Up @@ -16,6 +16,7 @@
import http
except ImportError:
sys.path.append(os.path.abspath('../'))
sys.path.append(os.path.abspath('_themes'))

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down Expand Up @@ -95,7 +96,8 @@

# The theme to use for HTML and HTML Help pages. Major themes that come with
# Sphinx are currently 'default' and 'sphinxdoc'.
html_theme = 'default'
html_theme_path = ['_themes']
html_theme = 'bootstrap'

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
Expand Down
14 changes: 13 additions & 1 deletion docs/httpclient.rst
Expand Up @@ -8,11 +8,23 @@ HTTPClient
Synopsis
--------

::

>>> from http import Request
>>> from httpclient import HTTPClient
>>> request = Request('GET', 'http://lumberjaph.net')
>>> client = HTTPClient()
>>> client.request(request)
>>> response = client.request(request)
>>> print response.status
200


Interface
---------

:class:`HTTPClient` instances have the following methods:

.. autoclass:: HTTPClient()
:members:
:undoc-members:
:undoc-members:
8 changes: 4 additions & 4 deletions docs/index.rst
Expand Up @@ -9,10 +9,10 @@ This module is heavily inspired by LWP::UserAgent

>>> from httpclient import HTTPClient
>>> from http import Request
>>> r = Request('GET', 'http://lumberjaph.net')
>>> c = HTTPClient()
>>> resp = c.request(r)
>>> print resp.status
>>> request = Request('GET', 'http://lumberjaph.net')
>>> cclient = HTTPClient()
>>> response = cclient.request(request)
>>> print response.status
200

User Guide
Expand Down
77 changes: 76 additions & 1 deletion docs/tutorial.rst
Expand Up @@ -14,4 +14,79 @@ Then a Request object is created. The request is then passed to the client, whic

The default client will set the timeout to 60 seconds, the useragent string to 'python-http', and the number of redirection to follow to 7.

>>> client = Client(agent='my uber application', timeout=10)
>>> client = Client(agent='my uber application', timeout=10)

How to use this library
-----------------------

For the purpose of this documentation, let's start with a simple client for the GitHub API.

::

from httpclient import HTTPClient
from http import Request, Url
import json


class GitHubClient(object):

def __init__(self, user_agent=None, base_url='https://api.github.com'):
self.base_url = base_url
if user_agent is None:
self._user_agent = HTTPClient()
else:
self._user_agent = user_agent

@property
def user_agent(self):
return self._user_agent

@user_agent.setter
def user_agent(self, user_agent):
self._user_agent = user_agent

def get_user(self, username):
endpoint = Url(self.base_url)
endpoint.path.append('users')
endpoint.path.append(username)
request = Request('GET', endpoint)
response = self.user_agent.request(request)
if response.is_success:
return json.loads(response.content)
else:
raise Exception(response.status_line)

::

>>> gh = GitHubClient()
>>> res = gh.get_user('franckcuny')
>>> print res['name']
'franck'

Setting the useragent
~~~~~~~~~~~~~~~~~~~~~

You should let the possibility for the developper to set its own ``user_agent``.

How to test
-----------

Now, we will add tests for this client.

::

def _cb(request):
response = Response(200, content=json.dumps({'name': 'batman'}))
return response
class Test(unittest.TestCase):
def testName(self):
ua = HTTPClient()
ua.add_handler('request_send', _cb)
client = GitHubClient(user_agent=ua)
res = client.get_user('franckcuny')
self.assertEqual(res['name'], 'batman')
If we execute these test, the callback ``cb`` will be called, and since it returns a class:``Response`` object, the request is never sent over the wire.

0 comments on commit 96a7ae5

Please sign in to comment.