Skip to content

Commit e2cc72a

Browse files
committed
Changed Unset structure and added possibility to send headers in requests
1 parent 2e8b82a commit e2cc72a

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

docs/az/docs/integrations/core/api-reference.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@
4848

4949
## Extras
5050

51-
::: integrify.utils._UNSET
51+
::: integrify.utils.UNSET
5252

53-
::: integrify.utils.Unsettable
53+
::: integrify.utils.Unset
5454

55-
::: integrify.utils.UnsettableAndNone
55+
::: integrify.utils.UnsettOrNone
56+
57+
::: integrify.utils.UnsetField
58+
59+
::: integrify.utils.UnsetOrNoneField

src/integrify/api.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from integrify.logger import LOGGER_FUNCTION
99
from integrify.schemas import APIResponse, DryResponse, PayloadBaseModel
10-
from integrify.utils import _UNSET, _ResponseT
10+
from integrify.utils import UNSET, _ResponseT
1111

1212

1313
class APIClient:
@@ -105,8 +105,8 @@ def __getattribute__(self, name: str) -> Any:
105105
verb,
106106
handler,
107107
# Exclude unset values, to trigger pydantic defaults
108-
*(arg for arg in args if arg is not _UNSET),
109-
**{k: v for k, v in kwds.items() if v is not _UNSET},
108+
*(arg for arg in args if arg is not UNSET),
109+
**{k: v for k, v in kwds.items() if v is not UNSET},
110110
)
111111

112112

@@ -178,7 +178,7 @@ def handle_payload(self, *args, **kwds):
178178
return self.__req_model.model_dump(
179179
by_alias=True,
180180
exclude=self.req_model.URL_PARAM_FIELDS,
181-
exclude_none=True,
181+
exclude_unset=True,
182182
mode='json',
183183
)
184184

@@ -275,6 +275,7 @@ def sync_req(
275275
verb: str,
276276
handler: APIPayloadHandler,
277277
*args,
278+
headers: Optional[dict] = None,
278279
**kwds,
279280
) -> Union[httpx.Response, APIResponse[_ResponseT], DryResponse]:
280281
"""Sync sorğu atan funksiya
@@ -287,19 +288,19 @@ def sync_req(
287288
assert isinstance(self.client, httpx.Client)
288289

289290
data = handler.handle_request(*args, **kwds)
290-
headers = handler.headers
291+
full_headers = {**handler.headers, **(headers or {})}
291292
full_url = handler.set_urlparams(url)
292293

293294
if self.dry or handler.dry:
294295
return DryResponse(
295296
url=full_url,
296297
verb=verb,
297298
request_args=handler.req_args,
298-
headers=headers,
299+
headers=full_headers,
299300
data=data,
300301
)
301302

302-
request_kwds = {'headers': headers, **handler.req_args}
303+
request_kwds = {'headers': full_headers, **handler.req_args}
303304

304305
if verb == 'GET':
305306
request_kwds['params'] = data
@@ -325,6 +326,7 @@ async def async_req( # pragma: no cover
325326
verb: str,
326327
handler: APIPayloadHandler,
327328
*args,
329+
headers: Optional[dict] = None,
328330
**kwds,
329331
) -> Union[httpx.Response, APIResponse[_ResponseT], DryResponse]:
330332
"""Async sorğu atan funksiya
@@ -337,7 +339,7 @@ async def async_req( # pragma: no cover
337339
assert isinstance(self.client, httpx.AsyncClient)
338340

339341
data = handler.handle_request(*args, **kwds)
340-
headers = handler.headers
342+
full_headers = {**handler.headers, **(headers or {})}
341343
full_url = handler.set_urlparams(url)
342344

343345
if self.dry:
@@ -346,11 +348,11 @@ async def async_req( # pragma: no cover
346348
url=full_url,
347349
verb=verb,
348350
request_args=handler.req_args,
349-
headers=headers,
351+
headers=full_headers,
350352
data=data,
351353
)
352354

353-
request_kwds = {'headers': headers, **handler.req_args}
355+
request_kwds = {'headers': full_headers, **handler.req_args}
354356

355357
if verb == 'GET':
356358
request_kwds['params'] = data

src/integrify/utils.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,49 @@
11
from enum import Enum
2-
from typing import Literal, TypeVar, Union
2+
from typing import Annotated, Literal, TypeVar, Union
33

4-
from pydantic import BaseModel
4+
from pydantic import BaseModel, Field
55

66
_ResponseT = TypeVar('_ResponseT', bound=Union[BaseModel, dict])
77
"""Dynamic response type."""
88

99
T = TypeVar('T')
1010

11-
_UNSET = object()
11+
12+
class UnsetType:
13+
"""Sentinel type to indicate an unset field value."""
14+
15+
_instance = None
16+
17+
def __new__(cls):
18+
if cls._instance is None:
19+
cls._instance = super().__new__(cls)
20+
return cls._instance
21+
22+
def __repr__(self):
23+
return 'UNSET'
24+
25+
def __str__(self):
26+
return 'UNSET'
27+
28+
def __bool__(self):
29+
return False
30+
31+
32+
UNSET = UnsetType()
1233
"""Set olunmamış argument dəyəri"""
1334

14-
Unsettable = Union[T, Literal[_UNSET]] # type: ignore[valid-type]
35+
Unset = Union[T, Literal[UNSET]] # type: ignore[valid-type]
1536
""" Optional argument tipi """
1637

17-
UnsettableAndNone = Union[T, Literal[_UNSET], None] # type: ignore[valid-type]
38+
UnsettOrNone = Union[T, Literal[UNSET], None] # type: ignore[valid-type]
1839
"""None dəyəri ala bilən optional argument tipi"""
1940

41+
UnsetField = Annotated[Unset[T], Field(default=UNSET)]
42+
"""Pydantic üçün set olunmamış argument dəyəri"""
43+
44+
UnsetOrNoneField = Annotated[UnsettOrNone[T], Field(default=UNSET)]
45+
"""Pydantic üçün set olunmamış və None dəyəri ala bilən argument dəyəri"""
46+
2047

2148
class Environment(str, Enum):
2249
TEST = 'test'

0 commit comments

Comments
 (0)