Skip to main content

Simple client for Evrim

Project description

codecov

Evrim Client

A simple Python client to interact with Evrim's REST API.

Authentication

Evrim's REST API uses JSON Web Tokens (JWT) for authentication. Users can either obtain one using their username and password or use an existing valid JWT.

Username/Password Authentication

Let's start by obtaining a JWT using our username and password. When initializing Evrim, the client will authenticate using the provided url, username, and password and obtain a valid JWT.

from evrim import Evrim
import os

# access env variables
url = os.getenv("EVRIM_URL")
username = os.getenv("EVRIM_USERNAME")
password = os.getenv("EVRIM_PASSWORD")

# authenticate using url, username, and password
client = Evrim(
    url=url
    username=username,
    password=password
)

If this authentication is successful, two tokens are then issued to the user:

  • access: Bearer token used in all subsequent requests in the Authorization header
  • refresh: Token used to obtain a new access token once it expires

Token Validation

JWTs expire after a certain amount of time. To check if your token is still valid, Evrim provides the validate_token function.

from evrim import Evrim
from time import sleep
import os

# access env variables
url = os.getenv("EVRIM_URL")
username = os.getenv("EVRIM_USERNAME")
password = os.getenv("EVRIM_PASSWORD")

# authenticate using url, username, and password
client = Evrim(
    url=url
    username=username,
    password=password
)

# let some time pas
print("sleeping for 20 seconds ...")
sleep(20)

# check if token still valid
if client.validate_token():
    print("Token is still valid!")

If your token is still valid, this function will return True.

Token Refresh

If your token happens to be no longer valid, there are a few paths forward:

  • Obtain a new JWT using set_token
  • Refresh your existing token pair with refresh_token

Let's look at both in the example below.

Set New Tokens

You can set a new token pair by simply using the set_token function. This will use your existing username and password to obtain a new token pair.

client.set_token()

This will update the session Authentication header with your fresh access token and set a new refresh token.

Refresh Existing Token

You can also refresh your existing access token using the refresh_token function.

client.refresh_token()

This will updated only the session Authorization header with your fresh access token.

Existing Valid JWT Authentication

We can also authenticate using an existing valid JWT.

from evrim import Evrim
import os

url = os.getenv("EVRIM_URL")
token = os.getenv("EVRIM_TOKEN")
refresh = os.getenv("EVRIM_REFRESH_TOKEN")  # optional but can be used to refresh existing access token

client = Evrim.from_token(
    url=url,
    token=token,
    refresh=refresh  # optional value but helpful!
)

This is will do two things:

  • Validate your access token to ensure it is still valid
  • If valid, set you session Authorization header with the existing valid access token
  • If response is provided, this token will also be set so you can leverage operations like refresh_token.

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

evrim-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

evrim-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file evrim-0.1.1.tar.gz.

File metadata

  • Download URL: evrim-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.5.0-1025-azure

File hashes

Hashes for evrim-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0253d97d24c0147c14a44afadfd5fd3937fc4e69280df67ede24c6df2978369a
MD5 8746539b3df1b4de264cb0746d78b8f1
BLAKE2b-256 2ba29aa50529c100b5e1635a7b165a22669433e21a7fc1c1aa339169febdb13b

See more details on using hashes here.

File details

Details for the file evrim-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: evrim-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/6.5.0-1025-azure

File hashes

Hashes for evrim-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 20aebfce88fb38e47260c16d5ed54bb0d4ca092150e1e3fb4ef5030afb360370
MD5 53d130b9456aac904a39d910c7d67acb
BLAKE2b-256 9e72061f18eabad0bf8dd83268212e21672ee7ebd0f6997418bdf133af33482b

See more details on using hashes here.

Supported by

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