Skip to content

feat: Add raise_on_unexpected_status flag to Client #593

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 9 commits into from
Dec 18, 2022
2 changes: 2 additions & 0 deletions end_to_end_tests/golden-record/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ client = AuthenticatedClient(
)
```

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.

Things to know:
1. Every path/method combo becomes a Python module with four functions:
1. `sync`: Blocking request that returns parsed data (if successful) or `None`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from http import HTTPStatus
from typing import Any, Dict, Union
from typing import Any, Dict, Optional, Union

import httpx

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

Expand Down Expand Up @@ -32,12 +33,21 @@ def _get_kwargs(
}


def _build_response(*, response: httpx.Response) -> Response[Any]:
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
else:
return None


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


Expand All @@ -50,6 +60,10 @@ def sync_detailed(
Args:
common (Union[Unset, None, 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[Any]
"""
Expand All @@ -64,7 +78,7 @@ def sync_detailed(
**kwargs,
)

return _build_response(response=response)
return _build_response(client=client, response=response)


async def asyncio_detailed(
Expand All @@ -76,6 +90,10 @@ async def asyncio_detailed(
Args:
common (Union[Unset, None, 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[Any]
"""
Expand All @@ -88,4 +106,4 @@ async def asyncio_detailed(
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
response = await _client.request(**kwargs)

return _build_response(response=response)
return _build_response(client=client, response=response)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from http import HTTPStatus
from typing import Any, Dict, Union
from typing import Any, Dict, Optional, Union

import httpx

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

Expand Down Expand Up @@ -32,12 +33,21 @@ def _get_kwargs(
}


def _build_response(*, response: httpx.Response) -> Response[Any]:
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
else:
return None


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


Expand All @@ -50,6 +60,10 @@ def sync_detailed(
Args:
common (Union[Unset, None, 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[Any]
"""
Expand All @@ -64,7 +78,7 @@ def sync_detailed(
**kwargs,
)

return _build_response(response=response)
return _build_response(client=client, response=response)


async def asyncio_detailed(
Expand All @@ -76,6 +90,10 @@ async def asyncio_detailed(
Args:
common (Union[Unset, None, 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[Any]
"""
Expand All @@ -88,4 +106,4 @@ async def asyncio_detailed(
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
response = await _client.request(**kwargs)

return _build_response(response=response)
return _build_response(client=client, response=response)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from http import HTTPStatus
from typing import Any, Dict, Union
from typing import Any, Dict, Optional, Union

import httpx

from ... import errors
from ...client import Client
from ...models.get_location_header_types_int_enum_header import GetLocationHeaderTypesIntEnumHeader
from ...models.get_location_header_types_string_enum_header import GetLocationHeaderTypesStringEnumHeader
Expand Down Expand Up @@ -51,12 +52,21 @@ def _get_kwargs(
}


def _build_response(*, response: httpx.Response) -> Response[Any]:
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
else:
return None


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


Expand All @@ -79,6 +89,10 @@ def sync_detailed(
int_enum_header (Union[Unset, GetLocationHeaderTypesIntEnumHeader]):
string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]):

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[Any]
"""
Expand All @@ -98,7 +112,7 @@ def sync_detailed(
**kwargs,
)

return _build_response(response=response)
return _build_response(client=client, response=response)


async def asyncio_detailed(
Expand All @@ -120,6 +134,10 @@ async def asyncio_detailed(
int_enum_header (Union[Unset, GetLocationHeaderTypesIntEnumHeader]):
string_enum_header (Union[Unset, GetLocationHeaderTypesStringEnumHeader]):

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[Any]
"""
Expand All @@ -137,4 +155,4 @@ async def asyncio_detailed(
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
response = await _client.request(**kwargs)

return _build_response(response=response)
return _build_response(client=client, response=response)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime
from http import HTTPStatus
from typing import Any, Dict, Union
from typing import Any, Dict, Optional, Union

import httpx

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

Expand Down Expand Up @@ -56,12 +57,21 @@ def _get_kwargs(
}


def _build_response(*, response: httpx.Response) -> Response[Any]:
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
else:
return None


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


Expand All @@ -80,6 +90,10 @@ def sync_detailed(
null_not_required (Union[Unset, None, datetime.datetime]):
not_null_not_required (Union[Unset, None, datetime.datetime]):

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[Any]
"""
Expand All @@ -97,7 +111,7 @@ def sync_detailed(
**kwargs,
)

return _build_response(response=response)
return _build_response(client=client, response=response)


async def asyncio_detailed(
Expand All @@ -115,6 +129,10 @@ async def asyncio_detailed(
null_not_required (Union[Unset, None, datetime.datetime]):
not_null_not_required (Union[Unset, None, datetime.datetime]):

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[Any]
"""
Expand All @@ -130,4 +148,4 @@ async def asyncio_detailed(
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
response = await _client.request(**kwargs)

return _build_response(response=response)
return _build_response(client=client, response=response)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from http import HTTPStatus
from typing import Any, Dict
from typing import Any, Dict, Optional

import httpx

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

Expand Down Expand Up @@ -42,12 +43,21 @@ def _get_kwargs(
}


def _build_response(*, response: httpx.Response) -> Response[Any]:
def _parse_response(*, client: Client, response: httpx.Response) -> Optional[Any]:
if response.status_code == HTTPStatus.OK:
return None
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(f"Unexpected status code: {response.status_code}")
else:
return None


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


Expand All @@ -69,6 +79,10 @@ def sync_detailed(
header_param (str):
cookie_param (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[Any]
"""
Expand All @@ -87,7 +101,7 @@ def sync_detailed(
**kwargs,
)

return _build_response(response=response)
return _build_response(client=client, response=response)


async def asyncio_detailed(
Expand All @@ -108,6 +122,10 @@ async def asyncio_detailed(
header_param (str):
cookie_param (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[Any]
"""
Expand All @@ -124,4 +142,4 @@ async def asyncio_detailed(
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
response = await _client.request(**kwargs)

return _build_response(response=response)
return _build_response(client=client, response=response)
Loading