Skip to main content

A client library for accessing NHL API

Project description

nhl-api-client

A client library for accessing NHL API

Usage

First, create a client:

from nhl_api_client import Client

client = Client(base_url="http://api-web.nhle.com")

Now call your endpoint and use your models:

from nhl_api_client.api.default import get_season_standings_by_date
from nhl_api_client.models import SeasonStandings
from nhl_api_client.types import Response

with client as client:
    teams_response: SeasonStandings = get_season_standings_by_date.sync('2023-11-15', client=client)
    # or if you need more info (e.g. status_code)
    response: Response[SeasonStandings] = get_season_standings_by_date.sync_detailed('2023-11-15', client=client)

Or do the same thing with an async version:

from nhl_api_client.models import SeasonStandings
from nhl_api_client.api.my_tag import get_season_standings_by_date
from nhl_api_client.types import Response

async with client as client:
    my_data: SeasonStandings = await get_season_standings_by_date.asyncio('2023-11-15', client=client)
    response: Response[SeasonStandings] = await get_season_standings_by_date.asyncio_detailed('2023-11-15', client=client)

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.

  3. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above)

  4. Any endpoint which did not have a tag will be in nhl_api_client.api.default

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 nhl_api_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="http://api-web.nhle.com",
    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 nhl_api_client import Client

client = Client(
    base_url="http://api-web.nhle.com",
)
# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.
client.set_httpx_client(httpx.Client(base_url="http://api-web.nhle.com", proxies="http://localhost:8030"))

Building / publishing this package

This project uses Poetry to manage dependencies and packaging. Here are the basics:

  1. Update the metadata in pyproject.toml (e.g. authors, version)
  2. If you're using a private repository, configure it with Poetry
    1. poetry config repositories.<your-repository-name> <url-to-your-repository>
    2. poetry config http-basic.<your-repository-name> <username> <password>
  3. Publish the client with poetry publish --build -r <your-repository-name> or, if for public PyPI, just poetry publish --build

If you want to install this client into another project without publishing it (e.g. for development) then:

  1. If that project is using Poetry, you can simply do poetry add <path-to-this-client> from that project
  2. If that project is not using Poetry:
    1. Build a wheel with poetry build -f wheel
    2. Install that wheel from the other project pip install <path-to-wheel>

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

nhl_api_client-1.0.6.tar.gz (107.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nhl_api_client-1.0.6-py3-none-any.whl (100.9 kB view details)

Uploaded Python 3

File details

Details for the file nhl_api_client-1.0.6.tar.gz.

File metadata

  • Download URL: nhl_api_client-1.0.6.tar.gz
  • Upload date:
  • Size: 107.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.10.12 Linux/6.5.0-14-generic

File hashes

Hashes for nhl_api_client-1.0.6.tar.gz
Algorithm Hash digest
SHA256 0b2d0890036389584e9b4acb9c600a66cbf6ad61e65e598936842a449d81fb78
MD5 33eb9c0864a110d2398b2fb9a31d1ace
BLAKE2b-256 a39cc69ec37f99182a4d1c971ca24df7930fa976c4e7179ac347bf68b1a3daab

See more details on using hashes here.

File details

Details for the file nhl_api_client-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: nhl_api_client-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 100.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.10.12 Linux/6.5.0-14-generic

File hashes

Hashes for nhl_api_client-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f11f430607356b8818f2c3cc7824aad8f00f801aa71313817351d8d99c776448
MD5 64a4e258292bf6b221733692b07d693e
BLAKE2b-256 42a97518e853b10f61ce57bcd7cd189efcd5d0fefb3332ccbdfc7ff63365d461

See more details on using hashes here.

Supported by

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