dicogis.utils.http package

HTTP clients: requests or the network stack of QGIS, behind a common contract.

The backend is picked from the DICOGIS_HTTP_BACKEND environment variable, on the same model as DICOGIS_QT_API:

  • auto (default): the network stack of QGIS when running inside QGIS, i.e. a QgsApplication is running, requests otherwise.

  • requests: always requests.

  • qgis: always the network stack of QGIS.

class dicogis.utils.http.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.HttpError

Bases: Exception

Base class of the errors raised by the HTTP clients.

class dicogis.utils.http.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.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.HttpTransportError

Bases: HttpError

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

dicogis.utils.http.get_http_client(backend=None, *, authcfg=None, feedback=None)

Build the HTTP client of the running environment.

Parameters:
  • backend (str | None) – see resolve_http_backend()

  • authcfg (str | None) – identifier of a configuration of the QGIS authentication manager

  • feedback (QgsFeedback | None) – interrupts the running request when canceled (QGIS backend)

Raises:

ValueError – if the backend is unknown, or authcfg is given while QGIS isn’t used

Return type:

HttpClient

Returns:

HTTP client

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

dicogis.utils.http.resolve_http_backend(requested=None)

Determine which HTTP backend to use.

Parameters:

requested (str | None) – one of HTTP_BACKENDS, case-insensitive. Defaults to the value of the DICOGIS_HTTP_BACKEND environment variable, or auto if unset.

Raises:

ValueError – if the requested backend is unknown

Returns:

requests or qgis

Return type:

the backend to use

Submodules