Coverage for src/lanraragi_api/entity/base.py: 71%
24 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-22 23:20 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-22 23:20 +0000
1from enum import Enum
2from typing import ClassVar
4from pydantic import BaseModel, ConfigDict, Field
7class Auth(str, Enum):
8 """Way the API key is sent to the server.
10 Attributes:
11 QUERY_PARAM: Send the key as the ``key`` query parameter.
12 AUTH_HEADER: Send the key as a base64-encoded ``Authorization`` bearer
13 header.
14 """
16 QUERY_PARAM = "query param"
17 AUTH_HEADER = "auth header"
20class DictLikeModel(BaseModel):
21 """Compatibility helper for endpoints that previously returned dict.
23 On top of the usual pydantic attribute access, models deriving from this
24 class can be read like a dictionary, so callers written against the older
25 ``dict`` based responses keep working.
26 """
28 def __getitem__(self, key: str):
29 """Return the value of the field named ``key``.
31 Args:
32 key: Name of the field to read.
34 Returns:
35 The value of the requested field.
37 Raises:
38 KeyError: If the model has no field named ``key``.
39 """
40 data = self.model_dump()
41 if key not in data:
42 raise KeyError(key)
43 return data[key]
45 def get(self, key: str, default: None = None):
46 """Return the value of the field named ``key``, or a default.
48 Args:
49 key: Name of the field to read.
50 default: Value returned when the field does not exist. Defaults to
51 None.
53 Returns:
54 The value of the requested field, or ``default``.
55 """
56 return self.model_dump().get(key, default)
58 def keys(self):
59 """Return the names of all fields of this model.
61 Returns:
62 A view of the field names.
63 """
64 return self.model_dump().keys()
66 def items(self):
67 """Return all fields of this model as name/value pairs.
69 Returns:
70 A view of ``(name, value)`` pairs.
71 """
72 return self.model_dump().items()
75class OperationResponse(DictLikeModel):
76 """Result of an operation endpoint.
78 Attributes:
79 operation: Name of the operation.
80 error: Error message, if the operation failed.
81 successMessage: Success message, if the server sent one.
82 success: 1 if the operation was successful, else 0.
83 """
85 model_config: ClassVar[ConfigDict] = ConfigDict(extra="allow")
87 operation: str = Field(...)
88 error: str | None = Field(default=None)
89 successMessage: str | None = Field(default=None)
90 success: int = Field(...)