api.models

Attributes

_DefaultT

log

Exceptions

ApiException

Base class for all API exceptions.

ApiInvalidParamException

Base class for all API exceptions.

Classes

ApiEndpointItem

A single instance of an item of a specific endpoint.

ApiEndpoint

An API endpoint.

ApiEndpointCollection

A collection of all available API endpoints.

AuthEndpoint

ApiKey

Module Contents

api.models._DefaultT[source]
api.models.log[source]
exception api.models.ApiException(message: str = 'Internal Server Error', exception: Exception | None = None, status_code: int = 500, headers: dict[str, str] | None = None)[source]

Bases: Exception

Base class for all API exceptions.

Mainly used to ensure that all exceptions regarding the API are rendered with the correct content type.

message = 'Internal Server Error'[source]
status_code = 500[source]
headers[source]
exception api.models.ApiInvalidParamException(message: str = 'Invalid Parameter', status_code: int = 400)[source]

Bases: ApiException

Base class for all API exceptions.

Mainly used to ensure that all exceptions regarding the API are rendered with the correct content type.

message = 'Invalid Parameter'[source]
status_code = 400[source]
class api.models.ApiEndpointItem(app: onegov.core.Framework, 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.

app[source]
endpoint[source]
id[source]
property api_endpoint: ApiEndpoint[_M] | None[source]
property item: onegov.core.collection._M | None[source]
property data: dict[str, Any] | None[source]
form(request: onegov.core.request.CoreRequest) Form | None[source]
class api.models.ApiEndpoint(app: onegov.core.Framework, 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.

name: ClassVar[str] = ''[source]
endpoint: ClassVar[str] = ''[source]
filters: ClassVar[set[str]][source]
form_class: ClassVar[type[onegov.form.Form] | None] = None[source]
app[source]
extra_parameters[source]
page = None[source]
batch_size = 100[source]
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[_M]

Return a new endpoint item instance with the given item.

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 session: sqlalchemy.orm.Session[source]

Returns a dictionary with pagination instances.

property batch: dict[ApiEndpointItem[_M], onegov.core.collection._M][source]

Returns a dictionary with endpoint item instances and their titles.

abstract 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
}

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) Form | None[source]

Return a form for editing items of this collection.

abstract apply_changes(item: onegov.core.collection._M, form: Any) None[source]

Apply the changes to the item based on the given form data.

property collection: PaginationWithById[_M, Any][source]
Abstractmethod:

Return an instance of the collection with filters and page set.

assert_valid_filter(param: str) None[source]
class api.models.ApiEndpointCollection(app: onegov.core.Framework)[source]

A collection of all available API endpoints.

app[source]
property endpoints: dict[str, type[ApiEndpoint[Any]]][source]
class api.models.AuthEndpoint(app: onegov.core.Framework)[source]
app[source]
class api.models.ApiKey[source]

Bases: onegov.core.orm.Base

__tablename__ = 'api_keys'[source]
id: Column[uuid.UUID][source]
user_id: Column[uuid.UUID][source]
user: relationship[User][source]
name: Column[str][source]
read_only: Column[bool][source]
last_used: Column[datetime | None][source]
key: Column[uuid.UUID][source]