Clients¶
A client owns one gRPC connection, the credential sent on every call, and the
checks performed when it is opened — a health probe, then a call that proves the
credential. With a token, that is list_domains, which also reports the
service’s input limits. With an API client’s client_id and
client_secret, it is the exchange of those for a token, which the client
renews by itself before it expires. The operations themselves live on
namespaces hanging off it: client.memory, and client.networks and
client.users for a client with client credentials.
Closing a client ends the key of any session still acting for one of your users, so a session left open does not hold a key until it expires.
- class Memco(token=None, host=None, *, client_id=None, client_secret=None, token_lifetime=None, tls=None, timeout=30.0, env=None, log_level=None)[source]¶
Bases:
objectSynchronous client for Memco Shared Memory.
Opens one gRPC channel and holds it until closed, so a single client should be created once and reused. It is safe to share between threads.
Construction makes two calls. The first is the standard gRPC health endpoint, which is unauthenticated: it proves the host, port and TLS settings are sound. The second proves the credential, so a bad one fails here rather than on the first real call. With a token, it is
list_domains(), which also reports the input limits the service enforces. The client keeps those, and from then on refuses an oversized field locally instead of spending a round trip on a call the service would refuse.With an API client’s
client_idandclient_secretinstead, the second call exchanges them for a token, and no memory method is called: an issued token carries no content role of its own, so the service would refuse one. The client renews the token by itself, before it expires.Connecting and closing are reported to the
memcoailogger atINFO, and a rejected credential atERRORbefore it is raised, since a client is often built somewhere the traceback does not reach. The credential itself is never logged, at any level.- Parameters:
token (str | None) – Credential to authenticate with, either a static Memco API key or a session token. When omitted, and the environment holds no client credentials,
MEMCO_API_TOKENis used, falling back to the deprecatedMEMCO_API_KEYwith a warning.host (str | None) – Service endpoint, optionally including a port such as
localhost:50051. When omitted,MEMCO_API_HOSTis used, falling back togrpc.memco.ai. Port 443 is assumed when the host carries none.client_id (str | None) – An API client’s id, passed with
client_secretinstead of a token. When neither they nortokenis passed, and bothMEMCO_CLIENT_IDandMEMCO_CLIENT_SECRETare set, those are used, even ifMEMCO_API_TOKENis set too.client_secret (str | None) – The API client’s secret, passed with
client_id. It is sent only to exchange for a token, and never logged.token_lifetime (int | None) – Lifetime in seconds to ask for on each token issued for the client credentials. Omitted, the service’s default applies. The service owns the maximum, and refuses a longer lifetime.
tls (bool | None) – Whether to dial over TLS using the system trust store. Set to
Falseonly for a plaintext endpoint such as a local server. When omitted,MEMCO_API_TLSdecides –trueorfalse– falling back toTrue. Without TLS, the client logs aWARNINGas it is built.timeout (float) – Default deadline in seconds for each call, covering any wait for a credential as well as the request itself. Individual methods can override it.
env (Mapping[str, str] | None) – Environment mapping to read defaults from. Defaults to
os.environ; supplying one is mainly useful in tests.log_level (str | int | None) – The SDK’s log level, as a name —
"debug","info","warning","error","critical", or"none"to turn it off — or aloggingconstant. Defaults to"info"and overridesMEMCO_LOG. Any level but"none"attaches the SDK’s own stderr handler and stops thememcoailogger propagating, so records bypass the handlers the application configured; it is process-wide, since a logger is. An application with its own logging should pass"none"and set the level on thememcoailogger instead.
- Raises:
MemcoConfigError – If no credential is available, a token is passed beside client credentials, only half of the client pair is given,
token_lifetimeis given without client credentials or is not positive, the host is unusable,MEMCO_API_TLSis neithertruenorfalse, orlog_levelis not a level this SDK accepts.MemcoUnavailableError – If the service cannot be reached.
MemcoUnhealthyError – If the service reports that it is not serving.
MemcoAuthenticationError – If the credential is rejected.
MemcoSunsetError – If what this client uses is past its sunset date.
- Variables:
memory – The memory operations, as
MemoryOperations.networks – The network administration, as
NetworkOperations. Needs client credentials.users – The user administration, as
UserOperations. Needs client credentials.
Example
>>> with Memco() as client: ... session = client.memory.start_session("coding") ... result = session.search("how does gRPC health checking work") ... for memory in result.memories: ... print(memory.idx, len(memory.insights))
- memory¶
The memory operations. See
MemoryOperations.
- networks¶
The network administration. See
NetworkOperations.
- users¶
The user administration. See
UserOperations.
- property credential_kind: Literal['token', 'api_client']¶
Which credential the client resolved from its arguments and the environment.
"token"for an API token,"api_client"for an API client’sclient_idandclient_secret. Only an API client can administer networks and users, or open a session acting as one of your users.
- close()[source]¶
End the keys of impersonated sessions left open, then close the channel.
Blocks until any in-flight call has finished, so a client shared between threads can be closed from one of them safely, and so no key is ended under a call still using it. Safe to call more than once: a close on another thread is waited for, and one cut short – a signal handler raising in it, say – is finished by the next. Never raises of its own accord: a key that cannot be ended is logged at
WARNINGand expires on its own. After closing, any further call raisesMemcoConfigError, and closing a session changes nothing.- Return type:
None
- class AsyncMemco(token=None, host=None, *, client_id=None, client_secret=None, token_lifetime=None, tls=None, timeout=30.0, env=None, log_level=None)[source]¶
Bases:
objectAsyncio client for Memco Shared Memory.
Mirrors
Memcomethod for method; only the awaiting differs. Because opening a connection requires I/O, the checks the synchronous client runs in__init__cannot run here: use it as an async context manager, or callconnect()yourself.- Parameters:
token (str | None) – Credential to authenticate with, either a static Memco API key or a session token. When omitted, and the environment holds no client credentials,
MEMCO_API_TOKENis used, falling back to the deprecatedMEMCO_API_KEYwith a warning.host (str | None) – Service endpoint, optionally including a port. When omitted,
MEMCO_API_HOSTis used, falling back togrpc.memco.ai.client_id (str | None) – An API client’s id, passed with
client_secretinstead of a token. When neither they nortokenis passed, and bothMEMCO_CLIENT_IDandMEMCO_CLIENT_SECRETare set, those are used, even ifMEMCO_API_TOKENis set too.client_secret (str | None) – The API client’s secret, passed with
client_id. It is sent only to exchange for a token, and never logged.token_lifetime (int | None) – Lifetime in seconds to ask for on each token issued for the client credentials. Omitted, the service’s default applies. The service owns the maximum, and refuses a longer lifetime.
tls (bool | None) – Whether to dial over TLS using the system trust store. When omitted,
MEMCO_API_TLSdecides –trueorfalse– falling back toTrue. Without TLS, the client logs aWARNINGas it is built.timeout (float) – Default deadline in seconds for each call, covering any wait for a credential as well as the request itself.
env (Mapping[str, str] | None) – Environment mapping to read defaults from. Defaults to
os.environ.log_level (str | int | None) – The SDK’s log level, as a name —
"debug","info","warning","error","critical", or"none"to turn it off — or aloggingconstant. Defaults to"info"and overridesMEMCO_LOG. Any level but"none"attaches the SDK’s own stderr handler and stops thememcoailogger propagating, so records bypass the handlers the application configured; it is process-wide, since a logger is. An application with its own logging should pass"none"and set the level on thememcoailogger instead.
- Raises:
MemcoConfigError – If no credential is available, a token is passed beside client credentials, only half of the client pair is given,
token_lifetimeis given without client credentials or is not positive, the host is unusable,MEMCO_API_TLSis neithertruenorfalse, orlog_levelis not a level this SDK accepts.- Variables:
memory – The memory operations, as
AsyncMemoryOperations.networks – The network administration, as
AsyncNetworkOperations. Needs client credentials.users – The user administration, as
AsyncUserOperations. Needs client credentials.
Example
>>> async with AsyncMemco() as client: ... session = await client.memory.start_session("coding") ... result = await session.search("how does health checking work")
- memory¶
The memory operations. See
AsyncMemoryOperations.
- networks¶
The network administration.
- users¶
The user administration. See
AsyncUserOperations.
- async connect()[source]¶
Verify the connection, running the checks the constructor could not.
Probes the health endpoint, then proves the credential. With a token that is
list_domains(), which also teaches the client the input limits the service enforces. With client credentials it is the exchange for a token, and no memory method is called, since an issued token carries no content role. Calling this more than once repeats both, except that a token already issued is reused until it is due for renewal.- Returns:
This client.
- Raises:
MemcoUnavailableError – If the service cannot be reached.
MemcoUnhealthyError – If the service reports that it is not serving.
MemcoAuthenticationError – If the credential is rejected.
MemcoSunsetError – If what this client uses is past its sunset date.
- Return type:
- property credential_kind: Literal['token', 'api_client']¶
Which credential the client resolved from its arguments and the environment.
"token"for an API token,"api_client"for an API client’sclient_idandclient_secret. Only an API client can administer networks and users, or open a session acting as one of your users.
- async close()[source]¶
End the keys of impersonated sessions left open, then close the channel.
Waits for any in-flight call to finish first, so no key is ended under a call still using it. Safe to call more than once, and never raises: a key that cannot be ended is logged at
WARNINGand expires on its own. After closing, any further call raisesMemcoConfigError, and closing a session changes nothing. A close that is cancelled still finishes, and closing again waits for it to.- Return type:
None
Provenance¶
- provenance()[source]¶
Read the provenance of the installed generated client.
The descriptor is compiled in, and the result is cached.
- Returns:
The provenance recorded when this package was built.
- Raises:
MemcoConfigError – If the descriptor does not match the expected shape.
- Return type:
Example
>>> provenance().server_commit # the commit this wheel was built from '762721a87ab0...'