Skip to content

Commit

Permalink
Update package versions and refactor response handling code
Browse files Browse the repository at this point in the history
This commit includes an update of versions for the 'attrs' and 'pytest' packages in the `poetry.lock` file. The `pydantic_aiohttp` package has also been bumped from version 1.0.0 to 1.0.1. In addition, the code for handling responses in the `pydantic_aiohttp` module has been refactored for better clarity and separation of different types of responses. Unnecessary import statements have been removed to clean up the code.
  • Loading branch information
pylakey committed Jan 16, 2024
1 parent f88f19f commit 49386a9
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 27 deletions.
19 changes: 10 additions & 9 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

148 changes: 144 additions & 4 deletions pydantic_aiohttp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,148 @@
__version__ = '1.0.0'
__version__ = '1.0.1'
__author__ = "pylakey <[email protected]>"

from . import encoders
from . import utils
from . import errors
from . import responses
from . import types
from .client import Client
from .errors import *
from .types import *
from .errors import HTTPBadGateway
from .errors import HTTPBadRequest
from .errors import HTTPConflict
from .errors import HTTPExpectationFailed
from .errors import HTTPFailedDependency
from .errors import HTTPForbidden
from .errors import HTTPFound
from .errors import HTTPGatewayTimeout
from .errors import HTTPGone
from .errors import HTTPHttpServerVersionNotSupported
from .errors import HTTPImATeapot
from .errors import HTTPInsufficientStorage
from .errors import HTTPInternalServerError
from .errors import HTTPLengthRequired
from .errors import HTTPLocked
from .errors import HTTPLoopDetected
from .errors import HTTPMethodNotAllowed
from .errors import HTTPMisdirectedRequest
from .errors import HTTPMovedPermanently
from .errors import HTTPMultipleChoices
from .errors import HTTPNetworkAuthenticationRequired
from .errors import HTTPNotAcceptable
from .errors import HTTPNotExtended
from .errors import HTTPNotFound
from .errors import HTTPNotImplemented
from .errors import HTTPNotModified
from .errors import HTTPPaymentRequired
from .errors import HTTPPermanentRedirect
from .errors import HTTPPreconditionFailed
from .errors import HTTPPreconditionRequired
from .errors import HTTPProxyAuthenticationRequired
from .errors import HTTPRequestEntityTooLarge
from .errors import HTTPRequestHeaderFieldsTooLarge
from .errors import HTTPRequestTimeout
from .errors import HTTPRequestUriTooLong
from .errors import HTTPRequestedRangeNotSatisfiable
from .errors import HTTPSeeOther
from .errors import HTTPServiceUnavailable
from .errors import HTTPTemporaryRedirect
from .errors import HTTPTooEarly
from .errors import HTTPTooManyRequests
from .errors import HTTPUnauthorized
from .errors import HTTPUnavailableForLegalReasons
from .errors import HTTPUnprocessableEntity
from .errors import HTTPUnsupportedMediaType
from .errors import HTTPUpgradeRequired
from .errors import HTTPUseProxy
from .errors import HTTPVariantAlsoNegotiates
from .responses import JSONResponseClass
from .responses import NoneResponseClass
from .responses import PlainTextResponseClass
from .responses import PydanticModelResponseClass
from .responses import RawResponseClass
from .responses import ResponseClass
from .responses import StreamResponseClass
from .types import Body
from .types import Cookies
from .types import EmptyResponse
from .types import ErrorResponseModels
from .types import Headers
from .types import HttpEncodableMapping
from .types import Params
from .types import StrIntMapping

