-
-
Save cdeler/47dd0cc83c1fa0807aa7912cfa322f9e to your computer and use it in GitHub Desktop.
Support 'httpx.json = ...' proposal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .__version__ import __description__, __title__, __version__ | |
from ._api import delete, get, head, options, patch, post, put, request, stream | |
from ._auth import Auth, BasicAuth, DigestAuth | |
from ._client import AsyncClient, Client | |
from ._config import Limits, Proxy, Timeout, create_ssl_context | |
from ._exceptions import ( | |
CloseError, | |
ConnectError, | |
ConnectTimeout, | |
CookieConflict, | |
DecodingError, | |
HTTPError, | |
HTTPStatusError, | |
InvalidURL, | |
LocalProtocolError, | |
NetworkError, | |
PoolTimeout, | |
ProtocolError, | |
ProxyError, | |
ReadError, | |
ReadTimeout, | |
RemoteProtocolError, | |
RequestError, | |
RequestNotRead, | |
ResponseClosed, | |
ResponseNotRead, | |
StreamConsumed, | |
StreamError, | |
TimeoutException, | |
TooManyRedirects, | |
TransportError, | |
UnsupportedProtocol, | |
WriteError, | |
WriteTimeout, | |
) | |
from ._models import URL, Cookies, Headers, QueryParams, Request, Response | |
from ._status_codes import StatusCode, codes | |
from ._transports.asgi import ASGITransport | |
from ._transports.wsgi import WSGITransport | |
from ._json import json, JsonMarshaller | |
__all__ = [ | |
"__description__", | |
"__title__", | |
"__version__", | |
"ASGITransport", | |
"AsyncClient", | |
"Auth", | |
"BasicAuth", | |
"Client", | |
"CloseError", | |
"codes", | |
"ConnectError", | |
"ConnectTimeout", | |
"CookieConflict", | |
"Cookies", | |
"create_ssl_context", | |
"DecodingError", | |
"delete", | |
"DigestAuth", | |
"get", | |
"head", | |
"Headers", | |
"HTTPError", | |
"HTTPStatusError", | |
"InvalidURL", | |
"json", | |
"JsonMarshaller", | |
"Limits", | |
"LocalProtocolError", | |
"NetworkError", | |
"options", | |
"patch", | |
"PoolTimeout", | |
"post", | |
"ProtocolError", | |
"Proxy", | |
"ProxyError", | |
"put", | |
"QueryParams", | |
"ReadError", | |
"ReadTimeout", | |
"RemoteProtocolError", | |
"request", | |
"Request", | |
"RequestError", | |
"RequestNotRead", | |
"Response", | |
"ResponseClosed", | |
"ResponseNotRead", | |
"StatusCode", | |
"stream", | |
"StreamConsumed", | |
"StreamError", | |
"Timeout", | |
"TimeoutException", | |
"TooManyRedirects", | |
"TransportError", | |
"UnsupportedProtocol", | |
"URL", | |
"WriteError", | |
"WriteTimeout", | |
"WSGITransport", | |
] | |
__locals = locals() | |
for __name in __all__: | |
if not __name.startswith("__"): | |
setattr(__locals[__name], "__module__", "httpx") # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typing | |
class JsonMarshaller: | |
def loads(self, data: str, **kwargs: typing.Any) -> dict: | |
raise NotImplementedError | |
def dumps(self, data: typing.Any, **kwargs: typing.Any) -> str: | |
raise NotImplementedError | |
class StdlibJsonMarshaller(JsonMarshaller): | |
def __init__(self) -> None: | |
import json as jsonlib | |
self._json = jsonlib | |
def loads(self, data: str, **kwargs: typing.Any) -> dict: | |
return self._json.loads(data, **kwargs) | |
def dumps(self, obj: typing.Any, **kwargs: typing.Any) -> str: | |
return self._json.dumps(obj, **kwargs) | |
class JsonFacade: | |
def __init__(self): | |
self._json = StdlibJsonMarshaller() | |
@property | |
def json(self) -> JsonMarshaller: | |
return self._json | |
@json.setter | |
def json(self, jsonlib: JsonMarshaller) -> None: | |
self._json = jsonlib | |
json = JsonFacade() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment