Skip to content

Commit c8c24e7

Browse files
authored
♻️ separate github core
1 parent 193eef8 commit c8c24e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+339
-187
lines changed

codegen/templates/client/client.py.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ from pydantic import BaseModel, parse_obj_as
2121
from githubkit.utils import exclude_unset
2222

2323
if TYPE_CHECKING:
24-
from githubkit import GitHub
24+
from githubkit import GitHubCore
2525
from githubkit.response import Response
2626

2727
class {{ pascal_case(tag) }}Client:
28-
def __init__(self, github: "GitHub"):
28+
def __init__(self, github: "GitHubCore"):
2929
self._github = github
3030

3131
{% for endpoint in endpoints %}

codegen/templates/namespace/namespace.py.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ from .{{ tag }} import {{ pascal_case(tag) }}Client
1313
{% endfor %}
1414

1515
if TYPE_CHECKING:
16-
from githubkit import GitHub
16+
from githubkit import GitHubCore
1717

1818

1919
class RestNamespace:
20-
def __init__(self, github: "GitHub"):
20+
def __init__(self, github: "GitHubCore"):
2121
self._github = github
2222

2323
{% for tag in tags %}

githubkit/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from .core import GitHub as GitHub
21
from .config import Config as Config
2+
from .github import GitHub as GitHub
3+
from .core import GitHubCore as GitHubCore
34
from .response import Response as Response
45
from .paginator import Paginator as Paginator
56
from .auth import AppAuthStrategy as AppAuthStrategy

githubkit/auth/action.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .base import BaseAuthStrategy
99

1010
if TYPE_CHECKING:
11-
from githubkit import GitHub
11+
from githubkit import GitHubCore
1212

1313

1414
class ActionAuth(httpx.Auth):
@@ -25,5 +25,5 @@ def auth_flow(
2525

2626

2727
class ActionAuthStrategy(BaseAuthStrategy):
28-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
28+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
2929
return ActionAuth()

githubkit/auth/app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
jwt = None
2525

2626
if TYPE_CHECKING:
27-
from githubkit import GitHub, Response
27+
from githubkit import Response, GitHubCore
2828
from githubkit.rest.types import AppPermissionsType
2929

3030

3131
@dataclass(slots=True)
3232
class AppAuth(httpx.Auth):
3333
"""GitHub App or Installation Authentication Hook"""
3434

35-
github: "GitHub"
35+
github: "GitHubCore"
3636
app_id: str
3737
private_key: str
3838
client_id: Optional[str] = None
@@ -251,7 +251,7 @@ def as_oauth_app(self) -> OAuthAppAuthStrategy:
251251
)
252252
return OAuthAppAuthStrategy(self.client_id, self.client_secret)
253253

254-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
254+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
255255
return AppAuth(
256256
github,
257257
self.app_id,
@@ -285,7 +285,7 @@ def as_app(self) -> AppAuthStrategy:
285285
self.cache,
286286
)
287287

