Skip to main content

A client library for accessing NLB's Catalogue Search API

Project description

nlb-api-client

Python SDK for accessing NLB's Catalogue Search API, generated dynamically from NLB's Openapi.json using openapi-python-client.

Tenacity with wait_exponential is used for retry behavior on NLP API if 429 Too Many Request is received.

Prerequisite

The package can be installed with the following command:

pip install nlb-catalogue-client

As API key is required for access of NLB's API, please request one using Open Web Service Application Form.

Usage

First, create a client using AuthenticatedClient. Do note that to access NLB public APIs, ensure that NLB_API_ID and NLB_API_KEY are available and exposed as environment variable.

import os
from nlb_catalogue_client import AuthenticatedClient

NLB_APP_ID = os.environ["NLB_APP_ID"]
NLB_API_KEY = os.environ["NLB_API_KEY"]

client = AuthenticatedClient(
    base_url="https://openweb.nlb.gov.sg/api/v2/Catalogue/",
    auth_header_name="X-API-KEY",
    token=NLB_API_KEY,
    prefix="",
    headers={"X-APP-Code": NLB_APP_ID},
)

Now call your endpoint and use your models:

from nlb_catalogue_client.models import SearchTitlesResponseV2
from nlb_catalogue_client.api.catalogue import get_search_titles

with client as client:
    response = get_search_titles.sync_detailed(client=client, keywords="Snow White")
    if isinstance(response.parsed, SearchTitlesResponseV2):
        search_result: SearchTitlesResponseV2 = response.parsed
        if search_result.titles:
            print([bk.title for bk in search_result.titles])
    else:
        print("Error in API Response", response.status_code)

Or do the same thing with an async version:

from nlb_catalogue_client.models import SearchTitlesResponseV2
from nlb_catalogue_client.api.catalogue import get_search_titles

async with client as client:
    response = await get_search_titles.asyncio_detailed(client=client, keywords="Snow White")
    if isinstance(response.parsed, SearchTitlesResponseV2):
        search_result: SearchTitlesResponseV2 = response.parsed
        if search_result.titles:
            print([bk.title for bk in search_result.titles])
    else:
        print("Error in API Response", response.status_code)

Things to know:

  1. Every path/method combo becomes a Python module with four functions:

    1. sync: Blocking request that returns parsed data (if successful) or None
    2. sync_detailed: Blocking request that always returns a Request, optionally with parsed set if the request was successful.
    3. asyncio: Like sync but async instead of blocking
    4. asyncio_detailed: Like sync_detailed but async instead of blocking
  2. All path/query params, and bodies become method arguments.

Advanced customizations

There are more settings on the generated Client class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying httpx.Client or httpx.AsyncClient (depending on your use-case):

from nlb_catalogue_client import Client

def log_request(request):
    print(f"Request event hook: {request.method} {request.url} - Waiting for response")

def log_response(response):
    request = response.request
    print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}")

client = Client(
    base_url="https://openweb.nlb.gov.sg/api/v2/Catalogue/",
    httpx_args={"event_hooks": {"request": [log_request], "response": [log_response]}},
)

# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()

You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):

import httpx
from nlb_catalogue_client import Client

client = Client(
    base_url="https://openweb.nlb.gov.sg/api/v2/Catalogue/",
)
# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.
client.set_httpx_client(httpx.Client(base_url="https://openweb.nlb.gov.sg/api/v2/Catalogue/", proxies="http://localhost:8030"))

Running Tests

To run the test suite, ensure you have the package installed locally and execute:

pytest .

License

Distributed under the MIT License. See LICENSE for more information.

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

nlb_catalogue_client-0.1.3.tar.gz (44.2 kB view details)

Uploaded Source

Built Distribution

nlb_catalogue_client-0.1.3-py3-none-any.whl (68.6 kB view details)

Uploaded Python 3

File details

Details for the file nlb_catalogue_client-0.1.3.tar.gz.

File metadata

  • Download URL: nlb_catalogue_client-0.1.3.tar.gz
  • Upload date:
  • Size: 44.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for nlb_catalogue_client-0.1.3.tar.gz
Algorithm Hash digest
SHA256 883d53bf2883c31a6077de8839ef08580a676708160bb28af75724ef318f4a8e
MD5 d725fcc90630350442813990ac820db9
BLAKE2b-256 28cb4e4dda8eb09a8f36e9ee092c2e7c53b2d40dc87c9c82551f9b76c559a1c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for nlb_catalogue_client-0.1.3.tar.gz:

Publisher: publish.yml on kiritowu/nlb_catalogue_client

Attestations:

File details

Details for the file nlb_catalogue_client-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for nlb_catalogue_client-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7af96b06cfa685caac6795a7fd526c63496893b1e3f42bf04c3f7bea5b6bbaeb
MD5 195d553f25bf9e8ba98b4264cb37f243
BLAKE2b-256 1200581e4eaf9de9118942a845cf38225e742829d3fcc18ca5e9f28b09ca8b1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for nlb_catalogue_client-0.1.3-py3-none-any.whl:

Publisher: publish.yml on kiritowu/nlb_catalogue_client

Attestations:

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