datagovsg¶
Module contents¶
datagovsg.datagovsg¶
Client mixin for interacting with all of the API endpoints.
- class datagovsg.datagovsg.DataGovSg¶
Bases:
objectClient mixin for other API Clients.
Normally, it does not need to be created by applications. But applications may use the public methods provided here.
The constructor sets the following:
Connection retries using exponential backoff. (Reference: https://stackoverflow.com/a/35504626.)
Cache (cache duration/expiry is set in
send_request()).User-agent header.
- build_params(original_params: Any, default_params: dict | None = None) dict¶
Build the list of parameters that are compatible for use with the endpoint URLs, e.g. datetime objects to strings.
- sanitise_data(value: Any, iterate: bool = True, sanitise_numbers: bool = False) Any¶
Convert the following:
String that is like date or datetime: convert to
datetime.dateordatetime.datetimeobject respectively.
- Parameters:
- Returns:
The sanitised value.
- Return type:
Any
- send_request(url: str, params: dict | str | None = None, cache_duration: int | None = 0, sanitise: bool = True, sanitise_numbers: bool = False) Any¶
Send a request to an endpoint.
Normally, this method does not need to be called directly. However, if SingStat were to change their API specification but this package has not yet been updated to support that change, then applications may use this method to call the changed endpoints.
- Parameters:
url (Url) – The endpoint URL to send the request to.
params (dict | str) – List of parameters or parameter string to be passed to the endpoint URL. Parameter names must match the names required by the endpoints, particularly with typecase (e.g. camelCase). Defaults to None.
cache_duration (int) – Number of seconds before the cache expires. Defaults to 0, i.e. do not cache.
sanitise (bool) – If true, then the response’s values are sanitised using the
sanitise_data()method. Defaults to True.sanitise_numbers (bool) – If True, then sanitises number-like strings. Applicable only when
sanitiseargument is True. Defaults to False.
- Raises:
HTTPError – Error occurred during the request process.
- Returns:
Response JSON content of the request.
- Return type:
Any
datagovsg.exceptions¶
Exceptions that could occur when interacting with any API.
- exception datagovsg.exceptions.APIError(message: str, data: Any | None = None, errors: Any | None = None)¶
Bases:
ExceptionError when the API returns an error.
- Parameters:
message (Any) – The general error message to display when the error is raised.
data (Any) – Data response obtained by the calling method. Defaults to None.
errors (Any | None) – Other messages that were part of the raised error. Defaults to None.
- Return type:
None