Skip to main content

Lekko Python SDK Client

Project description

Coverage Status PyPI version

Lekko Python SDK

The Lekko SDK for Python

Install

The lekko_client package is published on PyPI.

pip install lekko_client

Usage

Initializing a cached Lekko client

Creates a client that fetches configs from Lekko backend and caches them in memory. Configs are kept up to date via polling.

import lekko_client

lekko_client.initialize(lekko_client.CachedServerConfig(
    owner_name="<REPOSITORY_OWNER>",
    repo_name="<REPOSITORY_NAME>",
    api_key="<API_KEY>",  # Optional - defaults to "LEKKO_API_KEY" ENV Var
    lekko_uri="<URI>",    # Optional - defaults to "prod.api.lekko.dev:443"
    context={},           # Optionally provide context dict to be merged into each get request
))

str_config = lekko_client.get_string("my_namespace", "my_config", {"context_key": "context_val"})

Initializing a cached Lekko client in git mode

Creates a client that reads configs from a git repository on disk and caches them in memory. Configs are kept up to date via a file watcher.

import lekko_client

lekko_client.initialize(lekko_client.CachedGitConfig(
    owner_name="<REPOSITORY_OWNER>",
    repo_name="<REPOSITORY_NAME>",
    git_repo_path="<GIT_REPO_PATH>",
    api_key="<API_KEY>",  # Optional - defaults to "LEKKO_API_KEY" ENV Var
    lekko_uri="<URI>",    # Optional - defaults to "prod.api.lekko.dev:443"
    context={},           # Optionally provide context dict to be merged into each get request
))

str_config = lekko_client.get_string("my_namespace", "my_config", {"context_key": "context_val"})

Initializing a Lekko client for remote evaluation

Create a client that communicates with the Lekko API or a Lekko sidecar for remote evaluation

import lekko_client

lekko_client.initialize(lekko_client.APIConfig(  # Or lekko_client.SidecarConfig
    owner_name="<REPOSITORY_OWNER>",
    repo_name="<REPOSITORY_NAME>",
    api_key="<API_KEY>",  # Optional - defaults to "LEKKO_API_KEY" ENV Var
    context={},           # Optionally provide context dict to be merged into each get request
))

str_config = lekko_client.get_string("my_namespace", "my_config", {"context_key": "context_val"})

Lifecycle management

lekko_client.initialize() must be invoked prior to calling the lekko_client.get_*() functions and should not be called multiple times. We recommend invoking it early in your app's lifecycle, for example when constructing your Flask app or as part of FastAPI's lifecycle context manager.

We recommend you invoke lekko_client.close() during app shutdown. This will ensure all evaluation events are properly tracked and the SDK client is deregistered correctly.

It is also possible to do your own lifecycle management and avoid the lekko_client.initialize() and lekko_client.get_*() methods entirely. The above root module-level methods are provided for convenience to suit most use cases, without having to explicitly manage a client instance and pass it around. Feel free to construct any of the clients in lekko_client.clients manually.

For example:

from lekko_client.clients import APIClient

client = APIClient(
    owner_name="<REPOSITORY_OWNER>",
    repo_name="<REPOSITORY_NAME>",
    api_key="<API_KEY>",
)

...

def use_client(client):
    str_config = client.get_string("my_namespace", "my_config", {"context_key": "context_val"})
    return str_config

use_client(client)

Protobuf configs

There are two methods to retrieve protobuf configs, with one allowing you to specify the expected proto message type.

  • get_proto_by_type(key, context, proto_message_type) will attempt to convert the config to the specified type and raise MismatchedProtoType on failure.

  • get_proto(key, context) will attempt to locate the proto message type in the proto symbol database, which tracks imported proto types. If unable to locate the type, it will simply return the message as a google.protobuf.any_pb2.Any

For example, if you have a proto config named proto_config defined as:

{
  "@type": "type.googleapis.com/google.protobuf.BoolValue",
  "value": false
}

You would see the following behavior:

>>> config_value = client.get_proto("proto", {})
>>> type(config_value)
<class 'google.protobuf.any_pb2.Any'>
>>> config_value.value
b''

But after importing the appropriate type:

>>> from google.protobuf import wrappers_pb2
>>> config_value = client.get_proto("proto", {})
>>> type(config_value)
<class 'google.protobuf.wrappers_pb2.BoolValue'>
>>> config_value.value
False

Example

See: https://github.com/lekkodev/python-sdk/blob/main/example.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lekko_client-0.2.1.tar.gz (52.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lekko_client-0.2.1-py3-none-any.whl (60.2 kB view details)

Uploaded Python 3

File details

Details for the file lekko_client-0.2.1.tar.gz.

File metadata

  • Download URL: lekko_client-0.2.1.tar.gz
  • Upload date:
  • Size: 52.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for lekko_client-0.2.1.tar.gz
Algorithm Hash digest
SHA256 86478a3fdc4aed1d53237553b4e6bc778dc28be596c26ee63ffe340bf021c8e6
MD5 0b6b4ef257e5d425d840236d9bc2e5a5
BLAKE2b-256 9672b70ffeb40f671a492581f8c67fe1a925e05c298bbe53d9bd12da596f90cc

See more details on using hashes here.

File details

Details for the file lekko_client-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: lekko_client-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 60.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for lekko_client-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3823fa1335fda3ad5844c375295bbfd058154ca94c64ae24892c7614b669ae7e
MD5 265d253d5eaceb07e9149b23baa9a5e8
BLAKE2b-256 4c23e374653f6d0bb9e439e405d85370050ef69c5c74c56fdb6721040c389d16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page