gitcode-api 1.1.0__py3-none-any.whl → 1.1.2__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.
@@ -5,7 +5,7 @@ payload cleanup, and response parsing for both sync and async clients.
5
5
  """
6
6
 
7
7
  import os
8
- from typing import Any, Dict, Optional, Tuple, Union
8
+ from typing import Any, Callable, Dict, Optional, Tuple, Union
9
9
  from urllib.parse import quote
10
10
 
11
11
  import httpx
@@ -30,6 +30,7 @@ class BaseGitCodeClient:
30
30
  :param repo: Default repository name for repository-scoped calls.
31
31
  :param base_url: Base URL for the GitCode REST API.
32
32
  :param timeout: Request timeout in seconds.
33
+ :param decrypt: Optional decryption function for encrypted access token.
33
34
  """
34
35
 
35
36
  def __init__(
@@ -40,20 +41,23 @@ class BaseGitCodeClient:
40
41
  repo: Optional[str] = None,
41
42
  base_url: str = DEFAULT_BASE_URL,
42
43
  timeout: Optional[float] = None,
44
+ decrypt: Optional[Callable] = None,
43
45
  ) -> None:
44
46
  """Store client configuration and resolve authentication."""
45
- self.api_key = self._resolve_api_key(api_key)
47
+ self.api_key = self._resolve_api_key(api_key, decrypt)
46
48
  self.owner = owner
47
49
  self.repo = repo
48
50
  self.base_url = base_url.rstrip("/")
49
51
  self.timeout = timeout if timeout is not None else DEFAULT_TIMEOUT
50
52
 
51
- def _resolve_api_key(self, api_key: Optional[str]) -> str:
53
+ def _resolve_api_key(self, api_key: Optional[str], decrypt: Optional[Callable] = None) -> str:
52
54
  """Resolve the access token from an argument or environment variable."""
53
55
  token = api_key or os.getenv(DEFAULT_TOKEN_ENV)
56
+ if callable(decrypt):
57
+ token = decrypt(token)
54
58
  if not token:
55
59
  raise GitCodeConfigurationError("No API key provided. Pass api_key=... or set GITCODE_ACCESS_TOKEN.")
56
- return token
60
+ return str(token)
57
61
 
58
62
  def _resolve_repo_context(
59
63
  self,
@@ -187,6 +191,7 @@ class SyncAPIClient(BaseGitCodeClient):
187
191
  :param base_url: Base URL for the GitCode REST API.
188
192
  :param timeout: Request timeout in seconds.
189
193
  :param http_client: Optional pre-configured ``httpx.Client`` instance.
194
+ :param decrypt: Optional decryption function for encrypted access token.
190
195
  """
191
196
 
192
197
  def __init__(
@@ -198,9 +203,10 @@ class SyncAPIClient(BaseGitCodeClient):
198
203
  base_url: str = DEFAULT_BASE_URL,
199
204
  timeout: Optional[float] = None,
200
205
  http_client: Optional[httpx.Client] = None,
206
+ decrypt: Optional[Callable] = None,
201
207
  ) -> None:
202
208
  """Create or reuse an ``httpx.Client`` for synchronous requests."""
203
- super().__init__(api_key=api_key, owner=owner, repo=repo, base_url=base_url, timeout=timeout)
209
+ super().__init__(api_key=api_key, owner=owner, repo=repo, base_url=base_url, timeout=timeout, decrypt=decrypt)
204
210
  self._owns_client = http_client is None
205
211
  self._client = http_client or httpx.Client(timeout=self.timeout)
206
212
 
@@ -238,9 +244,8 @@ class SyncAPIClient(BaseGitCodeClient):
238
244
  return self._parse_response(response, raw=raw)
239
245
 
240
246
  def close(self) -> None:
241
- """Close the underlying HTTP client if this instance created it."""
242
- if self._owns_client:
243
- self._client.close()
247
+ """Close the underlying HTTP client."""
248
+ self._client.close()
244
249
 
245
250
  def __enter__(self) -> "SyncAPIClient":
246
251
  """Enter a context manager and return the client instance."""
@@ -260,6 +265,7 @@ class AsyncAPIClient(BaseGitCodeClient):
260
265
  :param base_url: Base URL for the GitCode REST API.
261
266
  :param timeout: Request timeout in seconds.
262
267
  :param http_client: Optional pre-configured ``httpx.AsyncClient`` instance.
268
+ :param decrypt: Optional decryption function for encrypted access token.
263
269
  """
264
270
 
265
271
  def __init__(
@@ -271,9 +277,10 @@ class AsyncAPIClient(BaseGitCodeClient):
271
277
  base_url: str = DEFAULT_BASE_URL,
272
278
  timeout: Optional[float] = None,
273
279
  http_client: Optional[httpx.AsyncClient] = None,
280
+ decrypt: Optional[Callable] = None,
274
281
  ) -> None:
275
282
  """Create or reuse an ``httpx.AsyncClient`` for asynchronous requests."""
276
- super().__init__(api_key=api_key, owner=owner, repo=repo, base_url=base_url, timeout=timeout)
283
+ super().__init__(api_key=api_key, owner=owner, repo=repo, base_url=base_url, timeout=timeout, decrypt=decrypt)
277
284
  self._owns_client = http_client is None
278
285
  self._client = http_client or httpx.AsyncClient(timeout=self.timeout)
279
286
 
@@ -311,9 +318,8 @@ class AsyncAPIClient(BaseGitCodeClient):
311
318
  return self._parse_response(response, raw=raw)
312
319
 
313
320
  async def close(self) -> None:
314
- """Close the underlying async HTTP client if this instance created it."""
315
- if self._owns_client:
316
- await self._client.aclose()
321
+ """Close the underlying async HTTP client."""
322
+ await self._client.aclose()
317
323
 
318
324
  async def __aenter__(self) -> "AsyncAPIClient":
319
325
  """Enter an async context manager and return the client instance."""
gitcode_api/_client.py CHANGED
@@ -4,7 +4,7 @@ These client classes expose grouped resource helpers that mirror the
4
4
  published GitCode REST API documentation.
5
5
  """
6
6
 
7
- from typing import Optional
7
+ from typing import Callable, Optional
8
8
 
9
9
  import httpx
10
10
 
@@ -54,6 +54,7 @@ class GitCode(SyncAPIClient):
54
54
  :param base_url: Base URL for the GitCode REST API.
55
55
  :param timeout: Request timeout in seconds.
56
56
  :param http_client: Optional pre-configured ``httpx.Client`` instance.
57
+ :param decrypt: Optional decryption function for encrypted access token.
57
58
  """
58
59
 
59
60
  repos: ReposResource
@@ -113,6 +114,7 @@ class GitCode(SyncAPIClient):
113
114
  base_url: str = DEFAULT_BASE_URL,
114
115
  timeout: Optional[float] = None,
115
116
  http_client: Optional[httpx.Client] = None,
117
+ decrypt: Optional[Callable] = None,
116
118
  ) -> None:
