Skip to content

Latest commit

 

History

History
185 lines (119 loc) · 4.83 KB

api.rst

File metadata and controls

185 lines (119 loc) · 4.83 KB

API Reference

This page lists all of the interfaces exposed by the treq package.

Making Requests

The :pytreq module provides several convenience functions for making requests. These functions all create a default :pytreq.client.HTTPClient instance and pass their arguments to the appropriate :py~treq.client.HTTPClient method.

treq

request

get

head

post

put

patch

delete

Accessing Content

collect

content

text_content

json_content

The HTTP Client

treq.client

treq.client.HTTPClient has methods that match the signatures of the convenience request functions in the treq module.

HTTPClient(agent, cookiejar=None, data_to_body_producer=IBodyProducer)

request

get

head

post

put

patch

delete

Augmented Response Objects

treq.request, treq.get, etc. return an object which provides twisted.web.iweb.IResponse, plus a few additional convenience methods:

treq.response

collect

content

json

text

history

cookies

Inherited from twisted.web.iweb.IResponse:

ivar version

See IResponse.version <twisted.web.iweb.IResponse.version>

ivar code

See IResponse.code <twisted.web.iweb.IResponse.code>

ivar phrase

See IResponse.phrase <twisted.web.iweb.IResponse.phrase>

ivar headers

See IResponse.headers <twisted.web.iweb.IResponse.headers>

ivar length

See IResponse.length <twisted.web.iweb.IResponse.length>

ivar request

See IResponse.request <twisted.web.iweb.IResponse.request>

ivar previousResponse

See IResponse.previousResponse <twisted.web.iweb.IResponse.previousResponse>

deliverBody(protocol)

See IResponse.deliverBody() <twisted.web.iweb.IResponse.deliverBody>

setPreviousResponse(response)

See IResponse.setPreviousResponse() <twisted.web.iweb.IResponse.setPreviousResponse>

Authentication

treq.auth

add_auth

add_basic_auth

UnknownAuthConfig

Test Helpers

treq.testing

The treq.testing module contains tools for in-memory testing of HTTP clients and servers.

StubTreq Objects

StubTreq implements the same interface as the treq module or the ~treq.client.HTTPClient class, with the limitation that it does not support the files argument.

flush()

Flush all data between pending client/server pairs.

This is only necessary if a Resource under test returns NOT_DONE_YET from its render method, making a response asynchronous. In that case, after each write from the server, flush() must be called so the client can see it.

cleanSessions()

Clean up sessions to prevent leaving behind a dirty reactor.

If you are using StubTreq with twisted.web.server.Session objects, you most likely have to call this method once you are done, for example during the tearDown of a unittest TestCase.

As the methods on treq.client.HTTPClient:

request

See treq.request().

get

See treq.get().

head

See treq.head().

post

See treq.post().

put

See treq.put().

patch

See treq.patch().

delete

See treq.delete().

RequestTraversalAgent Objects

treq.testing.RequestTraversalAgent

RequestSequence Objects

treq.testing.RequestSequence

StringStubbingResource Objects

treq.testing.StringStubbingResource

HasHeaders Objects

treq.testing.HasHeaders

MultiPartProducer Objects

treq.multipart.MultiPartProducer is used internally when making requests which involve files.

treq.multipart