landtransportsg¶
Module contents¶
Package init.
landtransportsg.landtransportsg¶
Client mixin for interacting with all of the API endpoints.
- class landtransportsg.landtransportsg.LandTransportSg(account_key: str, cache_backend: str | BaseCache = 'sqlite')¶
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()).Account key. An account key is required to use the LTA DataMall API. Request for one from https://www.mytransport.sg/content/mytransport/home/dataMall/request-for-api.html.
User-agent header.
- Parameters:
cache_backend (str | BaseCache) – Cache backend name or instance to use. Refer to https://requests-cache.readthedocs.io/en/stable/user_guide/backends.html for more information and allowed values. Defaults to “sqlite”.
account_key (str) – The LTA DataMall-assigned Account key.
- build_params(params_expected_type: Any, original_params: Any, default_params: dict[str, Any] | None = None, key_map: dict[str, str] | None = None, remove_none_values: bool = True) dict¶
Build the list of parameters that are compatible for use with the endpoint URLs, e.g. camelCase parameter names instead of Python’s snake_case, datetime objects to strings.
- Parameters:
params_expected_type (Any) – The expected type of
original_params. Should be one of the importable types from the client’stypes_args.original_params (Any but should really be dict) – The set of parameters to use for building.
default_params (dict[str, Any] or None) – The set of parameters’ default values. Should be of the same type as what is specified in
params_expected_type. Defaults to None.key_map (dict[str, str] or None) – Mapping of keys used in
params_expected_typesto keys expected by the endpoint. Defaults to None.remove_none_values (bool) – If True, then parameters with None values are removed from the returned parameters. Defaults to True.
- Returns:
The set of parameters that can be used with the API endpoints.
- Return type:
- sanitise_data(value: Any, iterate: bool = True, ignore_keys: list[str] | None = None, key_path: str = '') Any¶
Convert the following:
If
iterateis True and value is adictorlist: sanitise the value’s contents.Blank string: convert to None.
String that is like date or datetime: convert to
datetime.dateordatetime.datetimeobject respectively.String that is number-like: convert to
intorfloatappropriately.Finally: Leave the value as-is.
- Parameters:
value (Any) – Value to sanitise.
iterate (bool) – If True, then
listanddictobjects are sanitised recursively. Defaults to True.ignore_keys (list[str] or None) – List of dict keys to ignore when sanitising, if value is a
dict. Defaults to None.key_path (str) – Current path of key in the dict. Defaults to blank string.
- Returns:
The sanitised value.
- Return type:
Any
- send_request(url: str, params: dict | None = None, cache_duration: int = 0, sanitise: bool = True, sanitise_ignore_keys: list[str] | None = None) Any¶
Send a request to an endpoint and return its response.
Normally, this method does not need to be called directly. However, if LTA Datamall 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) – List of parameters 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 {}.
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_ignore_keys (list[str] | None) – List of keys to ignore in the response value during sanitising when that response value is a
dict. Defaults to [].
- Raises:
HTTPError – Error occurred during the request process.
- Returns:
Results from the response.
- Return type:
Any
- send_download_request(url: str, params: dict | None = None, cache_duration: int = 0) str¶
Send a request to an endpoint that expects a response with a download link.
Normally, this method does not need to be called directly. However, if LTA Datamall 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) – List of parameters 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.
- Raises:
HTTPError – Error occurred during the request process.
- Returns:
Link for downloading the requested file.
- Return type:
landtransportsg.timezone¶
Standardise all datetime-related timezones to SGT (Singapore Time).
- landtransportsg.timezone.datetime_from_string(val: str) datetime | date | time¶
Convert a string into a datetime in SGT timezone.
Strings are parsed according to the following formats, in order: 1. %Y-%m-%dT%H:%M:%S.%f%z 2. %Y-%m-%dT%H:%M:%S%z 3. %Y-%m-%dT%H:%M:%S.%f 4. %Y-%m-%dT%H:%M:%S 5. %Y%m%dT%H:%M:%S.%f%z 6. %Y%m%dT%H:%M:%S%z 7. %Y%m%dT%H:%M:%S.%f 8. %Y%m%dT%H:%M:%S 9. %Y-%m-%d %H:%M:%S.%f%z 10. %Y-%m-%d %H:%M:%S%z 11. %Y-%m-%d %H:%M:%S.%f 12. %Y-%m-%d %H:%M:%S 13. %Y-%m-%d 14. %Y%m%d 15. %H:%M:%S.%f%z 16. %H:%M:%S%z 17. %H:%M:%S.%f 18. %H:%M%z 19. %H:%M 20. %H%M
- Parameters:
val (str) – String to convert to a datetime.
- Raises:
ValueError – val is not a recognised datetime string.
- Returns:
The value as a datetime or date, if there is no time, or time, if there is no date.
- Return type:
datetime | date | time
- landtransportsg.timezone.date_is_within_last_three_months(check_date: date, cutoff_day: int = 15) bool¶
Return whether the specified date is within the last 3 months of today.
- Parameters:
check_date (date) – Date to check if it is within the last 3 months of today.
cutoff_day (int) – The day of the month when a “new month” begins. Example: if cutoff_day is 15, then if today is between 1st and 14th September, then last 3 months is between May-July, else if today is between 15th and 30th September, then last 3 months is between June-August. Defaults to 15, i.e. the new month starts on the 15th day.
- Raises:
ValueError – cutoff day is not a valid calendar day.
- Returns:
True if the specified date is within the last 3 months of today.
- Return type:
landtransportsg.exceptions¶
Exceptions that could occur when interacting with any API endpoint.
- exception landtransportsg.exceptions.APIError(message: str, data: Any | None = None, errors: Any | None = None)¶
Bases:
ExceptionError when the API returns an error.
- Parameters:
message (str) – The general error message to display when the error is raised.
data (Any or None) – Data response obtained by the calling method. Defaults to None.
errors (Any or None) – Other messages that were part of the raised error. Defaults to None.
- Return type:
None