Pythong client for Joystick
This is the library that simplifies the way how you can communicate with Joystick API.
You can install the package via Pip:
pip install joystick-python
We provide two types of clients: asynchronous and synchronous. They have exactly the same interfaces, the only difference is a way you import them.
import os
from joystick import Client
joystick_api_key = os.getenv("JOYSTICK_API_KEY")
if joystick_api_key is None:
raise ValueError("Please set JOYSTICK_API_KEY environment variable.")
client = Client(
api_key=joystick_api_key,
)
response = client.get_contents({"cid1", "cid2"})
print(f'First content: {response["cid1"]}')
print(f'Second content: {response["cid2"]}')
import asyncio
import os
from joystick import AsyncClient
async def main():
joystick_api_key = os.getenv("JOYSTICK_API_KEY")
if joystick_api_key is None:
raise ValueError("Please set JOYSTICK_API_KEY environment variable.")
client = AsyncClient(
api_key=joystick_api_key,
)
response = await client.get_contents({"cid1", "cid2"})
print(f'First content: {response["cid1"]}')
print(f'Second content: {response["cid2"]}')
asyncio.run(main())
All examples below will be provided for
async
version of the client. For sync it's enough to use proper Client and avoidawait
keyword
...
await client.get_content('cid1')
...
When creating the Client
/AsyncClient
instance, you can specify additional parameters which will
be used by all API calls from the client, for more details see
API documentation:
client = AsyncClient(
api_key=joystick_api_key,
cache_expiration_seconds=60,
serialized=True,
params={
"param1": "value1",
"param2": "value2",
},
sem_ver="0.0.1",
user_id="user-id-1",
)
In most of the cases you will be not interested in the full response from the API, but if you're you can specify
fullResponse
option to the client methods. The client will return you raw API response:
get_content_response = await client.get_content('cid1', full_response=True)
# OR
get_contents_response = await client.get_contents({'cid1'} , full_response=True)
When true
, we will pass query parameter responseType=serialized
to Joystick API.
get_content_response = await client.get_content('cid1', serialized=True)
# OR
get_contents_response = await client.get_contents({'cid1'} , serialized=True)
This option can be set for every API call from the client by setting serialized
as true
via
constructor, or via propert setter.
client = AsyncClient(
api_key=joystick_api_key,
serialized=True,
)
If you want to ignore existing cache and request the new config – pass this option as true
.
get_content_response = await client.get_content('cid1', refresh=True)
# OR
get_contents_response = await client.get_contents({'cid1'} , refresh=True)
By default, the client uses in-memory caching, which means that if you build the distributed application, every instance will go to the Joystick API for at least first call and the cache will be erased after the application is closed.
You can specify your cache implementation which implements either
AsyncCacheInterface
if you use AsyncClient
,
or SyncCacheInterface
if you use SyncClient
.
We rely on library httpx
to make requests to Joystick API and we
support the same platforms as httpx
.
If you want to clear the cache – run await client.clear_cache()
.
We use the pyenv
to install multiple versions of Python on the developer's machine and venv
to create the virtual environment for these versions:
pyenv global 3.5.10
rm -rf ./venv # This one might fail, if it's the first time you create `venv` in this proj.
python3 -m venv venv
source ./venv/bin/activate
pip install -e '.[dev]'
nox -e test
nox -e format