Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/python/autogenerated_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Autogenerated client using openapi-python-client
https://github.com/openapi-generators/openapi-python-client?tab=readme-ov-file

### Generate the client
0. Install the client generator according to the [instructions](https://github.com/openapi-generators/openapi-python-client?tab=readme-ov-file#installation).
The client generator itself is a python package with a CLI interface. Note that this is a one-time step.
1. Activate the `venv` in which the client generator is installed:
```console
$ source .venv/bin/activate
```
2. The client generator will create a new python package with a boptest client. Make sure to adjust the paths.
```console
(my-venv)$ openapi-python-client generate --path ../project1-boptest/service/web/server/docs/openapi.yaml --output-path ../boptest-controller-project
```
**Note**: The `--path` option here will be replaced by the location of the deployed specs.

### Use the client
1. Change directory and activate the new `venv`
```console
(my-env)$ cd ../boptest-controller-project
(my-env)$ source .venv/bin/activate
(boptest-service-api-client)$
```
**Good to know**: In the autogenerated client, each operation like `POST /advance/{test_id}` becomes a *module* (in this case `post_advance_testid`).
Each module has a blocking function `module_name.sync(...)` for "normal" code, and a coroutine `module_name.async(...)` for asynchonous execution.
1. Create a python controller script that interacts with BOPTEST.
The file `usage.py` gives some examples to get you started.
Don't forget to add other python packages required for your controller (`numpy`, `pandas`, ...) to the `venv`.
1. Run the controller:
```console
(boptest-service-api-client)$ python ./usage.py
````

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""A client library for accessing BOPTEST Service API"""

from .client import AuthenticatedClient, Client

__all__ = (
"AuthenticatedClient",
"Client",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Contains methods for accessing the API"""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Contains endpoint functions for accessing the API"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
from http import HTTPStatus
from typing import Any
from urllib.parse import quote

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_forecast_points_testid_response_200 import GetForecastPointsTestidResponse200
from ...types import Response


def _get_kwargs(
testid: str,
) -> dict[str, Any]:
_kwargs: dict[str, Any] = {
"method": "get",
"url": "/forecast_points/{testid}".format(
testid=quote(str(testid), safe=""),
),
}

return _kwargs


def _parse_response(
*, client: AuthenticatedClient | Client, response: httpx.Response
) -> GetForecastPointsTestidResponse200 | None:
if response.status_code == 200:
response_200 = GetForecastPointsTestidResponse200.from_dict(response.json())

return response_200

if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(
*, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[GetForecastPointsTestidResponse200]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
testid: str,
*,
client: AuthenticatedClient | Client,
) -> Response[GetForecastPointsTestidResponse200]:
"""Get metadata for forecast points

Returns available forecast point names and metadata.

Args:
testid (str):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[GetForecastPointsTestidResponse200]
"""

kwargs = _get_kwargs(
testid=testid,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


def sync(
testid: str,
*,
client: AuthenticatedClient | Client,
) -> GetForecastPointsTestidResponse200 | None:
"""Get metadata for forecast points

Returns available forecast point names and metadata.

Args:
testid (str):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
GetForecastPointsTestidResponse200
"""

return sync_detailed(
testid=testid,
client=client,
).parsed


async def asyncio_detailed(
testid: str,
*,
client: AuthenticatedClient | Client,
) -> Response[GetForecastPointsTestidResponse200]:
"""Get metadata for forecast points

Returns available forecast point names and metadata.

Args:
testid (str):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[GetForecastPointsTestidResponse200]
"""

kwargs = _get_kwargs(
testid=testid,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)


async def asyncio(
testid: str,
*,
client: AuthenticatedClient | Client,
) -> GetForecastPointsTestidResponse200 | None:
"""Get metadata for forecast points

Returns available forecast point names and metadata.

Args:
testid (str):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
GetForecastPointsTestidResponse200
"""

return (
await asyncio_detailed(
testid=testid,
client=client,
)
).parsed
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
from http import HTTPStatus
from typing import Any
from urllib.parse import quote

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_inputs_testid_response_200 import GetInputsTestidResponse200
from ...types import Response


def _get_kwargs(
testid: str,
) -> dict[str, Any]:
_kwargs: dict[str, Any] = {
"method": "get",
"url": "/inputs/{testid}".format(
testid=quote(str(testid), safe=""),
),
}

return _kwargs


def _parse_response(
*, client: AuthenticatedClient | Client, response: httpx.Response
) -> GetInputsTestidResponse200 | None:
if response.status_code == 200:
response_200 = GetInputsTestidResponse200.from_dict(response.json())

return response_200

if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(
*, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[GetInputsTestidResponse200]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
testid: str,
*,
client: AuthenticatedClient | Client,
) -> Response[GetInputsTestidResponse200]:
"""Get metadata for inputs

Returns available control signal input point names (u) and metadata.

Args:
testid (str):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[GetInputsTestidResponse200]
"""

kwargs = _get_kwargs(
testid=testid,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


def sync(
testid: str,
*,
client: AuthenticatedClient | Client,
) -> GetInputsTestidResponse200 | None:
"""Get metadata for inputs

Returns available control signal input point names (u) and metadata.

Args:
testid (str):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
GetInputsTestidResponse200
"""

return sync_detailed(
testid=testid,
client=client,
).parsed


async def asyncio_detailed(
testid: str,
*,
client: AuthenticatedClient | Client,
) -> Response[GetInputsTestidResponse200]:
"""Get metadata for inputs

Returns available control signal input point names (u) and metadata.

Args:
testid (str):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[GetInputsTestidResponse200]
"""

kwargs = _get_kwargs(
testid=testid,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)


async def asyncio(
testid: str,
*,
client: AuthenticatedClient | Client,
) -> GetInputsTestidResponse200 | None:
"""Get metadata for inputs

Returns available control signal input point names (u) and metadata.

Args:
testid (str):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
GetInputsTestidResponse200
"""

return (
await asyncio_detailed(
testid=testid,
client=client,
)
).parsed
Loading
Loading