Skip to content

Support common parameters #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from typing import Any, Dict, Union

import httpx

from ...client import Client
from ...types import UNSET, Response, Unset


def _get_kwargs(
*,
client: Client,
common: Union[Unset, str] = UNSET,
) -> Dict[str, Any]:
url = "{}/common_parameters".format(client.base_url)

headers: Dict[str, Any] = client.get_headers()
cookies: Dict[str, Any] = client.get_cookies()

params: Dict[str, Any] = {
"common": common,
}
params = {k: v for k, v in params.items() if v is not UNSET and v is not None}

return {
"url": url,
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
}


def _build_response(*, response: httpx.Response) -> Response[None]:
return Response(
status_code=response.status_code,
content=response.content,
headers=response.headers,
parsed=None,
)


def sync_detailed(
*,
client: Client,
common: Union[Unset, str] = UNSET,
) -> Response[None]:
kwargs = _get_kwargs(
client=client,
common=common,
)

response = httpx.get(
**kwargs,
)

return _build_response(response=response)


async def asyncio_detailed(
*,
client: Client,
common: Union[Unset, str] = UNSET,
) -> Response[None]:
kwargs = _get_kwargs(
client=client,
common=common,
)

async with httpx.AsyncClient() as _client:
response = await _client.get(**kwargs)

return _build_response(response=response)
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from typing import Any, Dict, Union

import httpx

from ...client import Client
from ...types import UNSET, Response, Unset


def _get_kwargs(
*,
client: Client,
common: Union[Unset, str] = UNSET,
) -> Dict[str, Any]:
url = "{}/common_parameters".format(client.base_url)

headers: Dict[str, Any] = client.get_headers()
cookies: Dict[str, Any] = client.get_cookies()

params: Dict[str, Any] = {
"common": common,
}
params = {k: v for k, v in params.items() if v is not UNSET and v is not None}

return {
"url": url,
"headers": headers,
"cookies": cookies,
"timeout": client.get_timeout(),
"params": params,
}


def _build_response(*, response: httpx.Response) -> Response[None]:
return Response(
status_code=response.status_code,
content=response.content,
headers=response.headers,
parsed=None,
)


def sync_detailed(
*,
client: Client,
common: Union[Unset, str] = UNSET,
) -> Response[None]:
kwargs = _get_kwargs(
client=client,
common=common,
)

response = httpx.post(
**kwargs,
)

return _build_response(response=response)


async def asyncio_detailed(
*,
client: Client,
common: Union[Unset, str] = UNSET,
) -> Response[None]:
kwargs = _get_kwargs(
client=client,
common=common,
)

async with httpx.AsyncClient() as _client:
response = await _client.post(**kwargs)

return _build_response(response=response)
21 changes: 21 additions & 0 deletions end_to_end_tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,27 @@
}
}
}
},
"/common_parameters": {
"parameters": [
{
"schema": {
"type": "string"
},
"name": "common",
"in": "query"
}
],
"get": {
"responses": {
"200": {"description": "Success"}
}
},
"post": {
"responses": {
"200": {"description": "Success"}
}
}
}
},
"components": {
Expand Down
6 changes: 5 additions & 1 deletion openapi_python_client/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def from_data(
endpoint, schemas = Endpoint.from_data(
data=operation, path=path, method=method, tag=tag, schemas=schemas, config=config
)
if not isinstance(endpoint, ParseError):
endpoint, schemas = Endpoint._add_parameters(
endpoint=endpoint, data=path_data, schemas=schemas, config=config
)
if isinstance(endpoint, ParseError):
endpoint.header = (
f"ERROR parsing {method.upper()} {path} within {tag}. Endpoint will not be generated."
Expand Down Expand Up @@ -209,7 +213,7 @@ def _add_responses(

@staticmethod
def _add_parameters(
*, endpoint: "Endpoint", data: oai.Operation, schemas: Schemas, config: Config
*, endpoint: "Endpoint", data: Union[oai.Operation, oai.PathItem], schemas: Schemas, config: Config
) -> Tuple[Union["Endpoint", ParseError], Schemas]:
endpoint = deepcopy(endpoint)
if data.parameters is None:
Expand Down