xpublish.Rest¶
- class xpublish.Rest(datasets, routers=None, cache_kws=None, app_kws=None)[source]¶
Used to publish one or more Xarray Datasets via a REST API (FastAPI application).
To publish a single dataset via its own FastAPI application, you might want to use the
xarray.Dataset.rest
accessor instead for more convenience. It provides the same interface than this class.- Parameters
datasets (
xarray.Dataset
or dict) – A singlexarray.Dataset
object or a mapping of datasets objects to be served. If a mapping is given, keys are used as dataset ids and are converted to strings. See also the notes below.routers (list, optional) – A list of dataset-specific
fastapi.APIRouter
instances to include in the fastAPI application. If None, the default routers will be included. The items of the list may also be tuples with the following format:[(router1, {'prefix': '/foo', 'tags': ['foo', 'bar']})]
, where the 1st tuple element is afastapi.APIRouter
instance and the 2nd element is a dictionary that is used to pass keyword arguments tofastapi.FastAPI.include_router()
.cache_kws (dict, optional) – Dictionary of keyword arguments to be passed to
cachey.Cache.__init__()
. By default, the cache size is set to 1MB, but this can be changed withavailable_bytes
.app_kws (dict, optional) – Dictionary of keyword arguments to be passed to
fastapi.FastAPI.__init__()
.
Notes
The urls of the application’s API endpoints differ whether a single dataset or a mapping (collection) of datasets is given. In the latter case, all dataset-specific endpoint urls have the prefix
/datasets/{dataset_id}
, where{dataset_id}
corresponds to the keys of the mapping (converted to strings). Still in the latter case, the endpoint/datasets
is added and returns the list of all dataset ids.Methods
__init__
(datasets[, routers, cache_kws, app_kws])serve
([host, port, log_level])Serve this FastAPI application via
uvicorn.run()
.Attributes
Returns the
fastapi.FastAPI
application instance.Returns the
cachey.Cache
instance used by the FastAPI application.