client¶
by now, hopefully you are somewhat familiar with the wsgi interface and how webob Request and Response play a role.
We’ve seen numerous examples of how small snippets of wsgi middleware can be composed into a pretty functional application that communicates via HTTP.
We’ve also covered how those applications can be hosted by a web server and then interacted with by web browsers or other clients.
And finally we saw that any website can be accessed as if it were a python callable that adheres to the wsgi interface.
WebObToolkit includes a client that you can use for interacting with wsgi applications and other websites. You can use it to test wsgi applications or websites, or use it as a means of using services from those applications or websites in your programs.
"""
example client usage
"""
from webobtoolkit.client import Client, client_pipeline
from webobtoolkit.proxy import send_request_app
import logging
logging.basicConfig(level="DEBUG")
# first we make an pipeline
pipeline = client_pipeline(wsgi=send_request_app, # this wsgi app sends the request to the url you specify
cookie_support=True, # turn on cookie support
content_decoding=True, # decompress responses if necessary
logging=True, # turn on logging
log_level="DEBUG") # set log level
client = Client(pipeline=pipeline)
response = client.get("http://www.google.com", query_string=(dict(q="wsgi as http client")))
assert response.status_int == 200, "something went wrong"
In the above code you see that we are constructing a client_pipeline to give the client to use. By default it will use webobtoolkit.client.client_app an instance of webobtoolkit.client.client_pipeline <Reference> which is pre-configured for cookie support and gzip content decoding.
Reference¶
-
webobtoolkit.client.client_pipeline(app=<webob.client.SendRequest instance>, cookie_support=True, content_decoding=True, logging=False, log_level=None)¶ Return type: pre-configured WSGI Application Parameters: app – is a WSGI Application to wrap, the default is webob.client.send_request_app
Parameters: cookie_support – enables/disables the filters.cookie_filter()Parameters: content_decoding – enables/disables the filters.decode_filter()Parameters: - logging – enables/disables the
filters.http_log_filter() - log_level – the log_level for
filters.http_log_filter()
- logging – enables/disables the
-
class
webobtoolkit.client.Client(pipeline=None, assert_=None)¶ Parameters: pipeline – wsgi application to pass requests to, default is client.client_app()Parameters: assert – a callback lambda: request, response: True that will be called for every call to app
-
delete(url, query_string=None, post={}, headers={}, assert_=None)¶ make an HTTP DELETE Request and return the response
Return type: webob.ResponseParameters: - url – the url for the request
- query_string – the querystring dict which will be urlencoded for you
- post – form post
- headers – extra headers fpr the request
- assert – a callback to be ran after the response is recieved in the form of lambda: request, response: True . If present it will be ran for this call only rather than the one set on the client
-
get(url, query_string=None, headers={}, assert_=None)¶ make an HTTP GET Request and return the response
Return type: webob.ResponseParameters: - url – the url for the request
- query_string – the querystring dict which will be
urlencoded for you
Parameters: - headers – extra headers for the request
- assert – a callback to be ran after the response is
recieved in the form of lambda: request, response: True . If present it will be ran for this call only rather than the one set on the client
-
head(url, query_string=None, headers={}, assert_=None)¶ make an HTTP HEAD Request and return the response
Return type: webob.ResponseParameters: - url – the url for the request
- query_string – the querystring dict which will be urlencoded for you
- headers – extra headers for the request
- assert – a callback to be ran after the response is
recieved in the form of lambda: request, response: True . If present it will be ran for this call only rather than the one set on the client
-
options(url, query_string=None, post={}, headers={}, assert_=None)¶ make an HTTP OPTIONS Request and return the response
Return type: webob.ResponseParameters: - url – the url for the request
- query_string – the querystring dict which will be urlencoded for you
- post – form post
- headers – extra headers fpr the request
- assert – a callback to be ran after the response is recieved in the form of lambda: request, response: True . If present it will be ran for this call only rather than the one set on the client
-
post(url, query_string=None, post={}, headers={}, assert_=None, files={})¶ make an HTTP POST Request and return the response
Return type: webob.ResponseParameters: - url – the url for the request
- query_string – the querystring dict which will be
urlencoded for you
Parameters: - post – form post
- headers – extra headers fpr the request
- assert – a callback to be ran after the response is
recieved in the form of lambda: request, response: True . If present it will be ran for this call only rather than the one set on the client
-
put(url, query_string=None, post={}, headers={}, assert_=None)¶ make an HTTP PUT Request and return the response
Return type: webob.ResponseParameters: - url – the url for the request
- query_string – the querystring dict which will be
urlencoded for you
Parameters: - post – form post
- headers – extra headers fpr the request
- assert – a callback to be ran after the response is
recieved in the form of lambda: request, response: True . If present it will be ran for this call only rather than the one set on the client
-