corehttp.rest package

class corehttp.rest.AsyncHttpResponse[source]

Abstract base class for Async HTTP responses.

Use this abstract base class to create your own transport responses.

Responses implementing this ABC are returned from your async client’s send_request method if you pass in an HttpRequest

>>> from corehttp.rest import HttpRequest
>>> request = HttpRequest('GET', 'http://www.example.com')
<HttpRequest [GET], url: 'http://www.example.com'>
>>> response = await client.send_request(request)
<AsyncHttpResponse: 200 OK>
abstract async close() None[source]
abstract async iter_bytes(**kwargs: Any) AsyncIterator[bytes][source]

Asynchronously iterates over the response’s bytes. Will decompress in the process.

Returns:

An async iterator of bytes from the response

Return type:

AsyncIterator[bytes]

abstract async iter_raw(**kwargs: Any) AsyncIterator[bytes][source]

Asynchronously iterates over the response’s bytes. Will not decompress in the process.

Returns:

An async iterator of bytes from the response

Return type:

AsyncIterator[bytes]

abstract json() Any

Returns the whole body as a json object.

Returns:

The JSON deserialized response body

Return type:

any

Raises:

json.decoder.JSONDecodeError or ValueError (in python 2.7) if object is not JSON decodable

abstract raise_for_status() None

Raises an HttpResponseError if the response has an error status code.

If response is good, does nothing.

Raises:

abstract async read() bytes[source]

Read the response’s bytes into memory.

Returns:

The response’s bytes

Return type:

bytes

abstract text(encoding: str | None = None) str

Returns the response body as a string.

Parameters:

encoding (optional[str]) – The encoding you want to decode the text with. Can also be set independently through our encoding property

Returns:

The response’s content decoded as a string.

Return type:

str

abstract property content: bytes

Return the response’s content in bytes.

Return type:

bytes

Returns:

The response’s content in bytes.

abstract property content_type: str | None

The content type of the response.

Return type:

str

Returns:

The content type of the response.

abstract property encoding: str | None

Returns the response encoding.

Returns:

The response encoding. We either return the encoding set by the user, or try extracting the encoding from the response’s content type. If all fails, we return None.

Return type:

optional[str]

abstract property headers: MutableMapping[str, str]

The response headers. Must be case-insensitive.

Return type:

MutableMapping[str, str]

Returns:

The response headers. Must be case-insensitive.

abstract property is_closed: bool

Whether the network connection has been closed yet.

Return type:

bool

Returns:

Whether the network connection has been closed yet.

abstract property is_stream_consumed: bool

Whether the stream has been consumed.

Return type:

bool

Returns:

Whether the stream has been consumed.

abstract property reason: str

The reason phrase for this response.

Return type:

str

Returns:

The reason phrase for this response.

abstract property request: HttpRequest

The request that resulted in this response.

Return type:

HttpRequest

Returns:

The request that resulted in this response.

abstract property status_code: int

The status code of this response.

Return type:

int

Returns:

The status code of this response.

abstract property url: str

The URL that resulted in this response.

Return type:

str

Returns:

The URL that resulted in this response.

class corehttp.rest.HttpRequest(method: str, url: str, *, params: Mapping[str, str | int | float | bool | None | Sequence[str | int | float | bool | None]] | None = None, headers: MutableMapping[str, str] | None = None, json: Any = None, content: str | bytes | Iterable[bytes] | AsyncIterable[bytes] | None = None, data: Dict[str, Any] | None = None, files: Mapping[str, str | bytes | IO[str] | IO[bytes] | Tuple[str | None, str | bytes | IO[str] | IO[bytes]] | Tuple[str | None, str | bytes | IO[str] | IO[bytes], str | None]] | Sequence[Tuple[str, str | bytes | IO[str] | IO[bytes] | Tuple[str | None, str | bytes | IO[str] | IO[bytes]] | Tuple[str | None, str | bytes | IO[str] | IO[bytes], str | None]]] | None = None, **kwargs: Any)[source]

An HTTP request.

It should be passed to your client’s send_request method.

>>> from corehttp.rest import HttpRequest
>>> request = HttpRequest('GET', 'http://www.example.com')
<HttpRequest [GET], url: 'http://www.example.com'>
>>> response = client.send_request(request)
<HttpResponse: 200 OK>
Parameters:
  • method (str) – HTTP method (GET, HEAD, etc.)

  • url (str) – The url for your request

