datagovsg

Module contents

datagovsg.Ckan

alias of Client

datagovsg.Economy

alias of Client

datagovsg.Environment

alias of Client

datagovsg.Housing

alias of Client

datagovsg.Transport

alias of Client

datagovsg.datagovsg

Client mixin for interacting with all of the API endpoints.

class datagovsg.datagovsg.DataGovSg

Bases: object

Client 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:

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.

Parameters:
  • original_params (Any but should really be dict) – The set of parameters to use for building.

  • default_params (dict or None) – The set of parameters’ default values. Defaults to None.

Returns:

The set of parameters that can be used with the API endpoints.

Return type:

dict

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.date or datetime.datetime object respectively.

Parameters:
  • value (Any) – Value to sanitise.

  • iterate (bool) – If True, then list and dict objects are sanitised recursively. Defaults to True.

  • sanitise_numbers (bool) – If True, then strings that are like numbers are converted to int or float appropriately. Defaults to False.

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 sanitise argument 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: Exception

Error 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