__all__ = [
'Client',
'encoders',
'types',
'responses',
'errors',

# Types
'EmptyResponse',
'StrIntMapping',
'HttpEncodableMapping',
'Params',
'Cookies',
'Headers',
'Body',
'ErrorResponseModels',

# Errors
'HTTPBadGateway',
'HTTPBadRequest',
'HTTPConflict',
'HTTPExpectationFailed',
'HTTPFailedDependency',
'HTTPForbidden',
'HTTPFound',
'HTTPGatewayTimeout',
'HTTPGone',
'HTTPHttpServerVersionNotSupported',
'HTTPImATeapot',
'HTTPInsufficientStorage',
'HTTPInternalServerError',
'HTTPLengthRequired',
'HTTPLocked',
'HTTPLoopDetected',
'HTTPMethodNotAllowed',
'HTTPMisdirectedRequest',
'HTTPMovedPermanently',
'HTTPMultipleChoices',
'HTTPNetworkAuthenticationRequired',
'HTTPNotAcceptable',
'HTTPNotExtended',
'HTTPNotFound',
'HTTPNotImplemented',
'HTTPNotModified',
'HTTPPaymentRequired',
'HTTPPermanentRedirect',
'HTTPPreconditionFailed',
'HTTPPreconditionRequired',
'HTTPProxyAuthenticationRequired',
'HTTPRequestEntityTooLarge',
'HTTPRequestHeaderFieldsTooLarge',
'HTTPRequestTimeout',
'HTTPRequestUriTooLong',
'HTTPRequestedRangeNotSatisfiable',
'HTTPSeeOther',
'HTTPServiceUnavailable',
'HTTPTemporaryRedirect',
'HTTPTooEarly',
'HTTPTooManyRequests',
'HTTPUnauthorized',
'HTTPUnavailableForLegalReasons',
'HTTPUnprocessableEntity',
'HTTPUnsupportedMediaType',
'HTTPUpgradeRequired',
'HTTPUseProxy',
'HTTPVariantAlsoNegotiates',

# Responses
'ResponseClass',
'RawResponseClass',
'NoneResponseClass',
'PlainTextResponseClass',
'JSONResponseClass',
'PydanticModelResponseClass',
'StreamResponseClass',
]
4 changes: 3 additions & 1 deletion pydantic_aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any
from typing import Optional
from typing import Type
from typing import TypeVar
from typing import Union

import aiohttp
Expand All @@ -22,12 +23,13 @@
from .types import ErrorResponseModels
from .types import Headers
from .types import Params
from .types import ResponseType
from .utils import DEFAULT_DOWNLOAD_CHUNK_SIZE
from .utils import json_serialize
from .utils import model_to_dict
from .utils import read_file_by_chunk

ResponseType = TypeVar('ResponseType')


class Client:
def __init__(
Expand Down
22 changes: 14 additions & 8 deletions pydantic_aiohttp/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from .utils import DEFAULT_DOWNLOAD_CHUNK_SIZE

ResponseContentType = TypeVar('ResponseContentType')
PydanticModel = TypeVar('PydanticModel')


class ResponseClass(abc.ABC, Generic[ResponseContentType]):
Expand All @@ -31,6 +30,11 @@ async def parse(self, *args, **kwargs) -> Optional[ResponseContentType]:
return self.aiohttp_response.content


class RawResponseClass(ResponseClass[aiohttp.ClientResponse]):
async def parse(self, *args, **kwargs) -> aiohttp.ClientResponse:
return self.aiohttp_response


class NoneResponseClass(ResponseClass[None]):
async def parse(self) -> None:
return None
Expand All @@ -41,15 +45,22 @@ async def parse(self, *args, **kwargs) -> str:
return await self.aiohttp_response.text(self.charset)


class JSONResponseClass(ResponseClass[Union[dict, list, str, None]]):
async def parse(self, *args, **kwargs) -> Optional[ResponseContentType]:
_JsonBaseFields = Union[str, int, float, bool, None]
Json = Union[_JsonBaseFields, dict[str, _JsonBaseFields], list[_JsonBaseFields]]


class JSONResponseClass(ResponseClass[Json]):
async def parse(self, *args, **kwargs) -> Optional[Json]:
return await self.aiohttp_response.json(
encoding=self.charset,
loads=ujson.loads,
content_type=None
)


PydanticModel = TypeVar('PydanticModel')


class PydanticModelResponseClass(ResponseClass[PydanticModel]):
async def parse(self, *args, response_model: Type[PydanticModel], **kwargs) -> PydanticModel:
if response_model is None:
Expand Down Expand Up @@ -78,8 +89,3 @@ async def parse(
await fd.write(chunk)

return filepath


class RawResponseClass(ResponseClass[aiohttp.ClientResponse]):
async def parse(self, *args, **kwargs) -> aiohttp.ClientResponse:
return self.aiohttp_response
3 changes: 0 additions & 3 deletions pydantic_aiohttp/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any
from typing import Type
from typing import TypeVar
from typing import Union

import pydantic
Expand All @@ -13,8 +12,6 @@
Body = Union[dict[str, Any], pydantic.BaseModel]
ErrorResponseModels = dict[int, Type[pydantic.BaseModel]]

ResponseType = TypeVar('ResponseType')


class EmptyResponse(pydantic.BaseModel):
pass
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydantic_aiohttp"
version = "1.0.0"
version = "1.0.1"
description = "Simple HTTP Client based on aiohttp with integration of pydantic"
authors = ["pylakey <[email protected]>"]
license = "MIT"
Expand All @@ -25,7 +25,7 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.9"
aiofiles = "^23.0.0"
aiohttp = { extras = ["speedups"], version = "^3.8.1" }
aiohttp = { version = "==3.*", extras = ["speedups"] }
pydantic = ">=2"
ujson = "^5.7.0"

Expand Down

0 comments on commit 49386a9

Please sign in to comment.