Skip to content

Commit 2931e8d

Browse files
committed
Add 0 boilerplate sync and async client objects
With the current approach, for every API call, the user has to import a a module from a tag package and call the `asyncio` or `sync` method with the "client=client" argument. This patch adds a wrapper around tag packages and two wrapper `Client` objects to spare the need for that boilerplate code. Check this issue for more information: openapi-generators#224 `base_url` can be omitted during client initialization if `SPEC_TITLE_BASE_URL` is set. Object-oriented clients have also been renamed to include the spec title.
1 parent d98e349 commit 2931e8d

29 files changed

+1626
-52
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
""" A client library for accessing My Test API """
2-
from .client import AuthenticatedClient, Client
2+
3+
from .wrapper import MyTestAPIClient, SyncMyTestAPIClient
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from . import (
2+
defaults_tests_defaults_post,
3+
get_basic_list_of_booleans,
4+
get_basic_list_of_floats,
5+
get_basic_list_of_integers,
6+
get_basic_list_of_strings,
7+
get_user_list,
8+
int_enum_tests_int_enum_post,
9+
json_body_tests_json_body_post,
10+
no_response_tests_no_response_get,
11+
octet_stream_tests_octet_stream_get,
12+
optional_value_tests_optional_query_param,
13+
test_inline_objects,
14+
token_with_cookie_auth_token_with_cookie_get,
15+
unsupported_content_tests_unsupported_content_get,
16+
upload_file_tests_upload_post,
17+
)

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def _build_response(*, response: httpx.Response) -> Response[List[bool]]:
2727

2828

2929
def httpx_request(
30-
*,
3130
client: Client,
3231
) -> Response[List[bool]]:
3332

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def _build_response(*, response: httpx.Response) -> Response[List[float]]:
2727

2828

2929
def httpx_request(
30-
*,
3130
client: Client,
3231
) -> Response[List[float]]:
3332

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def _build_response(*, response: httpx.Response) -> Response[List[int]]:
2727

2828

2929
def httpx_request(
30-
*,
3130
client: Client,
3231
) -> Response[List[int]]:
3332

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def _build_response(*, response: httpx.Response) -> Response[List[str]]:
2727

2828

2929
def httpx_request(
30-
*,
3130
client: Client,
3231
) -> Response[List[str]]:
3332

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/no_response_tests_no_response_get.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def _build_response(*, response: httpx.Response) -> Response[None]:
1515

1616

1717
def httpx_request(
18-
*,
1918
client: Client,
2019
) -> Response[None]:
2120

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/octet_stream_tests_octet_stream_get.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def _build_response(*, response: httpx.Response) -> Response[File]:
2929

3030

3131
def httpx_request(
32-
*,
3332
client: Client,
3433
) -> Response[File]:
3534

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/unsupported_content_tests_unsupported_content_get.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def _build_response(*, response: httpx.Response) -> Response[None]:
1515

1616

1717
def httpx_request(
18-
*,
1918
client: Client,
2019
) -> Response[None]:
2120

end_to_end_tests/golden-record-custom/custom_e2e/client.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Dict
1+
import os
2+
from typing import Dict, Optional
23

34
import attr
45

@@ -7,11 +8,21 @@
78
class Client:
89
""" A class for keeping track of data related to the API """
910

10-
base_url: str
11+
base_url: Optional[str] = attr.ib(None, kw_only=True)
1112
cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
1213
headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
1314
timeout: float = attr.ib(5.0, kw_only=True)
1415

16+
def __attrs_post_init__(self) -> None:
17+
env_base_url = os.environ.get("MY_TEST_API_BASE_URL")
18+
self.base_url = self.base_url or env_base_url
19+
if self.base_url is None:
20+
raise ValueError(
21+
f'"base_url" has to be set either from the '
22+
f'environment variable "{env_base_url}", or '
23+
f'passed with the "base_url" argument'
24+
)
25+
1526
def get_headers(self) -> Dict[str, str]:
1627
""" Get headers to be used in all endpoints """
1728
return {**self.headers}

0 commit comments

Comments
 (0)