Module betterstack.uptime
This module will handle all low-level API calls, using a semi-modular approach. Whilst the API should always return its’ objects with the format below, the module does not care about what data is assigned to every object. This is generated at runtime, thus guaranteeing compliance with the API. This does mean that the interface to the user could change, so beware!
Standard data format
{
"data": {
"id": 1234,
"attributes": {
"somekey": "somevalue",
}
}
}
Submodules
Module contents
- class betterstack.uptime.RESTAPI(base_url: str, auth: BearerAuth)[source]
Bases:
object
Class that will handle all low-level API calls.
Inialize RESTAPI
- Parameters:
base_url (str) – The URL to be called, ending in a forward slash
auth (BearerAuth) – Authentication class to be used with requests
- __init__(base_url: str, auth: BearerAuth)[source]
Inialize RESTAPI
- Parameters:
base_url (str) – The URL to be called, ending in a forward slash
auth (BearerAuth) – Authentication class to be used with requests
- clean_params(parameters) Dict[str, any] [source]
Removes the trailing underscore in order to be able to use parameters like from
- Parameters:
parameters (dict) – A dict with all parameters to be cleaned
- Returns:
A dict with cleaned parameters
- Return type:
dict
- get(url: str, body: str = None, headers: Dict[str, any] = None, parameters: Dict[str, any] = None) Dict[str, any] [source]
Perform a GET request on a URL
- Parameters:
url (str) – URL to be accessed
body (str) – Body of the request
headers (dict) – Header to be sent
parameters (dict) – URL parameters to be sent
- Returns:
Response JSON
- Return type:
dict
- post(url: str, body: str = None, headers: Dict[str, any] = None, parameters: Dict[str, any] = None) Response [source]
Perform a POST request on a URL
- Parameters:
url (str) – URL to be accessed
body (str) – Body of the request
headers (dict) – Header to be sent
parameters (dict) – URL parameters to be sent
- Returns:
Response Object
- Return type:
requests.Response
- patch(url: str, body: str = None, headers: Dict[str, any] = None, parameters: Dict[str, any] = None) Response [source]
Perform a PATCH request on a URL
- Parameters:
url (str) – URL to be accessed
body (str) – Body of the request
headers (dict) – Header to be sent
parameters (dict) – URL parameters to be sent
- Returns:
Response Object
- Return type:
requests.Response
- delete(url: str, body: str = None, headers: Dict[str, any] = None, parameters: Dict[str, any] = None) Response [source]
Perform a DELETE request on a URL
- Parameters:
url (str) – URL to be accessed
body (str) – Body of the request
headers (dict) – Header to be sent
parameters (dict) – URL parameters to be sent
- Returns:
Response Object
- Return type:
requests.Response
- class betterstack.uptime.PaginatedAPI(base_url: str, auth: BearerAuth)[source]
Bases:
RESTAPI
Specically used with paginated API views
Inialize RESTAPI
- Parameters:
base_url (str) – The URL to be called, ending in a forward slash
auth (BearerAuth) – Authentication class to be used with requests
- return_monitors_for_page(url: str, body: str, headers: Dict[str, any] = None, parameters: Dict[str, any] = None, page: int = 1) List [source]
Used for multitreading page GETs
- Parameters:
url (str) – URL to be accessed
body (str) – Body of the request
headers (dict) – Header to be sent
parameters (dict) – URL parameters to be sent
- Returns:
List of entries
- Return type:
Generator
- get(url: str, body: str = None, headers: Dict[str, any] = None, parameters: Dict[str, any] = None) Generator [source]
Overrides the default behaviour, and checks for the pagination.next field. If it’s there: follow it (and it’s parameters) untill it’s empty.
- Parameters:
url (str) – URL to be accessed
body (str) – Body of the request
headers (dict) – Header to be sent
parameters (dict) – URL parameters to be sent
- Returns:
List of entries
- Return type:
Generator
- class betterstack.uptime.UptimeAPI(bearer_token: str)[source]
Bases:
PaginatedAPI
Initialize a new UptimeAPI
- Parameters:
bearer_token (str) – Bearer token to be used
- class betterstack.uptime.BaseAPIObject(api: RESTAPI, id: int = None, attributes: dict = None, **kwargs)[source]
Bases:
DynamicVariableMixin
Base class for all API objects. Uses dynamically assignable variables from api responses in order to store/filter/update data Always use the set_variable() function provided by DynamicVariableMixin in order to change or assign a variable. This ensures tracking of changed variables
Initializes the object with a corresponding API client, id and optional attributes If only the ID is provided, it will fetch data from the API in order to fill it’s attributes. If attributes are provided, just assign them
- Parameters:
api (RESTAPI) – API Instance
id (int) – Object ID to get (optional)
attributes (dict) – Attributes to assign to class. Use only attributes fetched from API here!
- __init__(api: RESTAPI, id: int = None, attributes: dict = None, **kwargs)[source]
Initializes the object with a corresponding API client, id and optional attributes If only the ID is provided, it will fetch data from the API in order to fill it’s attributes. If attributes are provided, just assign them
- Parameters:
api (RESTAPI) – API Instance
id (int) – Object ID to get (optional)
attributes (dict) – Attributes to assign to class. Use only attributes fetched from API here!
- generate_url() str [source]
Creates the URL in order to get this specific instance
- Returns:
Full instance URL
- Return type:
str
- classmethod generate_global_url() str [source]
Get the overview page for this object type
- Returns:
Overview page for object
- Return type:
str
- fetch_data(**kwargs)[source]
Gets all attributes from the API
- Parameters:
**kwargs –
A list of parameters to use as filters.
- save()[source]
Update all changed variables inside the class on the API. Updated list is provided by DynamicVariableMixin
- classmethod get_or_create(api: RESTAPI, **kwargs)[source]
Either, fetch an object using queryable attributes, or create a new object using said attributes.
- Parameters:
api (RESTAPI) – API instance
**kwargs –
Arguments to be used to filter results
- Returns:
Tuple with a Created boolean, and result
- Return type:
Tuple(bool, BaseAPIObject)
- Raises:
ValueError – if multiple matches get returned
- classmethod new(api: RESTAPI, **kwargs) BaseAPIObject [source]
Creates a new object using specified kwargs
- Parameters:
api (RESTAPI) – API instance
**kwargs –
Parameters to set when creating new instance
- Returns:
New instance using provided parameters
- Return type:
- classmethod filter(api: RESTAPI, **kwargs) Generator[BaseAPIObject] [source]
Uses url parameters to filter objects. Filter options must be in _allowed_query_parameters in order to work
- Parameters:
api (RESTAPI) – API instance
**kwargs –
Parameters to filter on
- Returns:
List of objects that match filter
- Return type:
List[BaseAPIObject]
- classmethod get_all_instances(api: PaginatedAPI) Generator[BaseAPIObject] [source]
Fetch all objects known to the API
- Parameters:
api (PaginatedAPI) – API instance
- Returns:
List of all instances
- Return type:
Generator[BaseAPIObject]
- classmethod filter_query_options(**kwargs)[source]
Check if query parameters are allowed for this specific object type
- Parameters:
**kwargs –
Query parameters to check
- Raises:
NotImplementedError – if the _allowed_query_parameters variable is not implemented
ValueError – if the class should not be filtered
ValueError – if the parameter is not in _allowed_query_parameters