I/O Module¶
API reference for the base I/O module.
io ¶
Module for the base class handling the access to the Database API.
Classes¶
API ¶
API(
api_root="https://owimetadatabase.azurewebsites.net/api/v1",
token=None,
uname=None,
password=None,
**kwargs,
)
Base API class handling user access information to the Database API.
Create an instance of the API class with required parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_root
|
str
|
Root URL of the API endpoint, the default working database url is provided. |
'https://owimetadatabase.azurewebsites.net/api/v1'
|
token
|
str
|
Token to access the API. |
None
|
uname
|
str
|
Username to access the API. |
None
|
password
|
str
|
Password to access the API. |
None
|
**kwargs
|
Any
|
Additional parameters to pass to the API. |
{}
|
Raises:
| Type | Description |
|---|---|
InvalidParameterError
|
If header format is invalid or if neither header, token, nor username and password are defined. |
Examples:
Source code in src/owi/metadatabase/io.py
Functions¶
__eq__ ¶
Compare two instances of the API class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another instance of the API class or a dictionary. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the instances are equal, False otherwise. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If comparison is not possible due to incompatible types. |
Examples:
>>> api_1 = API(api_root="https://example", token="test")
>>> api_2 = API(api_root="https://example", token="test")
>>> api_1 == api_2
True
Source code in src/owi/metadatabase/io.py
send_request ¶
Handle sending appropriate request.
Handles sending appropriate request according to the type of authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
url_params
|
Mapping
|
Parameters to send with the request to the database. |
required |
Returns:
| Type | Description |
|---|---|
Response
|
An instance of the Response object. |
Raises:
| Type | Description |
|---|---|
InvalidParameterError
|
If neither header nor username and password are defined. |
Examples:
>>> from types import SimpleNamespace
>>> from unittest import mock
>>> response = SimpleNamespace(status_code=200, reason="OK")
>>> with mock.patch("requests.get", return_value=response):
... api = API(api_root="https://example", token="test")
... resp = api.send_request("/projects", {})
>>> resp is response
True
Source code in src/owi/metadatabase/io.py
check_request_health
staticmethod
¶
Check status code of the response and provide details.
Checks status code of the response to request and provides details if unexpected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resp
|
Response
|
Instance of the Response object. |
required |
Raises:
| Type | Description |
|---|---|
APIConnectionError
|
If response status code is not 200. |
Examples:
>>> from types import SimpleNamespace
>>> ok = SimpleNamespace(status_code=200, reason="OK")
>>> API.check_request_health(ok)
Source code in src/owi/metadatabase/io.py
output_to_df
staticmethod
¶
Transform output to Pandas dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
Raw output of the sent request. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Pandas dataframe of the data from the output. |
Raises:
| Type | Description |
|---|---|
DataProcessingError
|
If failed to decode JSON from API response. |
Examples:
>>> from types import SimpleNamespace
>>> resp = SimpleNamespace(text='[{"a": 1}]')
>>> int(API.output_to_df(resp)["a"].iloc[0])
1
Source code in src/owi/metadatabase/io.py
postprocess_data
staticmethod
¶
Process dataframe information to extract additional data.
Processes dataframe information to extract the necessary additional data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe of the output data. |
required |
output_type
|
str
|
Expected type (amount) of the data extracted. |
required |
Returns:
| Type | Description |
|---|---|
PostprocessData
|
Dictionary of the additional data extracted. |
Raises:
| Type | Description |
|---|---|
InvalidParameterError
|
If more than one record was returned for 'single' output type, or if output type is not 'single' or 'list'. |
Examples:
Source code in src/owi/metadatabase/io.py
validate_data
staticmethod
¶
Validate the data extracted from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe of the output data. |
required |
data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe with corrected data. |
Examples:
Source code in src/owi/metadatabase/io.py
process_data ¶
Process output data according to specified request parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_data_type
|
str
|
Type of the data we want to request (according to database model). |
required |
url_params
|
Mapping
|
Parameters to send with the request to the database. |
required |
output_type
|
str
|
Expected type (amount) of the data extracted. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple of dataframe with the requested data and additional data from postprocessing. |
Examples:
>>> from types import SimpleNamespace
>>> from unittest import mock
>>> response = SimpleNamespace(text="[]", status_code=200, reason="OK")
>>> api = API(api_root="https://example", token="test")
>>> with mock.patch.object(API, "send_request", return_value=response):
... df, info = api.process_data("projects", {}, "list")
>>> df.empty
True
>>> info["existance"]
False