Client¶
client
¶
The MERMAID API client.
Classes:
| Name | Description |
|---|---|
MermaidClient |
Synchronous client for the MERMAID API. |
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_BASE_URL |
|
|
DEV_BASE_URL |
|
|
BASE_URL_ENV_VAR |
|
|
DEFAULT_ZONAL_STATS_URL |
|
|
ZONAL_STATS_URL_ENV_VAR |
|
|
DEFAULT_COVARIATES_URL |
|
|
COVARIATES_URL_ENV_VAR |
|
DEFAULT_ZONAL_STATS_URL
module-attribute
¶
DEFAULT_COVARIATES_URL
module-attribute
¶
PUBLIC_HEADERS
module-attribute
¶
PUBLIC_HEADERS = frozenset(
{
"accept",
"accept-encoding",
"connection",
"content-length",
"content-type",
"host",
"user-agent",
}
)
MermaidClient
¶
MermaidClient(
*,
auth: Auth | None = None,
api_key: str | None = None,
base_url: str | None = None,
zonal_stats_url: str | None = None,
covariates_url: str | None = None,
timeout: float | Timeout = DEFAULT_TIMEOUT,
max_retries: int = DEFAULT_MAX_RETRIES,
backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
headers: dict[str, str] | None = None,
user_agent: str | None = None,
transport: BaseTransport | None = None,
)
Synchronous client for the MERMAID API.
Owns the base URL, the timeouts, the User-Agent, the retry-with-backoff
policy and the JSON decoding. Credentials are never inspected here: the
client only ever asks an Auth to stamp an
outgoing request.
Each endpoint group is a property returning a wrapper whose list() gives
a lazy PaginatedList. Reference data needs
no credentials; /me/, /projects/, /sites/ and /managements/
do.
Use it as a context manager so the connection pool is closed.
The client is safe to share between threads, which is how a
Batch fans requests out. Throttling is
cooperative: a 429 seen on any thread sets a client-wide deadline
(Retry-After, or the computed backoff, capped at 30 seconds) that every
request waits on before it is sent, so the workers back off together instead
of one at a time. The deadline is shared by both hosts: a 429 from the
Zonal Stats service also pauses MERMAID API calls on the same client, and the
reverse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth
|
Auth | None
|
Credential provider. Mutually exclusive with |
None
|
api_key
|
str | None
|
A MERMAID API key, |
None
|
base_url
|
str | None
|
API root. Defaults to |
None
|
zonal_stats_url
|
str | None
|
Root of the separate Zonal Stats service. Defaults to
|
None
|
covariates_url
|
str | None
|
Root of the covariates STAC API. Defaults to
|
None
|
timeout
|
float | Timeout
|
Seconds, or an |
DEFAULT_TIMEOUT
|
max_retries
|
int
|
Extra attempts after a 429 or 5xx. |
DEFAULT_MAX_RETRIES
|
backoff_factor
|
float
|
Base delay of the exponential backoff, in seconds.
A |
DEFAULT_BACKOFF_FACTOR
|
headers
|
dict[str, str] | None
|
Extra headers sent with every request to the MERMAID API. Requests to the Zonal Stats service and the covariates catalog carry none of them. |
None
|
user_agent
|
str | None
|
Overrides |
None
|
transport
|
BaseTransport | None
|
An |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Attributes:
| Name | Type | Description |
|---|---|---|
auth |
The credential provider in use. |
|
base_url |
The resolved API root, always ending in |
|
zonal_stats_url |
The resolved Zonal Stats service root, always ending in |
|
covariates_url |
The resolved covariates STAC API root, always ending in |
|
max_retries |
Extra attempts made after a retryable status code. |
|
backoff_factor |
Base delay of the exponential backoff, in seconds. |
Example
from datamermaid import MermaidClient
with MermaidClient(api_key="mmd_abc.def") as client:
me = client.me()
for project in client.projects.list():
print(project.name)
Reference data is public, so it needs no credentials at all:
Methods:
| Name | Description |
|---|---|
close |
Close the underlying connection pool. |
me |
Return the profile that owns the credentials in use ( |
choices |
Return the API's controlled vocabularies ( |
request |
Issue a request, retrying transient failures and mapping errors. |
request_json |
Issue a request and decode the JSON body ( |
projects
cached
property
¶
projects: ProjectsResource
Access to the /projects/ endpoints.
Call it with a project id (client.projects(project_id)) for that
project's own sites, sample events and survey collections.
managements
cached
property
¶
managements: ManagementsResource
Management regimes, /managements/ (requires authentication).
benthic_attributes
cached
property
¶
benthic_attributes: BenthicAttributesResource
Benthic attributes, /benthicattributes/.
invert_attributes
cached
property
¶
invert_attributes: InvertAttributesResource
Macroinvertebrate attributes, /invertattributes/.
invert_species
cached
property
¶
invert_species: InvertSpeciesResource
Macroinvertebrate species, /invertspecies/.
summary_sample_events
cached
property
¶
summary_sample_events: SummarySampleEventsResource
Public sample event summaries, /summarysampleevents/.
label_mappings
cached
property
¶
label_mappings: LabelMappingsResource
Classifier label mappings, /classification/labelmappings/.
zonal_stats
cached
property
¶
zonal_stats: ZonalStats
The Zonal Stats service, a separate public host (client.zonal_stats.raster).
Requests to it carry no MERMAID credentials. See
ZonalStats.
covariates
cached
property
¶
covariates: Covariates
The covariates catalog, a public STAC API (client.covariates.collections()).
Requests to it carry no MERMAID credentials. See
Covariates.
choices
¶
Return the API's controlled vocabularies (GET /choices/).
Without an argument, every choice set keyed by name (countries,
reeftypes, managementparties, ...). With one, just that set's
rows, fetched from /choices/<name>/.
Unlike the list endpoints this one is not paginated, so it returns
plain dictionaries rather than a PaginatedList of models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
A single choice set to fetch. Omit it for all of them. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Every choice set keyed by name, or one set's rows. |
request
¶
request(
method: str,
url: str,
*,
params: dict[str, Any] | None = None,
json: Any = None,
public: bool = False,
**kwargs: Any,
) -> Response
Issue a request, retrying transient failures and mapping errors.
url may be relative to the base URL or absolute (as in the next
link of a paginated response). public=True is for another host: the
request is sent without the client's auth, and with only the headers in
PUBLIC_HEADERS.
request_json
¶
request_json(
method: str,
url: str,
*,
params: dict[str, Any] | None = None,
json: Any = None,
public: bool = False,
**kwargs: Any,
) -> Any
Issue a request and decode the JSON body (None when empty).
resolve_base_url
¶
Resolve the API root: explicit argument, then env var, then default.
resolve_zonal_stats_url
¶
Resolve the Zonal Stats service root: argument, then env var, then default.
resolve_covariates_url
¶
Resolve the covariates STAC API root: argument, then env var, then default.