gitcode-api 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gitcode_api/_models.py ADDED
@@ -0,0 +1,272 @@
1
+ """Lightweight wrappers and typed payload hints for GitCode API responses."""
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any, Iterator, List, Mapping, MutableMapping, TypedDict, TypeVar
5
+
6
+ JsonValue = Any
7
+
8
+
9
+ def _wrap_value(value: Any) -> Any:
10
+ """Recursively wrap nested dicts and lists in SDK helper objects."""
11
+ if isinstance(value, dict):
12
+ return APIObject(dict(value))
13
+ if isinstance(value, list):
14
+ return [_wrap_value(item) for item in value]
15
+ return value
16
+
17
+
18
+ @dataclass(slots=True)
19
+ class APIObject(Mapping[str, Any]):
20
+ """Dictionary-backed wrapper around GitCode JSON objects.
21
+
22
+ The object behaves like a mapping while also allowing attribute access for
23
+ top-level keys returned by the API.
24
+ """
25
+
26
+ data: MutableMapping[str, Any]
27
+
28
+ def __getitem__(self, key: str) -> Any:
29
+ """Return a wrapped item by key."""
30
+ return _wrap_value(self.data[key])
31
+
32
+ def __iter__(self) -> Iterator[str]:
33
+ """Iterate over keys in the underlying mapping."""
34
+ return iter(self.data)
35
+
36
+ def __len__(self) -> int:
37
+ """Return the number of top-level keys."""
38
+ return len(self.data)
39
+
40
+ def __getattr__(self, name: str) -> Any:
41
+ """Provide attribute-style access to top-level JSON keys."""
42
+ try:
43
+ return _wrap_value(self.data[name])
44
+ except KeyError as exc:
45
+ raise AttributeError(name) from exc
46
+
47
+ def get(self, key: str, default: Any = None) -> Any:
48
+ """Return a wrapped value with an optional default."""
49
+ return _wrap_value(self.data.get(key, default))
50
+
51
+ def to_dict(self) -> dict[str, Any]:
52
+ """Return a shallow ``dict`` copy of the underlying payload."""
53
+ return dict(self.data)
54
+
55
+
56
+ ModelT = TypeVar("ModelT", bound=APIObject)
57
+
58
+
59
+ def as_model(data: Mapping[str, Any], model_type: type[ModelT]) -> ModelT:
60
+ """Wrap a mapping in the requested SDK model type."""
61
+ return model_type(dict(data))
62
+
63
+
64
+ def as_model_list(data: List[Mapping[str, Any]], model_type: type[ModelT]) -> List[ModelT]:
65
+ """Wrap a list of mappings in the requested SDK model type."""
66
+ return [as_model(item, model_type) for item in data]
67
+
68
+
69
+ class RepositoryCreateParams(TypedDict, total=False):
70
+ """Typed fields accepted by repository creation endpoints."""
71
+
72
+ name: str
73
+ description: str
74
+ has_issues: bool
75
+ has_wiki: bool
76
+ auto_init: bool
77
+ gitignore_template: str
78
+ license_template: str
79
+ path: str
80
+ private: bool
81
+ public: int
82
+ default_branch: str
83
+ homepage: str
84
+ can_comment: bool
85
+
86
+
87
+ class IssueCreateParams(TypedDict, total=False):
88
+ """Typed fields accepted by issue creation endpoints."""
89
+
90
+ title: str
91
+ body: str
92
+ assignee: str
93
+ labels: List[str]
94
+ milestone: int | str
95
+
96
+
97
+ class PullRequestCreateParams(TypedDict, total=False):
98
+ """Typed fields accepted by pull request creation endpoints."""
99
+
100
+ title: str
101
+ body: str
102
+ head: str
103
+ base: str
104
+ assignees: List[str]
105
+ testers: List[str]
106
+ labels: List[str]
107
+ draft: bool
108
+
109
+
110
+ class WebhookCreateParams(TypedDict, total=False):
111
+ """Typed fields accepted by webhook creation endpoints."""
112
+
113
+ url: str
114
+ encryption_type: int
115
+ password: str
116
+ push_events: bool
117
+ tag_push_events: bool
118
+ issues_events: bool
119
+ note_events: bool
120
+ merge_requests_events: bool
121
+
122
+
123
+ class User(APIObject):
124
+ """User profile payload returned by user-related endpoints."""
125
+
126
+
127
+ class UserSummary(APIObject):
128
+ """Compact user payload returned by listing endpoints."""
129
+
130
+
131
+ class Repository(APIObject):
132
+ """Repository payload returned by repository endpoints."""
133
+
134
+
135
+ class Contributor(APIObject):
136
+ """Contributor payload returned by repository statistics endpoints."""
137
+
138
+
139
+ class ContentObject(APIObject):
140
+ """Repository content payload for files and directories."""
141
+
142
+
143
+ class CommitResult(APIObject):
144
+ """Commit result payload returned by content write operations."""
145
+
146
+
147
+ class Tree(APIObject):
148
+ """Git tree payload."""
149
+
150
+
151
+ class Blob(APIObject):
152
+ """Git blob payload."""
153
+
154
+
155
+ class Branch(APIObject):
156
+ """Repository branch payload."""
157
+
158
+
159
+ class ProtectedBranch(APIObject):
160
+ """Protected branch configuration payload."""
161
+
162
+
163
+ class Commit(APIObject):
164
+ """Commit payload."""
165
+
166
+
167
+ class CommitComparison(APIObject):
168
+ """Commit comparison payload."""
169
+
170
+
171
+ class CommitComment(APIObject):
172
+ """Commit comment payload."""
173
+
174
+
175
+ class Issue(APIObject):
176
+ """Issue payload."""
177
+
178
+
179
+ class IssueComment(APIObject):
180
+ """Issue comment payload."""
181
+
182
+
183
+ class IssueOperationLog(APIObject):
184
+ """Issue operation log payload."""
185
+
186
+
187
+ class PullRequest(APIObject):
188
+ """Pull request payload."""
189
+
190
+
191
+ class PullRequestFile(APIObject):
192
+ """Pull request file diff payload."""
193
+
194
+
195
+ class PullRequestComment(APIObject):
196
+ """Pull request comment payload."""
197
+
198
+
199
+ class PullRequestReview(APIObject):
200
+ """Pull request review payload."""
201
+
202
+
203
+ class PullRequestTest(APIObject):
204
+ """Pull request test request payload."""
205
+
206
+
207
+ class MergeResult(APIObject):
208
+ """Merge operation result payload."""
209
+
210
+
211
+ class MergeStatus(APIObject):
212
+ """Merge status payload."""
213
+
214
+
215
+ class Label(APIObject):
216
+ """Label payload."""
217
+
218
+
219
+ class Milestone(APIObject):
220
+ """Milestone payload."""
221
+
222
+
223
+ class RepoMember(APIObject):
224
+ """Repository member payload."""
225
+
226
+
227
+ class RepoMemberPermission(APIObject):
228
+ """Repository member permission payload."""
229
+
230
+
231
+ class Release(APIObject):
232
+ """Release payload."""
233
+
234
+
235
+ class Tag(APIObject):
236
+ """Tag payload."""
237
+
238
+
239
+ class ProtectedTag(APIObject):
240
+ """Protected tag configuration payload."""
241
+
242
+
243
+ class Webhook(APIObject):
244
+ """Webhook payload."""
245
+
246
+
247
+ class Organization(APIObject):
248
+ """Organization payload."""
249
+
250
+
251
+ class OrganizationMembership(APIObject):
252
+ """Organization membership payload."""
253
+
254
+
255
+ class EnterpriseMember(APIObject):
256
+ """Enterprise member payload."""
257
+
258
+
259
+ class Namespace(APIObject):
260
+ """Namespace payload."""
261
+
262
+
263
+ class Email(APIObject):
264
+ """Email payload for the authenticated user."""
265
+
266
+
267
+ class OAuthToken(APIObject):
268
+ """OAuth token payload."""
269
+
270
+
271
+ class SearchResult(APIObject):
272
+ """Search result payload."""
@@ -0,0 +1,77 @@
1
+ """Public resource group exports for the GitCode SDK."""
2
+
3
+ from .account import (
4
+ AsyncOAuthResource,
5
+ AsyncOrgsResource,
6
+ AsyncSearchResource,
7
+ AsyncUsersResource,
8
+ OAuthResource,
9
+ OrgsResource,
10
+ SearchResource,
11
+ UsersResource,
12
+ )
13
+ from .collaboration import (
14
+ AsyncIssuesResource,
15
+ AsyncLabelsResource,
16
+ AsyncMembersResource,
17
+ AsyncMilestonesResource,
18
+ AsyncPullsResource,
19
+ IssuesResource,
20
+ LabelsResource,
21
+ MembersResource,
22
+ MilestonesResource,
23
+ PullsResource,
24
+ )
25
+ from .misc import (
26
+ AsyncReleasesResource,
27
+ AsyncTagsResource,
28
+ AsyncWebhooksResource,
29
+ ReleasesResource,
30
+ TagsResource,
31
+ WebhooksResource,
32
+ )
33
+ from .repositories import (
34
+ AsyncBranchesResource,
35
+ AsyncCommitsResource,
36
+ AsyncRepoContentsResource,
37
+ AsyncReposResource,
38
+ BranchesResource,
39
+ CommitsResource,
40
+ RepoContentsResource,
41
+ ReposResource,
42
+ )
43
+
44
+ __all__ = [
45
+ "AsyncBranchesResource",
46
+ "AsyncCommitsResource",
47
+ "AsyncIssuesResource",
48
+ "AsyncLabelsResource",
49
+ "AsyncMembersResource",
50
+ "AsyncMilestonesResource",
51
+ "AsyncOAuthResource",
52
+ "AsyncOrgsResource",
53
+ "AsyncPullsResource",
54
+ "AsyncRepoContentsResource",
55
+ "AsyncReleasesResource",
56
+ "AsyncReposResource",
57
+ "AsyncSearchResource",
58
+ "AsyncTagsResource",
59
+ "AsyncUsersResource",
60
+ "AsyncWebhooksResource",
61
+ "BranchesResource",
62
+ "CommitsResource",
63
+ "IssuesResource",
64
+ "LabelsResource",
65
+ "MembersResource",
66
+ "MilestonesResource",
67
+ "OAuthResource",
68
+ "OrgsResource",
69
+ "PullsResource",
70
+ "RepoContentsResource",
71
+ "ReleasesResource",
72
+ "ReposResource",
73
+ "SearchResource",
74
+ "TagsResource",
75
+ "UsersResource",
76
+ "WebhooksResource",
77
+ ]
@@ -0,0 +1,75 @@
1
+ """Shared resource base classes for the GitCode SDK."""
2
+
3
+ from typing import Any, List
4
+
5
+ from .._base_client import AsyncAPIClient, SyncAPIClient
6
+ from .._models import APIObject, ModelT, as_model, as_model_list
7
+
8
+
9
+ class SyncResource:
10
+ """Base class for synchronous resource groups."""
11
+
12
+ def __init__(self, client: SyncAPIClient) -> None:
13
+ """Bind the resource to a synchronous API client."""
14
+ self._client = client
15
+
16
+ def _request(
17
+ self,
18
+ method: str,
19
+ path: str,
20
+ *,
21
+ params: dict[str, Any] | None = None,
22
+ json: Any = None,
23
+ data: dict[str, Any] | None = None,
24
+ raw: bool = False,
25
+ ) -> Any:
26
+ """Dispatch a low-level request through the owning client."""
27
+ return self._client.request(method, path, params=params, json=json, data=data, raw=raw)
28
+
29
+ def _model(self, method: str, path: str, model_type: type[ModelT], **kwargs: Any) -> ModelT:
30
+ """Send a request and wrap a JSON object in ``model_type``."""
31
+ data = self._request(method, path, **kwargs)
32
+ return as_model(data, model_type)
33
+
34
+ def _models(self, method: str, path: str, model_type: type[ModelT], **kwargs: Any) -> List[ModelT]:
35
+ """Send a request and wrap a JSON array in ``model_type`` instances."""
36
+ data = self._request(method, path, **kwargs)
37
+ return as_model_list(data, model_type)
38
+
39
+ def _maybe_model(self, method: str, path: str, model_type: type[ModelT], **kwargs: Any) -> ModelT | APIObject:
40
+ """Wrap dict responses as models and scalar responses as ``APIObject``."""
41
+ data = self._request(method, path, **kwargs)
42
+ if isinstance(data, dict):
43
+ return as_model(data, model_type)
44
+ return APIObject({"value": data})
45
+
46
+
47
+ class AsyncResource:
48
+ """Base class for asynchronous resource groups."""
49
+
50
+ def __init__(self, client: AsyncAPIClient) -> None:
51
+ """Bind the resource to an asynchronous API client."""
52
+ self._client = client
53
+
54
+ async def _request(
55
+ self,
56
+ method: str,
57
+ path: str,
58
+ *,
59
+ params: dict[str, Any] | None = None,
60
+ json: Any = None,
61
+ data: dict[str, Any] | None = None,
62
+ raw: bool = False,
63
+ ) -> Any:
64
+ """Dispatch a low-level async request through the owning client."""
65
+ return await self._client.request(method, path, params=params, json=json, data=data, raw=raw)
66
+
67
+ async def _model(self, method: str, path: str, model_type: type[ModelT], **kwargs: Any) -> ModelT:
68
+ """Send a request and wrap a JSON object in ``model_type``."""
69
+ data = await self._request(method, path, **kwargs)
70
+ return as_model(data, model_type)
71
+
72
+ async def _models(self, method: str, path: str, model_type: type[ModelT], **kwargs: Any) -> List[ModelT]:
73
+ """Send a request and wrap a JSON array in ``model_type`` instances."""
74
+ data = await self._request(method, path, **kwargs)
75
+ return as_model_list(data, model_type)