api.models
Attributes
Exceptions
Base class for all API exceptions. |
|
Base class for all API exceptions. |
Classes
Base class for protocol classes. |
|
A single instance of an item of a specific endpoint. |
|
An API endpoint. |
|
A collection of all available API endpoints. |
|
This is a Dummy, because morepath requires a model for linking. |
|
Module Contents
- class api.models.PaginationWithById[M: sqlalchemy.orm.DeclarativeBase, IdT: uuid.UUID | str | int][source]
Bases:
ProtocolBase class for protocol classes.
Protocol classes are defined as:
class Proto(Protocol): def meth(self) -> int: ...
Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing).
For example:
class C: def meth(self) -> int: return 0 def func(x: Proto) -> int: return x.meth() func(C()) # Passes static type check
See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:
class GenProto[T](Protocol): def meth(self) -> T: ...
- exception api.models.ApiException(message: str = 'Internal Server Error', status_code: int = 500, headers: dict[str, str] | None = None)[source]
Bases:
ExceptionBase class for all API exceptions.
Mainly used to ensure that all exceptions regarding the API are rendered with the correct content type.
- exception api.models.ApiInvalidParamException(message: str = 'Invalid Parameter', status_code: int = 400)[source]
Bases:
ApiExceptionBase class for all API exceptions.
Mainly used to ensure that all exceptions regarding the API are rendered with the correct content type.
- class api.models.ApiEndpointItem[M: sqlalchemy.orm.DeclarativeBase, IdT: onegov.core.collection.PKType](request: onegov.core.request.CoreRequest, endpoint: str, id: str)[source]
A single instance of an item of a specific endpoint.
Passes all functionality to the specific API endpoint and is mainly used for routing.
- property api_endpoint: ApiEndpoint[M, IdT] | None[source]
- class api.models.ApiEndpoint[M: sqlalchemy.orm.DeclarativeBase, IdT: onegov.core.collection.PKType](request: onegov.core.request.CoreRequest, extra_parameters: dict[str, list[str]] | None = None, page: int | None = None, batch_size: int | None = None)[source]
An API endpoint.
API endpoints wrap collection and do some filter mapping.
To add a new endpoint, inherit from this class and provide the missing functions and properties at the bottom. Note that the collection is expected to be to provide the functionality of
onegov.core.collection.Pagination.- property filters: collections.abc.Mapping[str, collections.abc.Collection[str] | str | None][source]
A mapping of the available filter params to their corresponding description or a collection of possible values. If possible values are specified it is assumed that the filter can be specfied multiple times.
The description is optional and should only be used for non-trivial filters that don’t just accept arbitrary strings.
- for_page(page: int | None, batch_size: int | None) Self | None[source]
Return a new endpoint instance with the given page while keeping the current filters.
- for_filter(**filters: list[str]) Self[source]
Return a new endpoint instance with the given filters while discarding the current filters and page.
- for_item(item: None) None[source]
- for_item(item: M) ApiEndpointItem[M, IdT]
Return a new endpoint item instance with the given item.
- for_item_id(item_id: None) None[source]
- for_item_id(item_id: IdT) ApiEndpointItem[M, IdT]
Return a new endpoint item instance with the given item id.
- get_filter[T, DefaultT, EmptyT](name: str, default: DefaultT = None, empty: EmptyT = None, coerce: collections.abc.Callable[[str], T] = str) T | DefaultT | EmptyT[source]
Returns the scalar filter value with the given name.
- property batch: dict[ApiEndpointItem[M, IdT], M][source]
A dictionary with endpoint item instances and their titles.
- abstractmethod item_data(item: M) dict[str, Any][source]
Return the data properties of the collection item as a dictionary.
For example:
{ 'name': 'Paul', 'age': 40 }
- item_form_class(item: M) type[onegov.form.Form] | None[source]
Return the form class specific to this endpoint item.
Cannot be combined with ApiEndpoint.form_class, if that attribute is set to something other than None, then this method will never be called.
Set the attribute when all items share the same form_class and override this method when each item has its own form_class
- abstractmethod item_links(item: M) dict[str, Any][source]
Return the link properties of the collection item as a dictionary. Links can either be string or a linkable object.
For example:
{ 'website': 'https://onegov.ch', 'friends': FriendsApiEndpoint(app).for_item(paul), 'home': ApiEndpointCollection(app) }
- form(item: M | None, request: onegov.core.request.CoreRequest) onegov.form.Form | None[source]
Return a form for editing items of this collection.
- abstractmethod apply_changes(item: M, form: Any) dict[str, Any] | None[source]
Apply the changes to the item based on the given form data.
May optionally return a valid Collection+JSON payload which will be included in the response.
- property collection: PaginationWithById[M, Any][source]
- Abstractmethod:
An instance of the collection with filters and page set.
- class api.models.ApiEndpointCollection(request: onegov.core.request.CoreRequest)[source]
A collection of all available API endpoints.
- property endpoints: dict[str, ApiEndpoint[Any, Any]][source]
- get_endpoint(name: str, batch_size: int | None, page: int = 0, extra_parameters: dict[str, list[str]] | None = None) ApiEndpoint[Any, Any] | None[source]
- class api.models.AuthEndpoint(app: onegov.core.Framework)[source]
This is a Dummy, because morepath requires a model for linking.