288-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
288+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
289289
return AppAuth(
290290
github,
291291
self.app_id,

githubkit/auth/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import httpx
55

66
if TYPE_CHECKING:
7-
from githubkit import GitHub
7+
from githubkit import GitHubCore
88

99

1010
class BaseAuthStrategy(abc.ABC):
1111
@abc.abstractmethod
12-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
12+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
1313
raise NotImplementedError

githubkit/auth/oauth.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
from ._url import require_bypass, get_oauth_base_url, require_basic_auth
3030

3131
if TYPE_CHECKING:
32-
from githubkit import GitHub
32+
from githubkit import GitHubCore
3333

3434

3535
def create_device_code(
36-
github: "GitHub", client_id: str, scopes: Optional[List[str]] = None
36+
github: "GitHubCore", client_id: str, scopes: Optional[List[str]] = None
3737
) -> Generator[httpx.Request, httpx.Response, Dict[str, Any]]:
3838
"""Create a device code for OAuth."""
3939
base_url = get_oauth_base_url(github.config.base_url)
@@ -55,7 +55,7 @@ def create_device_code(
5555

5656

5757
def exchange_web_flow_code(
58-
github: "GitHub",
58+
github: "GitHubCore",
5959
client_id: str,
6060
client_secret: str,
6161
code: str,
@@ -85,7 +85,7 @@ def exchange_web_flow_code(
8585

8686

8787
def exchange_device_code(
88-
github: "GitHub", client_id: str, device_code: str
88+
github: "GitHubCore", client_id: str, device_code: str
8989
) -> Generator[httpx.Request, httpx.Response, Dict[str, Any]]:
9090
"""Exchange device code for token."""
9191
base_url = get_oauth_base_url(github.config.base_url)
@@ -108,7 +108,7 @@ def exchange_device_code(
108108

109109

110110
def refresh_token(
111-
github: "GitHub", client_id: str, client_secret: str, refresh_token: str
111+
github: "GitHubCore", client_id: str, client_secret: str, refresh_token: str
112112
) -> Generator[httpx.Request, httpx.Response, Dict[str, Any]]:
113113
"""Refresh token."""
114114
base_url = get_oauth_base_url(github.config.base_url)
@@ -132,7 +132,7 @@ def refresh_token(
132132
class OAuthWebAuth(httpx.Auth):
133133
"""OAuth Web Flow Hook Authentication"""
134134

135-
github: "GitHub"
135+
github: "GitHubCore"
136136
client_id: str
137137
client_secret: str
138138
code: str
@@ -237,7 +237,7 @@ def auth_flow(
237237
class OAuthDeviceAuth(httpx.Auth):
238238
"""OAuth Device Flow Hook Authentication"""
239239

240-
github: "GitHub"
240+
github: "GitHubCore"
241241
client_id: str
242242
on_verification: Union[
243243
Callable[[Dict[str, Any]], None],
@@ -394,7 +394,7 @@ def as_web_user(
394394
self.client_id, self.client_secret, code, redirect_uri
395395
)
396396

397-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
397+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
398398
return httpx.BasicAuth(self.client_id, self.client_secret)
399399

400400

@@ -410,7 +410,7 @@ class OAuthWebAuthStrategy(BaseAuthStrategy):
410410
def as_oauth_app(self) -> OAuthAppAuthStrategy:
411411
return OAuthAppAuthStrategy(self.client_id, self.client_secret)
412412

413-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
413+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
414414
return OAuthWebAuth(
415415
github, self.client_id, self.client_secret, self.code, self.redirect_uri
416416
)
@@ -423,5 +423,5 @@ class OAuthDeviceAuthStrategy(BaseAuthStrategy):
423423
client_id: str
424424
on_verification: Callable
425425

426-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
426+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
427427
return OAuthDeviceAuth(github, self.client_id, self.on_verification)

githubkit/auth/token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .base import BaseAuthStrategy
77

88
if TYPE_CHECKING:
9-
from githubkit import GitHub
9+
from githubkit import GitHubCore
1010

1111

1212
@dataclass(slots=True)
@@ -31,5 +31,5 @@ class TokenAuthStrategy(BaseAuthStrategy):
3131

3232
token: str
3333

34-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
34+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
3535
return TokenAuth(self.token)

githubkit/auth/unauth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from .base import BaseAuthStrategy
66

77
if TYPE_CHECKING:
8-
from githubkit import GitHub
8+
from githubkit import GitHubCore
99

1010

1111
class UnauthAuthStrategy(BaseAuthStrategy):
1212
"""Unauthenticated GitHubKit"""
1313

14-
def get_auth_flow(self, github: "GitHub") -> httpx.Auth:
14+
def get_auth_flow(self, github: "GitHubCore") -> httpx.Auth:
1515
return httpx.Auth()

githubkit/core.py

Lines changed: 7 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@
2222
import httpx
2323

2424
from .response import Response
25-
from .rest import RestNamespace
26-
from .paginator import Paginator
2725
from .exception import RequestFailed
2826
from .config import Config, get_config
2927
from .auth import BaseAuthStrategy, TokenAuthStrategy, UnauthAuthStrategy
30-
from .graphql import GraphQLResponse, build_graphql_request, parse_graphql_response
3128
from .typing import (
3229
URLTypes,
3330
CookieTypes,
@@ -39,23 +36,13 @@
3936

4037
T = TypeVar("T")
4138
A = TypeVar("A", bound="BaseAuthStrategy")
42-
A_o = TypeVar("A_o", bound="BaseAuthStrategy")
4339

44-
CP = ParamSpec("CP")
45-
CT = TypeVar("CT")
46-
RT = TypeVar("RT")
4740

48-
R = Union[
49-
Callable[CP, Response[List[RT]]],
50-
Callable[CP, Awaitable[Response[List[RT]]]],
51-
]
52-
53-
54-
class GitHub(Generic[A]):
41+
class GitHubCore(Generic[A]):
5542
# none auth with config
5643
@overload
5744
def __init__(
58-
self: "GitHub[UnauthAuthStrategy]",
45+
self: "GitHubCore[UnauthAuthStrategy]",
5946
auth: None = None,
6047
*,
6148
config: Config,
@@ -65,7 +52,7 @@ def __init__(
6552
# token auth with config
6653
@overload
6754
def __init__(
68-
self: "GitHub[TokenAuthStrategy]",
55+
self: "GitHubCore[TokenAuthStrategy]",
6956
auth: str,
7057
*,
7158
config: Config,
@@ -75,7 +62,7 @@ def __init__(
7562
# other auth strategies with config
7663
@overload
7764
def __init__(
78-
self: "GitHub[A]",
65+
self: "GitHubCore[A]",
7966
auth: A,
8067
*,
8168
config: Config,
@@ -85,7 +72,7 @@ def __init__(
8572
# none auth without config
8673
@overload
8774
def __init__(
88-
self: "GitHub[UnauthAuthStrategy]",
75+
self: "GitHubCore[UnauthAuthStrategy]",
8976
auth: None = None,
9077
*,
9178
base_url: Optional[Union[str, httpx.URL]] = None,
@@ -100,7 +87,7 @@ def __init__(
10087
# token auth without config
10188
@overload
10289
def __init__(
103-
self: "GitHub[TokenAuthStrategy]",
90+
self: "GitHubCore[TokenAuthStrategy]",
10491
auth: str,
10592
*,
10693
base_url: Optional[Union[str, httpx.URL]] = None,
@@ -115,7 +102,7 @@ def __init__(
115102
# other auth strategies without config
116103
@overload
117104
def __init__(
118-
self: "GitHub[A]",
105+
self: "GitHubCore[A]",
119106
auth: A,
120107
*,
121108
base_url: Optional[Union[str, httpx.URL]] = None,
@@ -357,71 +344,3 @@ async def arequest(
357344
cookies=cookies,
358345
)
359346
return self._check(raw_resp, response_model, error_models)
360-
361-
# copy github instance with other auth
362-
def with_auth(self, auth: A_o) -> "GitHub[A_o]":
363-
return GitHub(auth=auth, config=self.config.copy())
364-
365-
# high level methods
366-
367-
# rest api
368-
@cached_property
369-
def rest(self) -> RestNamespace:
370-
return RestNamespace(self)
371-
372-
# graphql
373-
def graphql(
374-
self, query: str, variables: Optional[Dict[str, Any]] = None
375-
) -> Dict[str, Any]:
376-
json = build_graphql_request(query, variables)
377-
378-
return parse_graphql_response(
379-
self.request("POST", "/graphql", json=json, response_model=GraphQLResponse)
380-
)
381-
382-
async def async_graphql(
383-
self, query: str, variables: Optional[Dict[str, Any]] = None
384-
) -> Dict[str, Any]:
385-
json = build_graphql_request(query, variables)
386-
387-
return parse_graphql_response(
388-
await self.arequest(
389-
"POST", "/graphql", json=json, response_model=GraphQLResponse
390-
)
391-
)
392-
393-
# rest pagination
394-
@overload
395-
@staticmethod
396-
def paginate(
397-
request: R[CP, RT],
398-
page: int = 1,
399-
per_page: int = 100,
400-
map_func: None = None,
401-
*args: CP.args,
402-
**kwargs: CP.kwargs,
403-
) -> Paginator[RT]:
404-
...
405-
406-
@overload
407-
@staticmethod
408-
def paginate(
409-
request: R[CP, CT],
410-
page: int = 1,
411-
per_page: int = 100,
412-
map_func: Callable[[Response[List[CT]]], List[RT]] = ..., # type: ignore
413-
*args: CP.args,
414-
**kwargs: CP.kwargs,
415-
) -> Paginator[RT]:
416-
...
417-
418-
@staticmethod
419-
def paginate(
420-
request: R[CP, CT],
421-
page: int = 1,
422-
per_page: int = 100,
423-
map_func: Optional[Callable[[Response[List[CT]]], List[RT]]] = None,
424-
*args: CP.args,
425-
**kwargs: CP.kwargs,
426-
) -> Paginator[RT]:
427-
return Paginator(request, page, per_page, map_func, *args, **kwargs) # type: ignore

0 commit comments

Comments
 (0)