gpf_entrepot_toolbelt.utils.simple_http_client module¶
Minimalist client HTTP but only based on pure standard lib
Author: Julien Moura (https://github.com/guts)
- class gpf_entrepot_toolbelt.utils.simple_http_client.EnhancedHTTPResponse(sock, debuglevel=0, method=None, url=None)¶
Bases :
HTTPResponse
An enhanced HTTP Response object with some helping attributes.
- content: bytes = b''¶
- class gpf_entrepot_toolbelt.utils.simple_http_client.SimpleHttpClient(default_headers: dict[str, str] | None = {'User-Agent': 'IGNGéoplateformePythonToolbelt/1.14.0'}, timeout: int | None = 30)¶
Bases :
object
- Simple HTTP client class that supports GET, POST, PUT, HEAD, OPTIONS, DELETE
requests, file downloads and some authentication flow, using only standard Python modules (mainly http.client).
Exemple
client = SimpleHttpClient( default_headers={"Content-Type": "application/json"} ) response = client.get("http://api.example.com/entity") print(response.status, response.reason) # 200, 'OK' print(response.content) # body as bytes # b'{"user"...}' local_path = client.download_file("http://example.com/somefile.txt", Path("localfile.txt"))
- __init__(default_headers: dict[str, str] | None = {'User-Agent': 'IGNGéoplateformePythonToolbelt/1.14.0'}, timeout: int | None = 30)¶
Initialize the SimpleHttpClient.
- Paramètres:
default_headers (dict[str,str] | None, optional) – Default headers to include in all requests. Defaults to { « User-Agent »: f »{__title_clean__}/{__version__} », }.
timeout (int | None, optional) – _description_. Defaults to 30.
- auth_set_basic(username: str, password: str) None ¶
Set the Basic Authentication credentials.
- Paramètres:
username – The username for Basic Authentication.
password – The password for Basic Authentication.
Exemple
client = SimpleHttpClient() client.auth_set_basic("myusername", "mypassword") response = client.get("http://example.com")
- auth_set_bearer_token(token: str) None ¶
Set the Bearer token for authentication.
- Paramètres:
token – The Bearer token.
Exemple
client = SimpleHttpClient() client.auth_set_bearer_token("my_access_token") response = client.get("http://example.com")
- delete(url: str, headers: dict[str, str] | None = None) EnhancedHTTPResponse ¶
Send a DELETE request.
- Paramètres:
url – The URL to send the request to.
headers – Additional headers to include in the request.
- Renvoie:
The HTTPResponse object.
- download_file(url: str, destination: str | Path, method: Literal['GET', 'POST'] = 'GET', chunk_size: int = 8192, data: dict | None = None, headers: dict[str, str] | None = None) Path | HTTPResponse ¶
Download a file from the given URL and save it to the specified destination.
- Paramètres:
url (str) – The URL of the file to download.
destination (str|Path) – The local path where the downloaded file will be saved.
chunk_size (int, optional) – The size of each download chunk in bytes. Defaults to 8192.
headers – Additional headers to include in the request.
- Renvoie:
destination path if download succeeded. HTTPResponse if it failed.
- Type renvoyé:
Path | HTTPResponse
- get(url: str, headers: dict[str, str] | None = None) EnhancedHTTPResponse ¶
Send a GET request.
- Paramètres:
url – The URL to send the request to.
headers – Additional headers to include in the request.
- Renvoie:
The HTTPResponse object.
Exemple
client = SimpleHttpClient() response = client.get("http://example.com") print(response.status, response.reason) print(response.content)
- head(url: str, headers: dict[str, str] | None = None) EnhancedHTTPResponse ¶
Send a HEAD request.
- Paramètres:
url – The URL to send the request to.
headers – Additional headers to include in the request.
- Renvoie:
The HTTPResponse object.
- options(url: str, headers: dict[str, str] | None = None) EnhancedHTTPResponse ¶
Send an OPTIONS request.
- Paramètres:
url – The URL to send the request to.
headers – Additional headers to include in the request.
- Renvoie:
The HTTPResponse object.
- post(url: str, data: dict[str, str | int] | None = None, headers: dict[str, str] | None = None) EnhancedHTTPResponse ¶
Send a POST request.
- Paramètres:
url – The URL to send the request to.
data – The data to include in the request body.
headers – Additional headers to include in the request.
- Renvoie:
The HTTPResponse object.
- put(url: str, data: dict[str, str | int] | None = None, headers: dict[str, str] | None = None) EnhancedHTTPResponse ¶
Send a PUT request.
- Paramètres:
url – The URL to send the request to.
data – The data to include in the request body.
headers – Additional headers to include in the request.
- Renvoie:
The HTTPResponse object.