Simple client for Evrim
Project description
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 theAuthorization
headerrefresh
: Token used to obtain a newaccess
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 validaccess
token - If
response
is provided, this token will also be set so you can leverage operations likerefresh_token
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file evrim-0.1.0.tar.gz
.
File metadata
- Download URL: evrim-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.6 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93057ecf317247e72604679065bb332d2246ffeced3b1c86341a9a46a8f8ccf5 |
|
MD5 | 2efe5e9ab57b756fc949e54a549838a6 |
|
BLAKE2b-256 | cad918aea4183a08e27e444aad921c6dca3b637a9e47208ac9d9481700d22a12 |
File details
Details for the file evrim-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: evrim-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.6 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 385b862cd309bcdef2246ff503b141435865bd8da221787f1cafd0a727dc8fc7 |
|
MD5 | 3fb97b964a383e64cf5027f241664614 |
|
BLAKE2b-256 | f7039befaea08c3182fa49522de5382bc1cd4a857e4937ba003f98bdb1a2d8c3 |