Managing Connections
18 min
all sdk api calls require a connection to a litmus edge instance or a litmus edge manager (lem) instance by default the sdk builds this connection automatically from environment variables you can also create explicit connection objects for multi device workflows there are three connection modes direct le oauth 2 0 to a single litmus edge device direct lem api token to a litmus edge manager instance (for litmussdk lem calls) lem bridge route le calls to a managed edge device through the edge manager direct le connection generating an oauth 2 0 token in your litmus edge ui navigate to system > access control > tokens press + next to api credentials and select oauth 2 0 client grant the token admin permissions (lesser permissions are supported but may limit sdk functionality) copy the client id and client secret for more detail see the how to create and use oauth 2 0 tokens in litmus edge docid\ vw9e3nuxf2zatvznj2 vm configuration set the following environment variables, either in your shell or in a env file in your working directory export edge url=https //\<your edge ip> export edge api client id=\<client id> export edge api client secret=\<client secret> if your device uses a self signed tls certificate (common in edge deployments), also set export validate certificate=false env file example edge url=https //192 168 1 100 edge api client id=my client id edge api client secret=my client secret validate certificate=false direct lem connection for litmussdk lem calls (companies, lifecycle, digital twin, etc ) that target the lem api directly no le device involved generating a lem api token in the litmus edge manager admin console navigate to settings > tokens > new select the appropriate scope for your use case under token scope select the projects the token will access create the token and copy it configuration export edge manager url=https //\<your lem ip> export edge api token=\<lem api token> edge api token is the lem api token despite the edge prefix this same field is used for both direct lem calls (above) and the lem bridge flow (below) one credential, two flows lem bridge connection the lem bridge lets the sdk communicate with individual edge devices via the edge manager useful when the le devices aren't directly reachable but the lem is generating a lem api token in the litmus edge manager admin console navigate to settings > tokens > new enable the edge api bridge scope under token scope select the projects the token will access create the token and copy it configuration export use lem bridge=true export edge manager url=https //\<your lem ip> export edge manager project id=\<project id> export edge manager device id=\<device id> export edge api token=\<lem api token> le traffic is tunneled through lem's /api/v1/edge/{project}/{device} endpoint authentication uses the lem api token (the edge api token env var) sent as the x authtoken header environment variables reference variable used by description edge url direct le litmus edge device url edge api client id direct le oauth 2 0 client id edge api client secret direct le oauth 2 0 client secret edge manager url direct lem, lem bridge litmus edge manager url edge api token direct lem, lem bridge lem api token, sent as x authtoken edge manager project id lem bridge target lem project id edge manager device id lem bridge target edge device id within the project edge manager admin url (optional) lem admin url, currently unused use lem bridge lem bridge set to true to enable bridge mode validate certificate all false to disable tls verification (self signed certs) timeout seconds all request timeout in seconds default 30 using the default connection once configured, all functions use the default connection automatically from litmussdk devicehub import devices all devices = devices list devices() managing multiple connections to work with more than one device simultaneously, create explicit connection objects via the factories in litmussdk utils conn from litmussdk utils conn import new le connection from litmussdk devicehub import devices \# default connection (from environment variables) devices list devices() \# explicit connection to a different device conn b = new le connection( edge url="https //192 168 1 100", edge client id="my client id", edge client secret="my client secret", ) devices list devices(conn b) available connection factories new le connection(edge url, client id, client secret, validate certificate=true, timeout seconds=30) direct oauth 2 0 connection to a litmus edge device new lem connection(edge manager url, edge manager admin url, edge api token, validate certificate=true, timeout seconds=30) direct connection to a litmus edge manager instance new lem bridge connection(edge manager url, edge api token, project id, device id, validate certificate=true, timeout seconds=30) connection to a le device via the lem bridge disabling and refreshing the default connection the sdk caches the default connection per type (le / lem) for performance to clear the cache (for example, after rotating credentials in the env), use the corresponding refresh function from litmussdk utils import conn conn refresh default le connection() # clears the le connection cache conn refresh default lem connection() # clears the lem connection cache to disable automatic loading of the default connection entirely from litmussdk import config from litmussdk utils import conn config disable default connection() conn refresh default le connection() conn refresh default lem connection() after disabling, calls without an explicit connection will raise re enable with config enable default connection() license copyright (c) litmus automation inc