Skip to content

Commit

Permalink
Merge pull request #46 from ponytailer/add-header-support
Browse files Browse the repository at this point in the history
ft(headers): Add header support
  • Loading branch information
ponytailer authored Apr 18, 2024
2 parents 35b9d34 + b717791 commit 7e589ed
Show file tree
Hide file tree
Showing 9 changed files with 613 additions and 513 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,24 @@ book = client.get(1)
person = client.get_person(1)


```

# change log

### v0.1.14: add global or request level headers

```python

# global level headers
my_client = R("http://localhost/v1", headers={"Authorization": "xxxxxxx"})

# request level headers, and its priority is higher than global.

# header should be xxxxxxx
my_client.delete(1)
# header should be zzzzz
my_client.get(1, request_headers={"Authorization": "zzzzz"})
# header should be yyyyy
my_client.post(1, request_headers={"Authorization": "yyyyy"})

```
1,059 changes: 562 additions & 497 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions pydantic_client/clients/abstract_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

class AbstractClient:

def do_request(
self, request: HttpRequest,
) -> Any:
def do_request(self, request: HttpRequest) -> Any:
raise NotImplementedError

@staticmethod
Expand Down
10 changes: 7 additions & 3 deletions pydantic_client/clients/aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Type
from typing import Any, Type, Dict

from aiohttp.client import ClientSession

Expand All @@ -10,18 +10,22 @@
class AIOHttpClient(AbstractClient):
runner_class: Type[Proxy] = AsyncClientProxy

def __init__(self, base_url: str):
def __init__(self, base_url: str, headers: Dict[str, Any] = None):
self.base_url = base_url.rstrip("/")
self.headers = headers

async def do_request(self, request: HttpRequest) -> Any:
data, json = self.parse_request(request)
headers = request.request_headers if request.request_headers \
else self.headers
async with ClientSession() as session:
try:
req = session.request(
url=self.base_url + request.url,
method=request.method,
json=json,
data=data
data=data,
headers=headers
)

async with req as resp:
Expand Down
11 changes: 8 additions & 3 deletions pydantic_client/clients/httpx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Type
from typing import Any, Dict, Type

from httpx import AsyncClient

Expand All @@ -10,19 +10,24 @@
class HttpxClient(AbstractClient):
runner_class: Type[Proxy] = AsyncClientProxy

def __init__(self, base_url: str, http2: bool = False):
def __init__(self, base_url: str, http2: bool = False,
headers: Dict[str, Any] = None):
self.base_url = base_url.rstrip("/")
self.http2 = http2
self.headers = headers

async def do_request(self, request: HttpRequest) -> Any:
data, json = self.parse_request(request)
headers = request.request_headers if request.request_headers \
else self.headers
async with AsyncClient(http2=self.http2) as session:
try:
response = await session.request(
url=self.base_url + request.url,
method=request.method,
json=json,
data=data
data=data,
headers=headers
)
response.raise_for_status()
if response.is_success:
Expand Down
11 changes: 8 additions & 3 deletions pydantic_client/clients/requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Type
from typing import Any, Dict, Type

from requests import Session

Expand All @@ -8,21 +8,26 @@


class RequestsClient(AbstractClient):

runner_class: Type[Proxy] = ClientProxy

def __init__(self, base_url: str, session: Session = Session()):
def __init__(self, base_url: str, session: Session = Session(),
headers: Dict[str, Any] = None):
self.session = session
self.base_url = base_url.rstrip("/")
self.headers = headers

def do_request(self, request: HttpRequest) -> Any:
data, json = self.parse_request(request)
headers = request.request_headers if request.request_headers \
else self.headers

try:
return self.session.request(
url=self.base_url + request.url,
method=request.method,
json=json,
data=data,
headers=headers
).json()
except BaseException as e:
raise e
9 changes: 6 additions & 3 deletions pydantic_client/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import re
from typing import Any, Dict
from urllib.parse import urlparse, parse_qsl
from urllib.parse import parse_qsl, urlparse

from pydantic import BaseModel

Expand Down Expand Up @@ -33,7 +33,7 @@ def _apply_args(self, *args, **kwargs) -> Dict[str, Any]:
def _get_url(self, args) -> str:
keys = self.querystring_pattern.findall(self.method_info.url)
query_args = {arg: val for arg, val in args.items() if
arg in keys and val}
arg in keys and val}

for key in keys:
args.pop(key, None)
Expand Down Expand Up @@ -61,14 +61,17 @@ def get_request(self, *args, **kwargs):
url: str = self._get_url(func_args)
if self.method_info.form_body:
data, json = self.dict_to_body(func_args), {}
request_headers = data.pop("request_headers", None)
else:
data, json = {}, self.dict_to_body(func_args)
request_headers = json.pop("request_headers", None)

return HttpRequest(
url=url,
data=data,
json_body=json,
method=self.method_info.http_method
method=self.method_info.http_method,
request_headers=request_headers
)


Expand Down
1 change: 1 addition & 0 deletions pydantic_client/schema/http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ class HttpRequest(BaseModel):
json_body: Optional[Dict] = {}
url: str
method: str
request_headers: Optional[Dict] = None
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ homepage = "https://github.com/ponytailer/pydantic-client"
[[tool.poetry.source]]
name = "tuna"
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true


[tool.poetry.dependencies]
Expand Down

0 comments on commit 7e589ed

Please sign in to comment.