Keyword Arguments:
  • params (mapping) – Query parameters to be mapped into your URL. Your input should be a mapping of query name to query value(s).

  • headers (mapping) – HTTP headers you want in your request. Your input should be a mapping of header name to header value.

  • json (any) – A JSON serializable object. We handle JSON-serialization for your object, so use this for more complicated data structures than data.

  • content (str or bytes or iterable[bytes] or asynciterable[bytes]) – Content you want in your request body. Think of it as the kwarg you should input if your data doesn’t fit into json, data, or files. Accepts a bytes type, or a generator that yields bytes.

  • data (dict) – Form data you want in your request body. Use for form-encoded data, i.e. HTML forms.

  • files (mapping) – Files you want to in your request body. Use for uploading files with multipart encoding. Your input should be a mapping of file name to file content. Use the data kwarg in addition if you want to include non-file data files as part of your request.

Variables:
  • url (str) – The URL this request is against.

  • method (str) – The method type of this request.

  • headers (mapping) – The HTTP headers you passed in to your request

  • content (any) – The content passed in for the request

property content: Any

Get’s the request’s content

Returns:

The request’s content

Return type:

any

class corehttp.rest.HttpResponse[source]

Abstract base class for HTTP responses.

Use this abstract base class to create your own transport responses.

Responses implementing this ABC are returned from your client’s send_request method if you pass in an HttpRequest

>>> from corehttp.rest import HttpRequest
>>> request = HttpRequest('GET', 'http://www.example.com')
<HttpRequest [GET], url: 'http://www.example.com'>
>>> response = client.send_request(request)
<HttpResponse: 200 OK>
abstract close() None[source]
abstract iter_bytes(**kwargs: Any) Iterator[bytes][source]

Iterates over the response’s bytes. Will decompress in the process.

Returns:

An iterator of bytes from the response

Return type:

Iterator[str]

abstract iter_raw(**kwargs: Any) Iterator[bytes][source]

Iterates over the response’s bytes. Will not decompress in the process.

Returns:

An iterator of bytes from the response

Return type:

Iterator[str]

abstract json() Any

Returns the whole body as a json object.

Returns:

The JSON deserialized response body

Return type:

any

Raises:

json.decoder.JSONDecodeError or ValueError (in python 2.7) if object is not JSON decodable

abstract raise_for_status() None

Raises an HttpResponseError if the response has an error status code.

If response is good, does nothing.

Raises:

abstract read() bytes[source]

Read the response’s bytes.

Returns:

The read in bytes

Return type:

bytes

abstract text(encoding: str | None = None) str

Returns the response body as a string.

Parameters:

encoding (optional[str]) – The encoding you want to decode the text with. Can also be set independently through our encoding property

Returns:

The response’s content decoded as a string.

Return type:

str

abstract property content: bytes

Return the response’s content in bytes.

Return type:

bytes

Returns:

The response’s content in bytes.

abstract property content_type: str | None

The content type of the response.

Return type:

str

Returns:

The content type of the response.

abstract property encoding: str | None

Returns the response encoding.

Returns:

The response encoding. We either return the encoding set by the user, or try extracting the encoding from the response’s content type. If all fails, we return None.

Return type:

optional[str]

abstract property headers: MutableMapping[str, str]

The response headers. Must be case-insensitive.

Return type:

MutableMapping[str, str]

Returns:

The response headers. Must be case-insensitive.

abstract property is_closed: bool

Whether the network connection has been closed yet.

Return type:

bool

Returns:

Whether the network connection has been closed yet.

abstract property is_stream_consumed: bool

Whether the stream has been consumed.

Return type:

bool

Returns:

Whether the stream has been consumed.

abstract property reason: str

The reason phrase for this response.

Return type:

str

Returns:

The reason phrase for this response.

abstract property request: HttpRequest

The request that resulted in this response.

Return type:

HttpRequest

Returns:

The request that resulted in this response.

abstract property status_code: int

The status code of this response.

Return type:

int

Returns:

The status code of this response.

abstract property url: str

The URL that resulted in this response.

Return type:

str

Returns:

The URL that resulted in this response.