Skip to content

Commit

Permalink
Add 0 boilerplate sync and async client objects
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fyhertz committed Mar 3, 2021
1 parent d98e349 commit d6c74e8
Show file tree
Hide file tree
Showing 28 changed files with 1,617 additions and 52 deletions.
3 changes: 2 additions & 1 deletion end_to_end_tests/golden-record-custom/custom_e2e/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
""" A client library for accessing My Test API """
from .client import AuthenticatedClient, Client

from .wrapper import MyTestAPIClient, SyncMyTestAPIClient
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from . import (
defaults_tests_defaults_post,
get_basic_list_of_booleans,
get_basic_list_of_floats,
get_basic_list_of_integers,
get_basic_list_of_strings,
get_user_list,
int_enum_tests_int_enum_post,
json_body_tests_json_body_post,
no_response_tests_no_response_get,
octet_stream_tests_octet_stream_get,
optional_value_tests_optional_query_param,
test_inline_objects,
token_with_cookie_auth_token_with_cookie_get,
unsupported_content_tests_unsupported_content_get,
upload_file_tests_upload_post,
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def _build_response(*, response: httpx.Response) -> Response[List[bool]]:


def httpx_request(
*,
client: Client,
) -> Response[List[bool]]:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def _build_response(*, response: httpx.Response) -> Response[List[float]]:


def httpx_request(
*,
client: Client,
) -> Response[List[float]]:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def _build_response(*, response: httpx.Response) -> Response[List[int]]:


def httpx_request(
*,
client: Client,
) -> Response[List[int]]:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def _build_response(*, response: httpx.Response) -> Response[List[str]]:


def httpx_request(
*,
client: Client,
) -> Response[List[str]]:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def _build_response(*, response: httpx.Response) -> Response[None]:


def httpx_request(
*,
client: Client,
) -> Response[None]:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def _build_response(*, response: httpx.Response) -> Response[File]:


def httpx_request(
*,
client: Client,
) -> Response[File]:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def _build_response(*, response: httpx.Response) -> Response[None]:


def httpx_request(
*,
client: Client,
) -> Response[None]:

Expand Down
15 changes: 13 additions & 2 deletions end_to_end_tests/golden-record-custom/custom_e2e/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict
import os
from typing import Dict, Optional

import attr

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

base_url: str
base_url: Optional[str] = attr.ib(None, kw_only=True)
cookies: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
headers: Dict[str, str] = attr.ib(factory=dict, kw_only=True)
timeout: float = attr.ib(5.0, kw_only=True)

def __attrs_post_init__(self) -> None:
env_base_url = os.environ.get("MY_TEST_API_BASE_URL")
self.base_url = self.base_url or env_base_url
if self.base_url is None:
raise ValueError(
f'"base_url" has to be set either from the '
f'environment variable "{env_base_url}", or '
f'passed with the "base_url" argument'
)

def get_headers(self) -> Dict[str, str]:
""" Get headers to be used in all endpoints """
return {**self.headers}
Expand Down
Loading

0 comments on commit d6c74e8

Please sign in to comment.