Skip to content

Commit e4c1adb

Browse files
author
aman
committed
Generate api client from API 26.1-beta-3
1 parent 1bd36b0 commit e4c1adb

292 files changed

Lines changed: 35350 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ description = "Python SDK for the BodyLoop API for seamless ecosystem integratio
55
readme = "README.md"
66
license = { file = "LICENSE" }
77
requires-python = ">=3.11"
8-
dependencies = []
8+
dependencies = [
9+
"attrs>=25.4.0",
10+
"httpx>=0.28.1",
11+
"python-dateutil>=2.9.0.post0",
12+
]
913

1014
[project.urls]
1115
Homepage = "https://github.com/BodyLoop/bodyloop-pypi"

src/bodyloop_sdk/client/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# client
2+
A client library for accessing BodyLoop API
3+
4+
## Usage
5+
First, create a client:
6+
7+
```python
8+
from client import Client
9+
10+
client = Client(base_url="https://api.example.com")
11+
```
12+
13+
If the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead:
14+
15+
```python
16+
from client import AuthenticatedClient
17+
18+
client = AuthenticatedClient(base_url="https://api.example.com", token="SuperSecretToken")
19+
```
20+
21+
Now call your endpoint and use your models:
22+
23+
```python
24+
from client.models import MyDataModel
25+
from client.api.my_tag import get_my_data_model
26+
from client.types import Response
27+
28+
with client as client:
29+
my_data: MyDataModel = get_my_data_model.sync(client=client)
30+
# or if you need more info (e.g. status_code)
31+
response: Response[MyDataModel] = get_my_data_model.sync_detailed(client=client)
32+
```
33+
34+
Or do the same thing with an async version:
35+
36+
```python
37+
from client.models import MyDataModel
38+
from client.api.my_tag import get_my_data_model
39+
from client.types import Response
40+
41+
async with client as client:
42+
my_data: MyDataModel = await get_my_data_model.asyncio(client=client)
43+
response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client)
44+
```
45+
46+
By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.
47+
48+
```python
49+
client = AuthenticatedClient(
50+
base_url="https://internal_api.example.com",
51+
token="SuperSecretToken",
52+
verify_ssl="/path/to/certificate_bundle.pem",
53+
)
54+
```
55+
56+
You can also disable certificate validation altogether, but beware that **this is a security risk**.
57+
58+
```python
59+
client = AuthenticatedClient(
60+
base_url="https://internal_api.example.com",
61+
token="SuperSecretToken",
62+
verify_ssl=False
63+
)
64+
```
65+
66+
Things to know:
67+
1. Every path/method combo becomes a Python module with four functions:
68+
1. `sync`: Blocking request that returns parsed data (if successful) or `None`
69+
1. `sync_detailed`: Blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful.
70+
1. `asyncio`: Like `sync` but async instead of blocking
71+
1. `asyncio_detailed`: Like `sync_detailed` but async instead of blocking
72+
73+
1. All path/query params, and bodies become method arguments.
74+
1. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above)
75+
1. Any endpoint which did not have a tag will be in `client.api.default`
76+
77+
## Advanced customizations
78+
79+
There are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying `httpx.Client` or `httpx.AsyncClient` (depending on your use-case):
80+
81+
```python
82+
from client import Client
83+
84+
def log_request(request):
85+
print(f"Request event hook: {request.method} {request.url} - Waiting for response")
86+
87+
def log_response(response):
88+
request = response.request
89+
print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")
90+
91+
client = Client(
92+
base_url="https://api.example.com",
93+
httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
94+
)
95+
96+
# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()
97+
```
98+
99+
You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):
100+
101+
```python
102+
import httpx
103+
from client import Client
104+
105+
client = Client(
106+
base_url="https://api.example.com",
107+
)
108+
# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.
109+
client.set_httpx_client(httpx.Client(base_url="https://api.example.com", proxies="http://localhost:8030"))
110+
```
111+
112+
## Building / publishing this package
113+
This project uses [Poetry](https://python-poetry.org/) to manage dependencies and packaging. Here are the basics:
114+
1. Update the metadata in pyproject.toml (e.g. authors, version)
115+
1. If you're using a private repository, configure it with Poetry
116+
1. `poetry config repositories.<your-repository-name> <url-to-your-repository>`
117+
1. `poetry config http-basic.<your-repository-name> <username> <password>`
118+
1. Publish the client with `poetry publish --build -r <your-repository-name>` or, if for public PyPI, just `poetry publish --build`
119+
120+
If you want to install this client into another project without publishing it (e.g. for development) then:
121+
1. If that project **is using Poetry**, you can simply do `poetry add <path-to-this-client>` from that project
122+
1. If that project is not using Poetry:
123+
1. Build a wheel with `poetry build -f wheel`
124+
1. Install that wheel from the other project `pip install <path-to-wheel>`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""A client library for accessing BodyLoop API"""
2+
3+
from .client import AuthenticatedClient, Client
4+
5+
__all__ = (
6+
"AuthenticatedClient",
7+
"Client",
8+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Contains methods for accessing the API"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Contains endpoint functions for accessing the API"""
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
from http import HTTPStatus
2+
from typing import Any
3+
from urllib.parse import quote
4+
5+
import httpx
6+
7+
from ... import errors
8+
from ...client import AuthenticatedClient, Client
9+
from ...models.body_apitoken_api_v2_authentification_users_username_api_token_post import (
10+
BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost,
11+
)
12+
from ...models.http_validation_error import HTTPValidationError
13+
from ...models.token import Token
14+
from ...types import UNSET, Response, Unset
15+
16+
17+
def _get_kwargs(
18+
username: Any,
19+
*,
20+
body: BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset = UNSET,
21+
) -> dict[str, Any]:
22+
headers: dict[str, Any] = {}
23+
24+
_kwargs: dict[str, Any] = {
25+
"method": "post",
26+
"url": "/api/v2/authentification/users/{username}/api_token".format(
27+
username=quote(str(username), safe=""),
28+
),
29+
}
30+
31+
if not isinstance(body, Unset):
32+
_kwargs["json"] = body.to_dict()
33+
34+
headers["Content-Type"] = "application/json"
35+
36+
_kwargs["headers"] = headers
37+
return _kwargs
38+
39+
40+
def _parse_response(
41+
*, client: AuthenticatedClient | Client, response: httpx.Response
42+
) -> HTTPValidationError | Token | None:
43+
if response.status_code == 200:
44+
response_200 = Token.from_dict(response.json())
45+
46+
return response_200
47+
48+
if response.status_code == 422:
49+
response_422 = HTTPValidationError.from_dict(response.json())
50+
51+
return response_422
52+
53+
if client.raise_on_unexpected_status:
54+
raise errors.UnexpectedStatus(response.status_code, response.content)
55+
else:
56+
return None
57+
58+
59+
def _build_response(
60+
*, client: AuthenticatedClient | Client, response: httpx.Response
61+
) -> Response[HTTPValidationError | Token]:
62+
return Response(
63+
status_code=HTTPStatus(response.status_code),
64+
content=response.content,
65+
headers=response.headers,
66+
parsed=_parse_response(client=client, response=response),
67+
)
68+
69+
70+
def sync_detailed(
71+
username: Any,
72+
*,
73+
client: AuthenticatedClient,
74+
body: BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset = UNSET,
75+
) -> Response[HTTPValidationError | Token]:
76+
"""Apitoken
77+
78+
Request API token
79+
80+
Args:
81+
username (Any):
82+
body (BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset):
83+
84+
Raises:
85+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
86+
httpx.TimeoutException: If the request takes longer than Client.timeout.
87+
88+
Returns:
89+
Response[HTTPValidationError | Token]
90+
"""
91+
92+
kwargs = _get_kwargs(
93+
username=username,
94+
body=body,
95+
)
96+
97+
response = client.get_httpx_client().request(
98+
**kwargs,
99+
)
100+
101+
return _build_response(client=client, response=response)
102+
103+
104+
def sync(
105+
username: Any,
106+
*,
107+
client: AuthenticatedClient,
108+
body: BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset = UNSET,
109+
) -> HTTPValidationError | Token | None:
110+
"""Apitoken
111+
112+
Request API token
113+
114+
Args:
115+
username (Any):
116+
body (BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset):
117+
118+
Raises:
119+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
120+
httpx.TimeoutException: If the request takes longer than Client.timeout.
121+
122+
Returns:
123+
HTTPValidationError | Token
124+
"""
125+
126+
return sync_detailed(
127+
username=username,
128+
client=client,
129+
body=body,
130+
).parsed
131+
132+
133+
async def asyncio_detailed(
134+
username: Any,
135+
*,
136+
client: AuthenticatedClient,
137+
body: BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset = UNSET,
138+
) -> Response[HTTPValidationError | Token]:
139+
"""Apitoken
140+
141+
Request API token
142+
143+
Args:
144+
username (Any):
145+
body (BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset):
146+
147+
Raises:
148+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
149+
httpx.TimeoutException: If the request takes longer than Client.timeout.
150+
151+
Returns:
152+
Response[HTTPValidationError | Token]
153+
"""
154+
155+
kwargs = _get_kwargs(
156+
username=username,
157+
body=body,
158+
)
159+
160+
response = await client.get_async_httpx_client().request(**kwargs)
161+
162+
return _build_response(client=client, response=response)
163+
164+
165+
async def asyncio(
166+
username: Any,
167+
*,
168+
client: AuthenticatedClient,
169+
body: BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset = UNSET,
170+
) -> HTTPValidationError | Token | None:
171+
"""Apitoken
172+
173+
Request API token
174+
175+
Args:
176+
username (Any):
177+
body (BodyApitokenApiV2AuthentificationUsersUsernameApiTokenPost | Unset):
178+
179+
Raises:
180+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
181+
httpx.TimeoutException: If the request takes longer than Client.timeout.
182+
183+
Returns:
184+
HTTPValidationError | Token
185+
"""
186+
187+
return (
188+
await asyncio_detailed(
189+
username=username,
190+
client=client,
191+
body=body,
192+
)
193+
).parsed

0 commit comments

Comments
 (0)