|
| 1 | +from typing import Optional, Iterable, Union |
| 2 | + |
| 3 | +from crowdin_api.api_resources.abstract.resources import BaseResource |
| 4 | +from crowdin_api.api_resources.fields.enums import FieldEntity, FieldType |
| 5 | +from crowdin_api.api_resources.fields.types import ( |
| 6 | + ListFieldConfig, |
| 7 | + NumberFieldConfig, |
| 8 | + OtherFieldConfig, |
| 9 | + FieldPatchRequest, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class FieldsResource(BaseResource): |
| 14 | + """ |
| 15 | + Resource for Fields. |
| 16 | +
|
| 17 | + Link to documentation: |
| 18 | + https://developer.crowdin.com/enterprise/api/v2/#tag/Fields |
| 19 | + """ |
| 20 | + |
| 21 | + def get_fields_path(self, fieldId: Optional[int] = None): |
| 22 | + if fieldId is None: |
| 23 | + return "fields" |
| 24 | + return f"fields/{fieldId}" |
| 25 | + |
| 26 | + def list_fields( |
| 27 | + self, |
| 28 | + search: Optional[str] = None, |
| 29 | + entity: Optional[FieldEntity] = None, |
| 30 | + type: Optional[FieldType] = None, |
| 31 | + limit: Optional[int] = None, |
| 32 | + offset: Optional[int] = None, |
| 33 | + ): |
| 34 | + """ |
| 35 | + List Fields |
| 36 | +
|
| 37 | + Link to documentation: |
| 38 | + https://developer.crowdin.com/enterprise/api/v2/#operation/api.fields.getMany |
| 39 | + """ |
| 40 | + params = {"search": search, "entity": entity, "type": type} |
| 41 | + params.update(self.get_page_params(limit=limit, offset=offset)) |
| 42 | + |
| 43 | + return self._get_entire_data( |
| 44 | + method="get", path=self.get_fields_path(), params=params |
| 45 | + ) |
| 46 | + |
| 47 | + def add_field( |
| 48 | + self, |
| 49 | + name: str, |
| 50 | + slug: str, |
| 51 | + type: FieldType, |
| 52 | + entities: Iterable[FieldEntity], |
| 53 | + description: Optional[str] = None, |
| 54 | + config: Optional[ |
| 55 | + Union[ListFieldConfig, NumberFieldConfig, OtherFieldConfig] |
| 56 | + ] = None, |
| 57 | + ): |
| 58 | + """ |
| 59 | + Add Field |
| 60 | +
|
| 61 | + Link to documentation: |
| 62 | + https://developer.crowdin.com/enterprise/api/v2/#operation/api.fields.post |
| 63 | + """ |
| 64 | + data = { |
| 65 | + "name": name, |
| 66 | + "slug": slug, |
| 67 | + "type": type, |
| 68 | + "entities": entities, |
| 69 | + "description": description, |
| 70 | + "config": config, |
| 71 | + } |
| 72 | + |
| 73 | + return self.requester.request( |
| 74 | + method="post", path=self.get_fields_path(), request_data=data |
| 75 | + ) |
| 76 | + |
| 77 | + def get_field(self, fieldId: int): |
| 78 | + """ |
| 79 | + Get Field |
| 80 | +
|
| 81 | + Link to documentaion: |
| 82 | + https://developer.crowdin.com/enterprise/api/v2/#operation/api.fields.get |
| 83 | + """ |
| 84 | + |
| 85 | + return self.requester.request( |
| 86 | + method="get", path=self.get_fields_path(fieldId=fieldId) |
| 87 | + ) |
| 88 | + |
| 89 | + def delete_field(self, fieldId: int): |
| 90 | + """ |
| 91 | + Delete Field |
| 92 | +
|
| 93 | + Link to documetation: |
| 94 | + https://developer.crowdin.com/enterprise/api/v2/#operation/api.fields.delete |
| 95 | + """ |
| 96 | + |
| 97 | + return self.requester.request( |
| 98 | + method="delete", path=self.get_fields_path(fieldId=fieldId) |
| 99 | + ) |
| 100 | + |
| 101 | + def edit_field(self, fieldId: int, data: Iterable[FieldPatchRequest]): |
| 102 | + """ |
| 103 | + Edit Field |
| 104 | +
|
| 105 | + Link to documentation: |
| 106 | + https://developer.crowdin.com/enterprise/api/v2/#operation/api.fields.patch |
| 107 | + """ |
| 108 | + |
| 109 | + return self.requester.request( |
| 110 | + method="patch", |
| 111 | + path=self.get_fields_path(fieldId=fieldId), |
| 112 | + request_data=data, |
| 113 | + ) |
0 commit comments