-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbase.py
More file actions
59 lines (45 loc) · 1.57 KB
/
base.py
File metadata and controls
59 lines (45 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import Callable, TypeVar
from permit import PYDANTIC_VERSION, PermitConfig
from permit.api.base import SimpleHttpClient
if PYDANTIC_VERSION < (2, 0):
from pydantic import BaseModel, Extra, Field
else:
from pydantic.v1 import BaseModel, Extra, Field # type: ignore
T = TypeVar("T", bound=Callable)
TModel = TypeVar("TModel", bound=BaseModel)
TData = TypeVar("TData", bound=BaseModel)
def pagination_params(page: int, per_page: int) -> dict:
return {"page": page, "per_page": per_page}
class ClientConfig(BaseModel):
class Config:
extra = Extra.allow
base_url: str = Field(
...,
description="base url that will prefix the url fragment sent via the client",
)
headers: dict = Field(..., description="http headers sent to the API server")
class BasePdpPermitApi:
"""
The base class for Permit APIs.
"""
def __init__(self, config: PermitConfig):
"""
Initialize a BasePermitApi.
Args:
config: The Permit SDK configuration.
"""
self.config = config
def _build_http_client(self, endpoint_url: str = "", **kwargs):
client_config = ClientConfig(
base_url=f"{self.config.pdp}",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.config.token}",
},
)
client_config_dict = client_config.dict()
client_config_dict.update(kwargs)
return SimpleHttpClient(
client_config_dict,
base_url=endpoint_url,
)