117
119
  """Create a synchronous client and attach resource groups."""
118
120
  super().__init__(
@@ -122,6 +124,7 @@ class GitCode(SyncAPIClient):
122
124
  base_url=base_url,
123
125
  timeout=timeout,
124
126
  http_client=http_client,
127
+ decrypt=decrypt,
125
128
  )
126
129
  self.repos = ReposResource(self)
127
130
  self.contents = RepoContentsResource(self)
@@ -140,6 +143,9 @@ class GitCode(SyncAPIClient):
140
143
  self.search = SearchResource(self)
141
144
  self.oauth = OAuthResource(self)
142
145
 
146
+ def __enter__(self) -> "GitCode":
147
+ return self
148
+
143
149
 
144
150
  class AsyncGitCode(AsyncAPIClient):
145
151
  """Asynchronous GitCode API client.
@@ -150,6 +156,7 @@ class AsyncGitCode(AsyncAPIClient):
150
156
  :param base_url: Base URL for the GitCode REST API.
151
157
  :param timeout: Request timeout in seconds.
152
158
  :param http_client: Optional pre-configured ``httpx.AsyncClient`` instance.
159
+ :param decrypt: Optional decryption function for encrypted access token.
153
160
  """
154
161
 
155
162
  repos: AsyncReposResource
@@ -209,6 +216,7 @@ class AsyncGitCode(AsyncAPIClient):
209
216
  base_url: str = DEFAULT_BASE_URL,
210
217
  timeout: Optional[float] = None,
211
218
  http_client: Optional[httpx.AsyncClient] = None,
219
+ decrypt: Optional[Callable] = None,
212
220
  ) -> None:
213
221
  """Create an asynchronous client and attach resource groups."""
214
222
  super().__init__(
@@ -218,6 +226,7 @@ class AsyncGitCode(AsyncAPIClient):
218
226
  base_url=base_url,
219
227
  timeout=timeout,
220
228
  http_client=http_client,
229
+ decrypt=decrypt,
221
230
  )
222
231
  self.repos = AsyncReposResource(self)
223
232
  self.contents = AsyncRepoContentsResource(self)
@@ -235,3 +244,6 @@ class AsyncGitCode(AsyncAPIClient):
235
244
  self.orgs = AsyncOrgsResource(self)
236
245
  self.search = AsyncSearchResource(self)
237
246
  self.oauth = AsyncOAuthResource(self)
247
+
248
+ async def __aenter__(self) -> "AsyncGitCode":
249
+ return self