Closing Client Connections

Context Manager

Both ClientSync and ClientAsync clients can be used as context manager. This ensures that the client is properly closed and resources are released when the context manager exits.

Synchronous Example

from junglescout import ClientSync
from junglescout.models.parameters import (
    Marketplace,
)

API_KEY_NAME = "API_KEY_NAME"
API_KEY = "API_KEY"

with ClientSync(
    api_key_name=API_KEY_NAME,
    api_key=API_KEY,
    marketplace=Marketplace.US,
) as client:
    keywords_by_asin = client.keywords_by_asin(
        asin=["B0CP9Z56SW", "B0154ASID6"],
    )

assert client.is_closed

Asynchronous Example

import asyncio

from junglescout import ClientAsync
from junglescout.models.parameters import (
    Marketplace,
)

API_KEY_NAME = "API_KEY_NAME"
API_KEY = "API_KEY"

async def example_coroutine():
    async with ClientAsync(
        api_key_name=API_KEY_NAME,
        api_key=API_KEY,
        marketplace=Marketplace.US,
    ) as client:
        return await client.keywords_by_asin(
            asin=["B0CP9Z56SW", "B0154ASID6"],
        )
    assert client.is_closed


keywords_by_asin = asyncio.run(example_coroutine())

Explicit Close

Clients can also be closed explicitly by calling the close() method on both ClientSync and ClientAsync clients.

from junglescout import ClientSync
from junglescout.models.parameters import (
    Marketplace,
)

API_KEY_NAME = "API_KEY_NAME"
API_KEY = "API_KEY"

client = ClientSync(api_key_name=API_KEY_NAME, api_key=API_KEY, marketplace=Marketplace.US)
keywords_by_asin = client.keywords_by_asin(
    asin=["B0CP9Z56SW", "B0154ASID6"],
)
client.close()
assert client.is_closed