Package: lekko_client
Lekko Python SDKThe Lekko SDK for PythonInstallThelekko_clientpackage is published onPyPI.pip install lekko_clientUsageInitializing a cached Lekko clientCreates a client that fetches configs from Lekko backend and caches them in memory. Configs are kept up to date via polling.importlekko_clientlekko_client.initialize(lekko_client.CachedServerConfig(owner_name="<REPOSITORY_OWNER>",repo_name="<REPOSITORY_NAME>",api_key="<API_KEY>",# Optional - defaults to "LEKKO_API_KEY" ENV Varlekko_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 modeCreates 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.importlekko_clientlekko_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 Varlekko_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 evaluationCreate a client that communicates with the Lekko API or a Lekko sidecar for remote evaluationimportlekko_clientlekko_client.initialize(lekko_client.APIConfig(# Or lekko_client.SidecarConfigowner_name="<REPOSITORY_OWNER>",repo_name="<REPOSITORY_NAME>",api_key="<API_KEY>",# Optional - defaults to "LEKKO_API_KEY" ENV Varcontext={},# 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 managementlekko_client.initialize()must be invoked prior to calling thelekko_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 invokelekko_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 thelekko_client.initialize()andlekko_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 inlekko_client.clientsmanually.For example:fromlekko_client.clientsimportAPIClientclient=APIClient(owner_name="<REPOSITORY_OWNER>",repo_name="<REPOSITORY_NAME>",api_key="<API_KEY>",)...defuse_client(client):str_config=client.get_string("my_namespace","my_config",{"context_key":"context_val"})returnstr_configuse_client(client)Protobuf configsThere 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 raiseMismatchedProtoTypeon 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 agoogle.protobuf.any_pb2.AnyFor example, if you have a proto config namedproto_configdefined 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.valueb''But after importing the appropriate type:>>>fromgoogle.protobufimportwrappers_pb2>>>config_value=client.get_proto("proto",{})>>>type(config_value)<class'google.protobuf.wrappers_pb2.BoolValue'>>>>config_value.valueFalseExampleSee:https://github.com/lekkodev/python-sdk/blob/main/example.py