api.models
Attributes
Exceptions
Base class for all API exceptions. |
|
Base class for all API exceptions. |
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. |
|
Base class used for declarative class definitions. |
Module Contents
- 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(request: onegov.core.request.CoreRequest, endpoint: str, id: str)[source]
Bases:
Generic[onegov.core.collection._M]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[onegov.core.collection._M] | None[source]
- class api.models.ApiEndpoint(request: onegov.core.request.CoreRequest, extra_parameters: dict[str, str | None] | None = None, page: int | None = None)[source]
Bases:
Generic[onegov.core.collection._M]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.- for_page(page: int | None) Self | None[source]
Return a new endpoint instance with the given page while keeping the current filters.
- for_filter(**filters: Any) 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: onegov.core.collection._M) ApiEndpointItem[onegov.core.collection._M]
Return a new endpoint item instance with the given item.
- for_item_id(item_id: None) None[source]
- for_item_id(item_id: Any) ApiEndpointItem[Any]
Return a new endpoint item instance with the given item id.
- get_filter(name: str, default: _DefaultT, empty: _EmptyT) str | _DefaultT | _EmptyT[source]
- get_filter(name: str, default: _DefaultT, empty: None = None) str | _DefaultT | None
- get_filter(name: str, default: None = None, *, empty: _EmptyT) str | _EmptyT | None
- get_filter(name: str, default: None = None, empty: None = None) str | None
Returns the filter value with the given name.
- by_id(id: onegov.core.collection.PKType) onegov.core.collection._M | None[source]
Return the item with the given ID from the collection.
- property batch: dict[ApiEndpointItem[onegov.core.collection._M], onegov.core.collection._M][source]
Returns a dictionary with endpoint item instances and their titles.
- abstractmethod item_data(item: onegov.core.collection._M) dict[str, Any][source]
Return the data properties of the collection item as a dictionary.
For example:
{ 'name': 'Paul', 'age': 40 }
- abstractmethod item_links(item: onegov.core.collection._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: onegov.core.collection._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: onegov.core.collection._M, form: Any) None[source]
Apply the changes to the item based on the given form data.
- class api.models.ApiEndpointCollection(request: onegov.core.request.CoreRequest)[source]
A collection of all available API endpoints.
- property endpoints: dict[str, ApiEndpoint[Any]][source]
- get_endpoint(name: str, page: int = 0, extra_parameters: dict[str, Any] | None = None) ApiEndpoint[Any] | None[source]
- class api.models.AuthEndpoint(app: onegov.core.Framework)[source]
This is a Dummy, because morepath requires a model for linking.
- class api.models.ApiKey[source]
Bases:
onegov.core.orm.BaseBase class used for declarative class definitions.
The
_orm.DeclarativeBaseallows for the creation of new declarative bases in such a way that is compatible with type checkers:from sqlalchemy.orm import DeclarativeBase class Base(DeclarativeBase): pass
The above
Baseclass is now usable as the base for new declarative mappings. The superclass makes use of the__init_subclass__()method to set up new classes and metaclasses aren’t used.When first used, the
_orm.DeclarativeBaseclass instantiates a new_orm.registryto be used with the base, assuming one was not provided explicitly. The_orm.DeclarativeBaseclass supports class-level attributes which act as parameters for the construction of this registry; such as to indicate a specific_schema.MetaDatacollection as well as a specific value for :paramref:`_orm.registry.type_annotation_map`:from typing_extensions import Annotated from sqlalchemy import BigInteger from sqlalchemy import MetaData from sqlalchemy import String from sqlalchemy.orm import DeclarativeBase bigint = Annotated[int, "bigint"] my_metadata = MetaData() class Base(DeclarativeBase): metadata = my_metadata type_annotation_map = { str: String().with_variant(String(255), "mysql", "mariadb"), bigint: BigInteger(), }
Class-level attributes which may be specified include:
- Parameters:
metadata – optional
_schema.MetaDatacollection. If a_orm.registryis constructed automatically, this_schema.MetaDatacollection will be used to construct it. Otherwise, the local_schema.MetaDatacollection will supersede that used by an existing_orm.registrypassed using the :paramref:`_orm.DeclarativeBase.registry` parameter.type_annotation_map – optional type annotation map that will be passed to the
_orm.registryas :paramref:`_orm.registry.type_annotation_map`.registry – supply a pre-existing
_orm.registrydirectly.
Added in version 2.0: Added
DeclarativeBase, so that declarative base classes may be constructed in such a way that is also recognized by PEP 484 type checkers. As a result,DeclarativeBaseand other subclassing-oriented APIs should be seen as superseding previous “class returned by a function” APIs, namely_orm.declarative_base()and_orm.registry.generate_base(), where the base class returned cannot be recognized by type checkers without using plugins.__init__ behavior
In a plain Python class, the base-most
__init__()method in the class hierarchy isobject.__init__(), which accepts no arguments. However, when the_orm.DeclarativeBasesubclass is first declared, the class is given an__init__()method that links to the :paramref:`_orm.registry.constructor` constructor function, if no__init__()method is already present; this is the usual declarative constructor that will assign keyword arguments as attributes on the instance, assuming those attributes are established at the class level (i.e. are mapped, or are linked to a descriptor). This constructor is never accessed by a mapped class without being called explicitly via super(), as mapped classes are themselves given an__init__()method directly which calls :paramref:`_orm.registry.constructor`, so in the default case works independently of what the base-most__init__()method does.Changed in version 2.0.1:
_orm.DeclarativeBasehas a default constructor that links to :paramref:`_orm.registry.constructor` by default, so that calls tosuper().__init__()can access this constructor. Previously, due to an implementation mistake, this default constructor was missing, and callingsuper().__init__()would invokeobject.__init__().The
_orm.DeclarativeBasesubclass may also declare an explicit__init__()method which will replace the use of the :paramref:`_orm.registry.constructor` function at this level:class Base(DeclarativeBase): def __init__(self, id=None): self.id = id
Mapped classes still will not invoke this constructor implicitly; it remains only accessible by calling
super().__init__():class MyClass(Base): def __init__(self, id=None, name=None): self.name = name super().__init__(id=id)
Note that this is a different behavior from what functions like the legacy
_orm.declarative_base()would do; the base created by those functions would always install :paramref:`_orm.registry.constructor` for__init__().