blob: 4f93e293679e481c33c9464a85c351f859587528 [file] [log] [blame]
jcgregorio26c0cd72006-07-03 17:36:17 +00001% Template for a library manual section.
2% PLEASE REMOVE THE COMMENTS AFTER USING THE TEMPLATE
3%
4% Complete documentation on the extended LaTeX markup used for Python
5% documentation is available in ``Documenting Python'', which is part
6% of the standard documentation for Python. It may be found online
7% at:
8%
9% http://www.python.org/doc/current/doc/doc.html
10
11% ==== 0. ====
12% Copy this file to <mydir>/lib<mymodule>.tex, and edit that file
13% according to the instructions below.
14
15
16% ==== 1. ====
17% The section prologue. Give the section a title and provide some
18% meta-information. References to the module should use
19% \refbimodindex, \refstmodindex, \refexmodindex or \refmodindex, as
20% appropriate.
21
22
23\section{\module{httplib2}
24 A comprehensive HTTP client library. }
25
26% Choose one of these to specify the module module name. If there's
27% an underscore in the name, use
28% \declaremodule[modname]{...}{mod_name} instead.
29%
30\declaremodule{}{httplib2} % not standard, in Python
31
32% Portability statement: Uncomment and fill in the parameter to specify the
33% availability of the module. The parameter can be Unix, IRIX, SunOS, Mac,
34% Windows, or lots of other stuff. When ``Mac'' is specified, the availability
35% statement will say ``Macintosh'' and the Module Index may say ``Mac''.
36% Please use a name that has already been used whenever applicable. If this
37% is omitted, no availability statement is produced or implied.
38%
39% \platform{Unix}
40
41% These apply to all modules, and may be given more than once:
42
43\moduleauthor{Joe Gregorio}{joe@bitworking.org} % Author of the module code;
44 % omit if not known.
45\sectionauthor{Joe Gregorio}{joe@bitworking.org} % Author of the documentation,
46 % even if not a module section.
47
48
49% Leave at least one blank line after this, to simplify ad-hoc tools
50% that are sometimes used to massage these files.
51\modulesynopsis{A comprehensive HTTP client library, \module{httplib2} supports many features left out of other HTTP libraries.}
52
53
54% ==== 2. ====
55% Give a short overview of what the module does.
56% If it is platform specific, mention this.
57% Mention other important restrictions or general operating principles.
58% For example:
59
60The \module{httplib2} module is a comprehensive HTTP client library with the following features:
61
62\begin{description}
63\item[HTTP and HTTPS] HTTPS support is only available if the socket module was compiled with SSL support.
64\item[Keep-Alive] Supports HTTP 1.1 Keep-Alive, keeping the socket open and performing multiple requests over the same connection if possible.
65\item[Authentication] The following three types of HTTP Authentication are supported. These can be used over both HTTP and HTTPS.
66 \begin{itemize}
67 \item Digest
68 \item Basic
69 \item WSSE
70 \end{itemize}
71\item[Caching]
72 The module can optionally operate with a private cache that understands the Cache-Control: header and uses both the ETag and Last-Modified cache validators.
73\item[All Methods]
74 The module can handle any HTTP request method, not just GET and POST.
75\item[Redirects]
76 Automatically follows 3XX redirects on GETs.
77\item[Compression]
78 Handles both 'deflate' and 'gzip' types of compression.
joe.gregorio5fcc1992007-10-23 15:12:17 +000079\item[Proxies]
80 If the Socksipy module is installed then httplib2 can handle sock4, sock5 and http proxies.
jcgregorio26c0cd72006-07-03 17:36:17 +000081\item[Lost update support]
82 Automatically adds back ETags into PUT requests to resources we have already cached. This implements Section 3.2 of Detecting the Lost Update Problem Using Unreserved Checkout
83\end{description}
84
85% ==== 3. ====
86% List the public functions defined by the module. Begin with a
87% standard phrase. You may also list the exceptions and other data
88% items defined in the module, insofar as they are important for the
89% user.
90
91The \module{httplib2} module defines the following variables:
92% ---- 3.2. ----
93% Data items are described using a ``datadesc'' block. This has only
94% one parameter: the item's name.
95
96\begin{datadesc}{debuglevel}
97The amount of debugging information to print. The default is 0.
98\end{datadesc}
99
100% --- 3.3. ---
101% Exceptions are described using a ``excdesc'' block. This has only
102% one parameter: the exception name. Exceptions defined as classes in
103% the source code should be documented using this environment, but
104% constructor parameters must be omitted.
105
jcgregoriocde4f092007-03-08 21:59:04 +0000106The \module{httplib2} module may raise the following Exceptions. Note that
107there is an option that turns exceptions into
108normal responses with an HTTP status code indicating
109an error occured. See \member{Http.force_exception_to_status_code}
jcgregorio26c0cd72006-07-03 17:36:17 +0000110
111\begin{excdesc}{HttpLib2Error}
112The Base Exception for all exceptions raised by httplib2.
113\end{excdesc}
114
115\begin{excdesc}{RedirectMissingLocation}
116A 3xx redirect response code was provided but no Location: header
117was provided to point to the new location.
118\end{excdesc}
119
jcgregorio26c0cd72006-07-03 17:36:17 +0000120\begin{excdesc}{RedirectLimit}
121The maximum number of redirections was reached without coming to a final URI.
122\end{excdesc}
123
124
jcgregoriocde4f092007-03-08 21:59:04 +0000125\begin{excdesc}{ServerNotFoundError}
126Unable to resolve the host name given.
127\end{excdesc}
128
129\begin{excdesc}{RelativeURIError}
130A relative, as opposed to an absolute URI, was passed into request().
131\end{excdesc}
132
jcgregorio26c0cd72006-07-03 17:36:17 +0000133\begin{excdesc}{FailedToDecompressContent}
134The headers claimed that the content of the response was compressed but the
135decompression algorithm applied to the content failed.
136\end{excdesc}
137
jcgregorio26c0cd72006-07-03 17:36:17 +0000138\begin{excdesc}{UnimplementedDigestAuthOptionError}
139The server requested a type of Digest authentication that we
140are unfamiliar with.
141\end{excdesc}
142
143\begin{excdesc}{UnimplementedHmacDigestAuthOptionError}
144The server requested a type of HMACDigest authentication that we
145are unfamiliar with.
146\end{excdesc}
147
148% ---- 3.4. ----
149% Other standard environments:
150%
151% classdesc - Python classes; same arguments are funcdesc
152% methoddesc - methods, like funcdesc but has an optional parameter
153% to give the type name: \begin{methoddesc}[mytype]{name}{args}
154% By default, the type name will be the name of the
155% last class defined using classdesc. The type name
156% is required if the type is implemented in C (because
157% there's no classdesc) or if the class isn't directly
158% documented (if it's private).
159% memberdesc - data members, like datadesc, but with an optional
160% type name like methoddesc.
161
joe.gregorio5fcc1992007-10-23 15:12:17 +0000162\begin{classdesc}{Http}{\optional{cache=None}, \optional{timeout=None}, \optional{proxy_info=None}}
jcgregorio26c0cd72006-07-03 17:36:17 +0000163The class that represents a client HTTP interface.
164The \var{cache} parameter is either the name of a directory
165to be used as a flat file cache, or it must an object that
166implements the required caching interface.
jcgregorioafc233d2007-03-09 02:30:04 +0000167The \var{timeout} parameter is the socket level timeout.
joe.gregorio5fcc1992007-10-23 15:12:17 +0000168The \var{proxy_info} is an instance of \class{ProxyInfo} and is supplied
169if a proxy is to be used. Note that the Socksipy module must be
170installed for proxy support to work.
jcgregorio26c0cd72006-07-03 17:36:17 +0000171\end{classdesc}
172
173\begin{classdesc}{Response}{info}
174Response is a subclass of \class{dict} and instances of this
175class are returned from calls
176to Http.request. The \var{info} parameter is either
177an \class{rfc822.Message} or an \class{httplib.HTTPResponse} object.
178\end{classdesc}
179
jcgregorioee542cc2006-11-16 04:43:19 +0000180\begin{classdesc}{FileCache}{dir_name, \optional{safe=safename}}
181FileCache implements a Cache as a directory of files.
182The \var{dir_name} parameter is
183the name of the directory to use. If the directory does
184not exist then FileCache attempts to create the directory.
185The optional \var{safe} parameter is a funtion which generates
186the cache filename for each URI. A FileCache object is
187constructed and used for caching when you pass a directory name
188into the constructor of \class{Http}.
189\end{classdesc}
jcgregorio26c0cd72006-07-03 17:36:17 +0000190
joe.gregorio5fcc1992007-10-23 15:12:17 +0000191\begin{classdesc}{ProxyInfo}{proxy_type, proxy_host, proxy_port, \optional{proxy_rdns=None}, \optional{proxy_user=None}, \optional{proxy_pass=None}}
192The parameter \var{proxy_type} must be set to one of socks.PROXY_TYPE_XXX
193constants. The \var{proxy_host} and \var{proxy_port} must be set to the location
194of the proxy. The optional \var{proxy_rdns} should be set to True if
195the DNS server on the proxy should be used. The \var{proxy_user} and
196\var{proxy_pass} are supplied when the proxy is protected by authentication.
197\end{classdesc}
198
jcgregorio26c0cd72006-07-03 17:36:17 +0000199
200% If your module defines new object types (for a built-in module) or
201% classes (for a module written in Python), you should list the
202% methods and instance variables (if any) of each type or class in a
203% separate subsection.
204
205\subsection{Http Objects}
206\label{http-objects}
207% This label is generally useful for referencing this section, but is
208% also used to give a filename when generating HTML.
209
210Http objects have the following methods:
211
jcgregorio14644372007-07-30 14:13:37 +0000212\begin{methoddesc}[Http]{request}{uri, \optional{method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS, connection_type=None}}
jcgregorio26c0cd72006-07-03 17:36:17 +0000213Performs a single HTTP request.
214The \var{uri} is the URI of the HTTP resource and can begin with either \code{http} or \code{https}. The value of \var{uri} must be an absolute URI.
215
216The \var{method} is the HTTP method to perform, such as \code{GET}, \code{POST}, \code{DELETE}, etc. There is no restriction
217on the methods allowed.
218
219The \var{body} is the entity body to be sent with the request. It is a string
220object.
221
222Any extra headers that are to be sent with the request should be provided in the
223\var{headers} dictionary.
224
225The maximum number of redirect to follow before raising an exception is \var{redirections}. The default is 5.
226
jcgregorio14644372007-07-30 14:13:37 +0000227The \var{connection_type} is the type of connection object to use. The supplied class
228should implement the interface of httplib.HTTPConnection.
229
jcgregorio26c0cd72006-07-03 17:36:17 +0000230The return value is a tuple of (response, content), the first being and instance of the
231\class{Response} class, the second being a string that contains the response entity body.
232\end{methoddesc}
233
jcgregorioe6a01962007-03-09 02:52:48 +0000234\begin{methoddesc}[Http]{add_credentials}{name, password, \optional{domain=None}}
jcgregorio26c0cd72006-07-03 17:36:17 +0000235Adds a name and password that will be used when a request
jcgregorioe6a01962007-03-09 02:52:48 +0000236requires authentication. Supplying the optional \var{domain} name will
237restrict these credentials to only be sent to the specified
238domain. If \var{domain} is not specified then the given credentials will
239be used to try to satisfy every HTTP 401 challenge.
240\end{methoddesc}
241
242\begin{methoddesc}[Http]{add_certificate}{key, cert, domain}
243Add a \var{key} and \var{cert} that will be used for an SSL connection
244to the specified domain. \var{keyfile} is the name of a PEM formatted
245file that contains your private key. \var{certfile} is a PEM formatted certificate chain file.
jcgregorio26c0cd72006-07-03 17:36:17 +0000246\end{methoddesc}
247
248\begin{methoddesc}[Http]{clear_credentials}{}
249Remove all the names and passwords used for authentication.
250\end{methoddesc}
251
jcgregorio2f1e1422007-05-03 13:17:33 +0000252\begin{memberdesc}[Http]{follow_redirects}
253If \code{True}, which is the default, safe redirects are followed, where
254safe means that the client is only doing a \code{GET} or \code{HEAD} on the
255URI to which it is being redirected. If \code{False} then no redirects are followed.
256Note that a False 'follow_redirects' takes precedence over a True 'follow_all_redirects'.
257Another way of saying that is for 'follow_all_redirects' to have any affect, 'follow_redirects'
jcgregorio5dc440b2007-05-03 13:23:27 +0000258must be True.
jcgregorio2f1e1422007-05-03 13:17:33 +0000259\end{memberdesc}
260
jcgregorio26c0cd72006-07-03 17:36:17 +0000261\begin{memberdesc}[Http]{follow_all_redirects}
jcgregorio25185622006-10-28 05:12:34 +0000262If \code{False}, which is the default, only safe redirects are followed, where
jcgregorio26c0cd72006-07-03 17:36:17 +0000263safe means that the client is only doing a \code{GET} or \code{HEAD} on the
jcgregorio25185622006-10-28 05:12:34 +0000264URI to which it is being redirected. If \code{True} then all redirects are followed.
jcgregorio5dc440b2007-05-03 13:23:27 +0000265Note that a False 'follow_redirects' takes precedence over a True 'follow_all_redirects'.
266Another way of saying that is for 'follow_all_redirects' to have any affect, 'follow_redirects'
267must be True.
jcgregorio26c0cd72006-07-03 17:36:17 +0000268\end{memberdesc}
269
jcgregoriocde4f092007-03-08 21:59:04 +0000270\begin{memberdesc}[Http]{force_exception_to_status_code}
271If \code{True}, which is the default, then no \module{httplib2} exceptions will be thrown. Instead,
272those error conditions will be turned into \class{Response} objects
273that will be returned normally.
274
275If \code{False}, then exceptions will be thrown.
276\end{memberdesc}
277
jcgregorio25185622006-10-28 05:12:34 +0000278\begin{memberdesc}[Http]{ignore_etag}
jcgregorio87eee732007-03-09 03:23:11 +0000279Defaults to \code{False}. If \code{True}, then any etags present in the cached response
280are ignored when processing the current request, i.e. httplib2 does \strong{not} use
jcgregorio25185622006-10-28 05:12:34 +0000281'if-match' for PUT or 'if-none-match' when GET or HEAD requests are made. This
282is mainly to deal with broken servers which supply an etag, but change it capriciously.
283\end{memberdesc}
jcgregorio26c0cd72006-07-03 17:36:17 +0000284
285\subsection{Cache Objects}
286\label{cache-objects}
287% This label is generally useful for referencing this section, but is
288% also used to give a filename when generating HTML.
289
290If you wish to supply your own caching implementation
291then you will need to pass in an object that supports the
292following methods. Note that the \module{memcache} module
293supports this interface natively.
294
295\begin{methoddesc}[Cache]{get}{key}
296Takes a string \var{key} and returns the value as a string.
297\end{methoddesc}
298
299\begin{methoddesc}[Cache]{set}{key, value}
300Takes a string \var{key} and \var{value} and stores it in the cache.
301\end{methoddesc}
302
303\begin{methoddesc}[Cache]{delete}{key}
304Deletes the cached value stored at \var{key}. The value
305of \var{key} is a string.
306\end{methoddesc}
307
308
309
310
jcgregorio26c0cd72006-07-03 17:36:17 +0000311\subsection{Response Objects}
312\label{response-objects}
313% This label is generally useful for referencing this section, but is
314% also used to give a filename when generating HTML.
315
316Response objects are derived from \class{dict} and map
317header names (lower case with the trailing colon removed)
318to header values. In addition to the dict methods
319a Response object also has:
320
321\begin{memberdesc}[Response]{fromcache}
322If \code{true} the the response was returned from the cache.
323\end{memberdesc}
324
325\begin{memberdesc}[Response]{version}
326The version of HTTP that the server supports. A value
327of 11 means '1.1'.
328\end{memberdesc}
329
330\begin{memberdesc}[Response]{status}
331The numerical HTTP status code returned in the response.
332\end{memberdesc}
333
334\begin{memberdesc}[Response]{reason}
335The human readable component of the HTTP response status code.
336\end{memberdesc}
337
338\begin{memberdesc}[Response]{previous}
339If redirects are followed then the \class{Response} object returned
340is just for the very last HTTP request and \var{previous} points to
341the previous \class{Response} object. In this manner they form a chain
342going back through the responses to the very first response.
343Will be \code{None} if there are no previous respones.
344\end{memberdesc}
345
jcgregorio772adc82006-11-17 21:52:34 +0000346The Response object also populates the header \code{content-location}, that
jcgregoriof76c9512006-11-07 18:01:50 +0000347contains the URI that was ultimately requested. This is useful if
348redirects were encountered, you can determine the ultimate URI that
349the request was sent to. All Response objects contain this key value,
350including \code{previous} responses so you can determine the entire
jcgregoriocde4f092007-03-08 21:59:04 +0000351chain of redirects. If \member{Http.force_exception_to_status_code} is \code{True}
352and the number of redirects has exceeded the number of allowed number
353of redirects then the \class{Response} object will report the error
354in the status code, but the complete chain of previous responses will
355still be in tact.
356
jcgregorio26c0cd72006-07-03 17:36:17 +0000357
358% ==== 4. ====
359% Now is probably a good time for a complete example. (Alternatively,
360% an example giving the flavor of the module may be given before the
361% detailed list of functions.)
362
363\subsection{Examples \label{httplib2-example}}
364
365To do a simple \code{GET} request just supply the absolute URI
366of the resource:
367
368\begin{verbatim}
369import httplib2
370h = httplib2.Http()
371resp, content = h.request("http://bitworking.org/")
372assert resp.status == 200
373assert resp['content-type'] == 'text/html'
374\end{verbatim}
375
376Here is more complex example that does a PUT
377of some text to a resource that requires authentication.
378The Http instance also uses a file cache
379in the directory \code{.cache}.
380
381\begin{verbatim}
382import httplib2
383h = httplib2.Http(".cache")
384h.add_credentials('name', 'password')
385resp, content = h.request("https://example.org/chap/2",
386 "PUT", body="This is text",
387 headers={'content-type':'text/plain'} )
388\end{verbatim}
389
390Here is an example that connects to a server that
391supports the Atom Publishing Protocol.
392
393\begin{verbatim}
394import httplib2
395h = httplib2.Http()
396h.add_credentials(myname, mypasswd)
397h.follow_all_redirects = True
398headers = {'Content-Type': 'application/atom+xml'}
399body = """<?xml version="1.0" ?>
400 <entry xmlns="http://www.w3.org/2005/Atom">
401 <title>Atom-Powered Robots Run Amok</title>
402 <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
403 <updated>2003-12-13T18:30:02Z</updated>
404 <author><name>John Doe</name></author>
405 <content>Some text.</content>
406</entry>
407"""
408uri = "http://www.example.com/collection/"
409resp, content = h.request(uri, "POST", body=body, headers=headers)
410\end{verbatim}
411% Note that there is no trailing ">>> " prompt shown.
412
jcgregoriocde4f092007-03-08 21:59:04 +0000413Here is an example of providing data to an HTML form processor.
414In this case we presume this is a POST form. We need to take our
415data and format it as "application/x-www-form-urlencoded" data and use that as a
416body for a POST request.
jcgregorio26c0cd72006-07-03 17:36:17 +0000417
jcgregoriocde4f092007-03-08 21:59:04 +0000418\begin{verbatim}
419>>> import httplib2
420>>> import urllib
421>>> data = {'name': 'fred', 'address': '123 shady lane'}
422>>> body = urllib.urlencode(data)
423>>> body
424'name=fred&address=123+shady+lane'
425>>> h = httplib2.Http()
426>>> resp, content = h.request("http://example.com", method="POST", body=body)
427\end{verbatim}
428% Note that there is no trailing ">>> " prompt shown.
joe.gregorio5fcc1992007-10-23 15:12:17 +0000429
430Here is an example of using a proxy server:
431\begin{verbatim}
432import httplib2
433import socks
434
435httplib2.debuglevel=4
436h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'localhost', 8000))
437r,c = h.request("http://bitworking.org/news/")
438\end{verbatim}
439
440
441