dicogis.utils.http.client module

Contract of the HTTP clients: what the publication code expects from the network.

Depends on neither requests nor Qt, in the same spirit as dicogis.utils.progress.ProgressReporter: the backends implement it, the publication code only knows about it.

class dicogis.utils.http.client.HttpClient(*args, **kwargs)

Bases: Protocol

What the publication code needs to reach a catalog.

Implementations are stateless, cookies included: what a request sends is what its caller gives. Each backend otherwise has its own cookie jar, which overrides the Cookie header once it holds a cookie for the host.

__init__(*args, **kwargs)
request(method, url, *, params=None, headers=None, data=None, timeout=None)

Send a request and wait for its response, redirections being followed.

Parameters:
Raises:

HttpTransportError – if no response was received

Return type:

HttpResponse

Returns:

response, whatever its status code

exception dicogis.utils.http.client.HttpError

Bases: Exception

Base class of the errors raised by the HTTP clients.

class dicogis.utils.http.client.HttpResponse(status_code, headers=<factory>, cookies=<factory>, content=b'', url='')

Bases: object

An HTTP response, whatever the backend which received it.

status_code

HTTP status code

headers

headers, names in lowercase

cookies

cookies set by this very response, by name

content

body

url

final URL of the request

__init__(status_code, headers=<factory>, cookies=<factory>, content=b'', url='')
content: bytes = b''
cookies: dict[str, str]
headers: dict[str, str]
json()

Decode the body as JSON.

Return type:

object

Returns:

decoded body

raise_for_status()

Raise an error if the response tells one.

Raises:

HttpStatusError – if the status code is 400 or above

Return type:

None

status_code: int
property text: str

Body as text.

Returns:

decoded body, undecodable bytes being replaced

url: str = ''
exception dicogis.utils.http.client.HttpStatusError(response)

Bases: HttpError

An HTTP response was received, telling an error.

response

the response at stake

__init__(response)

Build the error from its response.

Parameters:

response (HttpResponse) – the response at stake

exception dicogis.utils.http.client.HttpTransportError

Bases: HttpError

No HTTP response was received: connection refused, timeout, canceled…

Read the cookies of Set-Cookie headers merged on several lines, as Qt does.

Parameters:

header_value (str) – value of the Set-Cookie header

Return type:

dict[str, str]

Returns:

cookies values, by name, without their attributes