codeapi-client 0.4.1__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.
@@ -0,0 +1,211 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+
5
+ import httpx
6
+
7
+ from codeapi.client._base import ClientBase
8
+ from codeapi.types import CodeInfo, CodeType, CodeZip, Job, JsonData
9
+
10
+
11
+ class AsyncCodeClient:
12
+ def __init__(self, client: ClientBase):
13
+ self._client = client
14
+
15
+ async def upload(
16
+ self,
17
+ code_zip: CodeZip,
18
+ code_name: str,
19
+ code_type: CodeType,
20
+ metadata: JsonData | dict | None = None,
21
+ ) -> str:
22
+ """Upload code.
23
+
24
+ Args:
25
+ code_zip (CodeZip): The code zip.
26
+ code_name (str): The name of the code.
27
+ code_type (CodeType): The type of the code.
28
+ metadata (JsonData | dict | None): The JSON metadata of the code.
29
+
30
+ Returns:
31
+ str: The code ID.
32
+
33
+ Raises:
34
+ HTTPException: If the request fails.
35
+ """
36
+ url = f"{self._client.base_url}/code/upload"
37
+
38
+ files = self._client._prepare_files(code_zip=code_zip, metadata=metadata)
39
+ data = {"code_name": code_name, "code_type": code_type}
40
+
41
+ async with httpx.AsyncClient() as client:
42
+ try:
43
+ response = await client.post(
44
+ url, headers=self._client.api_key_header, files=files, data=data
45
+ )
46
+ response.raise_for_status()
47
+ return str(response.json()).strip()
48
+ except httpx.HTTPStatusError as e:
49
+ raise self._client._get_http_exception(httpx_error=e)
50
+
51
+ async def download(
52
+ self, code_id: str, zip_path: Path | str | None = None
53
+ ) -> CodeZip:
54
+ """Download code and optionally saves it to a file.
55
+
56
+ Args:
57
+ code_id (str): The code ID.
58
+ zip_path (Path | str | None): The path to save the code zip file to.
59
+
60
+ Returns:
61
+ CodeZip: The downloaded code zip.
62
+
63
+ Raises:
64
+ HTTPException: If the request fails.
65
+ """
66
+ url = f"{self._client.base_url}/code/{code_id}/download"
67
+
68
+ async with httpx.AsyncClient() as client:
69
+ try:
70
+ async with client.stream(
71
+ "GET", url, headers=self._client.api_key_header
72
+ ) as response:
73
+ zip_bytes = await self._client._handle_stream_download_async(
74
+ response, zip_path
75
+ )
76
+ except httpx.HTTPStatusError as e:
77
+ raise self._client._get_http_exception(httpx_error=e)
78
+
79
+ return CodeZip(zip_bytes=zip_bytes)
80
+
81
+ async def delete(self, code_id: str) -> str:
82
+ """Deletes code by code_id.
83
+
84
+ Args:
85
+ code_id (str): The code ID.
86
+
87
+ Returns:
88
+ str: Confirmation message.
89
+
90
+ Raises:
91
+ HTTPException: If the request fails.
92
+ """
93
+ url = f"{self._client.base_url}/code/{code_id}"
94
+ async with httpx.AsyncClient() as client:
95
+ try:
96
+ response = await client.delete(url, headers=self._client.api_key_header)
97
+ response.raise_for_status()
98
+ return f"Code with id={code_id} deleted"
99
+ except httpx.HTTPStatusError as e:
100
+ raise self._client._get_http_exception(httpx_error=e)
101
+
102
+ async def get_info(self, code_id: str) -> CodeInfo:
103
+ """Gets the code info.
104
+
105
+ Args:
106
+ code_id (str): The code ID.
107
+
108
+ Returns:
109
+ CodeInfo: The code info.
110
+
111
+ Raises:
112
+ HTTPException: If the request fails.
113
+ """
114
+ url = f"{self._client.base_url}/code/{code_id}/info"
115
+ async with httpx.AsyncClient() as client:
116
+ try:
117
+ response = await client.get(url, headers=self._client.api_key_header)
118
+ response.raise_for_status()
119
+ return CodeInfo(**response.json())
120
+ except httpx.HTTPStatusError as e:
121
+ raise self._client._get_http_exception(httpx_error=e)
122
+
123
+ async def list_info(self, code_type: CodeType | None = None) -> list[CodeInfo]:
124
+ """Gets list of code infos.
125
+
126
+ Args:
127
+ code_type (CodeType | None): The type of code to list.
128
+
129
+ Returns:
130
+ list[CodeInfo]: The list of code infos.
131
+
132
+ Raises:
133
+ HTTPException: If the request fails.
134
+ """
135
+ url = f"{self._client.base_url}/code/info"
136
+ async with httpx.AsyncClient() as client:
137
+ try:
138
+ params = {"code_type": code_type} if code_type else None
139
+ response = await client.get(
140
+ url, headers=self._client.api_key_header, params=params
141
+ )
142
+ response.raise_for_status()
143
+ return [CodeInfo(**item) for item in response.json()]
144
+ except httpx.HTTPStatusError as e:
145
+ raise self._client._get_http_exception(httpx_error=e)
146
+
147
+ async def get_metadata(self, code_id: str) -> JsonData:
148
+ """Gets code metadata.
149
+
150
+ Args:
151
+ code_id (str): The ID of the code.
152
+
153
+ Returns:
154
+ JsonData: The metadata.
155
+
156
+ Raises:
157
+ HTTPException: If the request fails.
158
+ """
159
+ url = f"{self._client.base_url}/code/{code_id}/metadata"
160
+ async with httpx.AsyncClient() as client:
161
+ try:
162
+ response = await client.get(url, headers=self._client.api_key_header)
163
+ response.raise_for_status()
164
+ return JsonData(response.json())
165
+ except httpx.HTTPStatusError as e:
166
+ raise self._client._get_http_exception(httpx_error=e)
167
+
168
+ async def ingest(
169
+ self,
170
+ code_zip: CodeZip,
171
+ code_name: str,
172
+ code_type: CodeType,
173
+ metadata: JsonData | dict | None = None,
174
+ build_pexenv: bool = False,
175
+ pexenv_python: str | None = None,
176
+ ) -> Job:
177
+ """Starts a code ingestion job.
178
+
179
+ Args:
180
+ code_zip (CodeZip): The code zip.
181
+ code_name (str): The name of the code.
182
+ code_type (CodeType): The type of the code.
183
+ metadata (JsonData | dict | None): The JSON metadata of the code.
184
+ build_pexenv (bool): Whether to build the pex venv.
185
+ pexenv_python: (str | None): Python interpreter for the pex venv.
186
+
187
+ Returns:
188
+ Job: The code ingestion job.
189
+
190
+ Raises:
191
+ HTTPException: If the request fails.
192
+ """
193
+ url = f"{self._client.base_url}/jobs/code/ingest"
194
+
195
+ files = self._client._prepare_files(code_zip=code_zip, metadata=metadata)
196
+ data = {
197
+ "code_name": code_name,
198
+ "code_type": code_type,
199
+ "build_pexenv": build_pexenv,
200
+ "pexenv_python": pexenv_python,
201
+ }
202
+
203
+ async with httpx.AsyncClient() as client:
204
+ try:
205
+ response = await client.post(
206
+ url, headers=self._client.api_key_header, files=files, data=data
207
+ )
208
+ response.raise_for_status()
209
+ return Job(**response.json())
210
+ except httpx.HTTPStatusError as e:
211
+ raise self._client._get_http_exception(httpx_error=e)