Coverage for pephubclient/schemas/schema.py: 64%
101 statements
« prev ^ index » next coverage.py v7.15.3, created at 2026-08-04 18:15 +0000
« prev ^ index » next coverage.py v7.15.3, created at 2026-08-04 18:15 +0000
1import logging
2from typing import Union, List
4from pephubclient.helpers import RequestManager
5from pephubclient.constants import ResponseStatusCodes
6from pephubclient.schemas.constants import (
7 PEPHUB_SCHEMA_VERSION_URL,
8 PEPHUB_SCHEMA_VERSIONS_URL,
9 PEPHUB_SCHEMA_NEW_SCHEMA_URL,
10 PEPHUB_SCHEMA_NEW_VERSION_URL,
11 PEPHUB_SCHEMA_RECORD_URL,
12 LATEST_VERSION,
13)
14from pephubclient.exceptions import ResponseError
15from pephubclient.schemas.models import (
16 SchemaVersionResult,
17 NewSchemaVersionModel,
18 NewSchemaRecordModel,
19 UpdateSchemaRecordFields,
20 UpdateSchemaVersionFields,
21)
23_LOGGER = logging.getLogger("pephubclient")
26class PEPHubSchema(RequestManager):
27 """
28 Class for managing schemas in PEPhub and provides methods for
29 getting, creating, updating and removing schemas records and schema versions.
30 """
32 def __init__(self, jwt_data: str = None):
33 """
34 :param jwt_data: jwt token for authorization
35 """
37 self.__jwt_data = jwt_data
39 def get(
40 self, namespace: str, schema_name: str, version: str = LATEST_VERSION
41 ) -> dict:
42 """
43 Get schema value for specific schema version.
45 :param: namespace: namespace of schema
46 :param: schema_name: name of schema
47 :param: version: version of schema
49 :return: Schema object as dictionary
50 """
52 pephub_response = self.send_request(
53 method="GET",
54 url=PEPHUB_SCHEMA_VERSION_URL.format(
55 namespace=namespace, schema_name=schema_name, version=version
56 ),
57 headers=self.parse_header(self.__jwt_data),
58 cookies=None,
59 )
60 if pephub_response.status_code == ResponseStatusCodes.OK:
61 decoded_response = self.decode_response(pephub_response, output_json=True)
62 return decoded_response
64 if pephub_response.status_code == ResponseStatusCodes.NOT_EXIST:
65 raise ResponseError("Schema doesn't exist, or you are unauthorized.")
66 if pephub_response.status_code == ResponseStatusCodes.INTERNAL_ERROR:
67 raise ResponseError(
68 f"Internal server error. Unexpected return value. Error: {pephub_response.status_code}"
69 )
70 else:
71 raise ResponseError(
72 f"Unexpected Status code return. Error: {pephub_response.status_code}"
73 )
75 def get_versions(self, namespace: str, schema_name: str) -> SchemaVersionResult:
76 """
77 Get list of versions
79 :param namespace: Namespace of the schema record
80 :param schema_name: Name of the schema record
82 :return: {
83 pagination: PaginationResult
84 results: List[SchemaVersionAnnotation]
85 }
86 """
88 pephub_response = self.send_request(
89 method="GET",
90 url=PEPHUB_SCHEMA_VERSIONS_URL.format(
91 namespace=namespace, schema_name=schema_name
92 ),
93 headers=self.parse_header(self.__jwt_data),
94 cookies=None,
95 )
97 if pephub_response.status_code == ResponseStatusCodes.OK:
98 decoded_response = self.decode_response(pephub_response, output_json=True)
99 return SchemaVersionResult(**decoded_response)
101 if pephub_response.status_code == ResponseStatusCodes.NOT_EXIST:
102 raise ResponseError("Schema doesn't exist, or you are unauthorized.")
103 if pephub_response.status_code == ResponseStatusCodes.INTERNAL_ERROR:
104 raise ResponseError(
105 f"Internal server error. Unexpected return value. Error: {pephub_response.status_code}"
106 )
107 else:
108 raise ResponseError(
109 f"Unexpected Status code return. Error: {pephub_response.status_code}"
110 )
112 def create_schema(
113 self,
114 namespace: str,
115 schema_name: str,
116 schema_value: dict,
117 version: str = "1.0.0",
118 description: str = None,
119 maintainers: str = None,
120 contributors: str = None,
121 release_notes: str = None,
122 tags: Union[str, List[str], dict, None] = None,
123 lifecycle_stage: str = None,
124 private: bool = False,
125 ) -> None:
126 """
127 Create a new schema record + version in the database
129 :param namespace: Namespace of the schema
130 :param schema_name: Name of the schema record
131 :param schema_value: Schema value itself in dict format
132 :param version: First version of the schema
133 :param description: Schema description
134 :param maintainers: Schema maintainers
135 :param contributors: Schema contributors of current version
136 :param release_notes: Release notes for current version
137 :param tags: Tags of the current version. Can be str, list[str], or dict
138 :param lifecycle_stage: Stage of the schema record
139 :param private: Weather project should be public or private. Default: False (public)
141 :raise: ResponseError if status not 202.
142 :return: None
143 """
145 url = PEPHUB_SCHEMA_NEW_SCHEMA_URL.format(namespace=namespace)
146 request_body = NewSchemaRecordModel(
147 schema_name=schema_name,
148 description=description,
149 maintainers=maintainers,
150 lifecycle_stage=lifecycle_stage,
151 private=private,
152 contributors=contributors,
153 release_notes=release_notes,
154 tags=tags,
155 version=version,
156 schema_value=schema_value,
157 ).model_dump(exclude_none=True)
159 pephub_response = self.send_request(
160 method="POST",
161 url=url,
162 headers=self.parse_header(self.__jwt_data),
163 cookies=None,
164 json=request_body,
165 )
167 if pephub_response.status_code == ResponseStatusCodes.ACCEPTED:
168 _LOGGER.info(
169 f"Schema '{namespace}/{schema_name}:{version}' successfully created in PEPhub"
170 )
171 return None
173 elif pephub_response.status_code == ResponseStatusCodes.UNAUTHORIZED:
174 raise ResponseError(
175 "User not authorized or doesn't have permission to write to this namespace"
176 )
178 else:
179 raise ResponseError(
180 f"Unexpected error. Status code: {pephub_response.status_code}"
181 )
183 def add_version(
184 self,
185 namespace: str,
186 schema_name: str,
187 schema_value: dict,
188 version: str = "1.0.0",
189 contributors: str = None,
190 release_notes: str = None,
191 tags: Union[str, List[str], dict, None] = None,
192 ) -> None:
193 """
194 Add new version to the schema registry
196 :param namespace: Namespace of the schema
197 :param schema_name: Name of the schema record
198 :param schema_value: Schema value itself in dict format
199 :param version: First version of the schema
200 :param contributors: Schema contributors of current version
201 :param release_notes: Release notes for current version
202 :param tags: Tags of the current version. Can be str, list[str], or dict
204 :raise: ResponseError if status not 202.
205 :return: None
206 """
207 url = PEPHUB_SCHEMA_NEW_VERSION_URL.format(
208 namespace=namespace, schema_name=schema_name
209 )
210 request_body = NewSchemaVersionModel(
211 contributors=contributors,
212 release_notes=release_notes,
213 tags=tags,
214 version=version,
215 schema_value=schema_value,
216 ).model_dump(exclude_none=True, exclude_unset=True)
218 pephub_response = self.send_request(
219 method="POST",
220 url=url,
221 headers=self.parse_header(self.__jwt_data),
222 cookies=None,
223 json=request_body,
224 )
226 if pephub_response.status_code == ResponseStatusCodes.ACCEPTED:
227 _LOGGER.info(
228 f"Schema version '{namespace}/{schema_name}:{version}' successfully created in PEPhub"
229 )
230 return None
232 elif pephub_response.status_code == ResponseStatusCodes.UNAUTHORIZED:
233 raise ResponseError(
234 "User not authorized or doesn't have permission to write to this namespace"
235 )
237 else:
238 raise ResponseError(
239 f"Unexpected error. Status code: {pephub_response.status_code}"
240 )
242 def update_record(
243 self,
244 namespace: str,
245 schema_name: str,
246 update_fields: Union[dict, UpdateSchemaRecordFields],
247 ) -> None:
248 """
249 Update schema registry data
251 :param namespace: Namespace of the schema
252 :param schema_name: Name of the schema version
253 :param update_fields: dict or pydantic model UpdateSchemaRecordFields:
254 {
255 maintainers: str,
256 lifecycle_stage: str,
257 private: bool,
258 name: str,
259 description: str,
260 }
262 :raise: ResponseError if status not 202.
263 :return: None
264 """
266 if isinstance(update_fields, dict):
267 update_fields = UpdateSchemaRecordFields(**update_fields)
269 update_fields = update_fields.model_dump(exclude_none=True, exclude_unset=True)
271 url = PEPHUB_SCHEMA_RECORD_URL.format(
272 namespace=namespace, schema_name=schema_name
273 )
275 pephub_response = self.send_request(
276 method="PATCH",
277 url=url,
278 headers=self.parse_header(self.__jwt_data),
279 cookies=None,
280 json=update_fields,
281 )
283 if pephub_response.status_code == ResponseStatusCodes.ACCEPTED:
284 _LOGGER.info(
285 f"Schema record '{namespace}/{schema_name}' was updated successfully!"
286 )
287 return None
289 elif pephub_response.status_code == ResponseStatusCodes.NOT_EXIST:
290 raise ResponseError("Schema doesn't exist in PEPhub")
292 elif pephub_response.status_code == ResponseStatusCodes.UNAUTHORIZED:
293 raise ResponseError(
294 "User not authorized or doesn't have permission to write to this namespace"
295 )
297 else:
298 raise ResponseError(
299 f"Unexpected error. Status code: {pephub_response.status_code}"
300 )
302 def update_version(
303 self,
304 namespace: str,
305 schema_name: str,
306 version: str,
307 update_fields: Union[dict, UpdateSchemaVersionFields],
308 ) -> None:
309 """
310 Update released version of the schema.
312 :param namespace: Namespace of the schema
313 :param schema_name: Name of the schema version
314 :param version: Schema version
315 :param update_fields: dict or pydantic model UpdateSchemaVersionFields:
316 {
317 contributors: str,
318 schema_value: str,
319 release_notes: str,
320 }
322 :raise: ResponseError if status not 202.
323 :return: None
324 """
326 url = PEPHUB_SCHEMA_VERSION_URL.format(
327 namespace=namespace, schema_name=schema_name, version=version
328 )
330 if isinstance(update_fields, dict):
331 update_fields = UpdateSchemaVersionFields(**update_fields)
333 update_fields = update_fields.model_dump(exclude_unset=True, exclude_none=True)
335 pephub_response = self.send_request(
336 method="PATCH",
337 url=url,
338 headers=self.parse_header(self.__jwt_data),
339 cookies=None,
340 json=update_fields,
341 )
343 if pephub_response.status_code == ResponseStatusCodes.ACCEPTED:
344 _LOGGER.info(
345 f"Schema version '{namespace}/{schema_name}:{version}' was updated successfully!"
346 )
347 return None
349 elif pephub_response.status_code == ResponseStatusCodes.NOT_EXIST:
350 raise ResponseError("Schema doesn't exist in PEPhub")
352 elif pephub_response.status_code == ResponseStatusCodes.UNAUTHORIZED:
353 raise ResponseError(
354 "User not authorized or doesn't have permission to write to this namespace"
355 )
357 else:
358 raise ResponseError(
359 f"Unexpected error. Status code: {pephub_response.status_code}"
360 )
362 def delete_schema(self, namespace: str, schema_name: str) -> None:
363 """
364 Delete schema from the database
366 :param namespace: Namespace of the schema
367 :param schema_name: Name of the schema version
368 """
370 url = PEPHUB_SCHEMA_RECORD_URL.format(
371 namespace=namespace, schema_name=schema_name
372 )
374 pephub_response = self.send_request(
375 method="DELETE",
376 url=url,
377 headers=self.parse_header(self.__jwt_data),
378 cookies=None,
379 )
381 if pephub_response.status_code == ResponseStatusCodes.ACCEPTED:
382 _LOGGER.info(
383 f"Schema record '{namespace}/{schema_name}' was updated successfully!"
384 )
385 return None
387 elif pephub_response.status_code == ResponseStatusCodes.NOT_EXIST:
388 raise ResponseError("Schema doesn't exist in PEPhub")
390 elif pephub_response.status_code == ResponseStatusCodes.UNAUTHORIZED:
391 raise ResponseError(
392 "User not authorized or doesn't have permission to write to this namespace"
393 )
395 else:
396 raise ResponseError(
397 f"Unexpected error. Status code: {pephub_response.status_code}"
398 )
400 def delete_version(
401 self,
402 namespace: str,
403 schema_name: str,
404 version: str,
405 ) -> None:
406 """
407 Delete schema Version
409 :param namespace: Namespace of the schema
410 :param schema_name: Name of the schema
411 :param version: Schema version
413 :raise: ResponseError if status not 202.
414 :return: None
415 """
417 url = PEPHUB_SCHEMA_VERSION_URL.format(
418 namespace=namespace, schema_name=schema_name, version=version
419 )
421 pephub_response = self.send_request(
422 method="DELETE",
423 url=url,
424 headers=self.parse_header(self.__jwt_data),
425 cookies=None,
426 )
428 if pephub_response.status_code == ResponseStatusCodes.ACCEPTED:
429 _LOGGER.info(
430 f"Schema version '{namespace}/{schema_name}:{version}' was updated successfully!"
431 )
432 return None
434 elif pephub_response.status_code == ResponseStatusCodes.NOT_EXIST:
435 raise ResponseError("Schema doesn't exist in PEPhub")
437 elif pephub_response.status_code == ResponseStatusCodes.UNAUTHORIZED:
438 raise ResponseError(
439 "User not authorized or doesn't have permission to write to this namespace"
440 )
442 else:
443 raise ResponseError(
444 f"Unexpected error. Status code: {pephub_response.status_code}"
445 )