Skip to content

Commit

Permalink
Feature DataGolf API Client: Unoffical.
Browse files Browse the repository at this point in the history
Adds endpoints for general information at this time.

Adds base structure for handling http requests, parsing, api_key handling.

Adds Pytest, black and ruff checks to CI/CD pipleline.
  • Loading branch information
coreyjs committed Jun 9, 2024
1 parent 7d7c327 commit 9df7a61
Show file tree
Hide file tree
Showing 16 changed files with 765 additions and 0 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: NHL-API-PY

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# If you wanted to use multiple Python versions, you'd have specify a matrix in the job and
# reference the matrixe python version here.
- uses: actions/setup-python@v5
with:
python-version: 3.9.18

# Cache the installation of Poetry itself, e.g. the next step. This prevents the workflow
# from installing Poetry every time, which can be slow. Note the use of the Poetry version
# number in the cache key, and the "-0" suffix: this allows you to invalidate the cache
# manually if/when you want to upgrade Poetry, or if something goes wrong. This could be
# mildly cleaner by using an environment variable, but I don't really care.
- name: cache poetry install
uses: actions/cache@v4
with:
path: ~/.local
key: poetry-1.5.1-0

# Install Poetry. You could do this manually, or there are several actions that do this.
# `snok/install-poetry` seems to be minimal yet complete, and really just calls out to
# Poetry's default install script, which feels correct. I pin the Poetry version here
# because Poetry does occasionally change APIs between versions and I don't want my
# actions to break if it does.
#
# The key configuration value here is `virtualenvs-in-project: true`: this creates the
# venv as a `.venv` in your testing directory, which allows the next step to easily
# cache it.
- uses: snok/install-poetry@v1
with:
version: 1.5.1
virtualenvs-create: true
virtualenvs-in-project: true

# Cache your dependencies (i.e. all the stuff in your `pyproject.toml`). Note the cache
# key: if you're using multiple Python versions, or multiple OSes, you'd need to include
# them in the cache key. I'm not, so it can be simple and just depend on the poetry.lock.
- name: cache deps
id: cache-deps
uses: actions/cache@v3
with:
path: .venv
key: pydeps-${{ hashFiles('**/poetry.lock') }}

# Install dependencies. `--no-root` means "install all dependencies but not the project
# itself", which is what you want to avoid caching _your_ code. The `if` statement
# ensures this only runs on a cache miss.
- run: poetry install --no-interaction --no-root
if: steps.cache-deps.outputs.cache-hit != 'true'

# Now install _your_ project. This isn't necessary for many types of projects -- particularly
# things like Django apps don't need this. But it's a good idea since it fully-exercises the
# pyproject.toml and makes that if you add things like console-scripts at some point that
# they'll be installed and working.
- run: poetry install --no-interaction

# And finally run tests. I'm using pytest and all my pytest config is in my `pyproject.toml`
# so this line is super-simple. But it could be as complex as you need.
- run: poetry run pytest

# run a check for black
- name: poetry run black . --check
run: poetry run black . --check

# run a lint check with ruff
- name: poetry run ruff check .
run: poetry run ruff check .
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.idea
.ipynb_checkpoints
.mypy_cache
.vscode
__pycache__
.pytest_cache
htmlcov
dist
site
.coverage
coverage.xml
.netlify
test.db
log.txt
Pipfile.lock
env3.*
env
docs_build
venv
docs.zip
archive.zip

# vim temporary files
*~
.*.sw?

*/.DS_Store
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9.18
1 change: 1 addition & 0 deletions data_golf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .client import DataGolfClient # noqa: F401
Empty file added data_golf/api/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions data_golf/api/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List


class General:
def __init__(self, client):
self.client = client

def player_list(self, format: str = "json") -> List[dict]:
"""
:return:
"""
return self.client.get(resource="/get-player-list", format=format)

def tour_schedule(self, format: str = "json") -> List[dict]:
"""
:return:
"""
return self.client.get(resource="/get-schedule", format=format)
37 changes: 37 additions & 0 deletions data_golf/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from data_golf.config import DGConfig
from data_golf.http import HttpClient
from data_golf.api.general import General


class DGCInvalidApiKey(Exception):
pass


class DataGolfClient:
def __init__(
self,
api_key: str,
verbose: bool = False,
timeout: int = 15,
ssl_verify: bool = True,
) -> None:
self._validate_api_key(api_key)

self._config = DGConfig(
api_key=api_key, verbose=verbose, timeout=timeout, ssl_verify=ssl_verify
)
self._http_client = HttpClient(self._config)

# Endpoints
self.general = General(self._http_client)

def _validate_api_key(self, api_key: str) -> None:
"""
Private method to validate the API key.
:param api_key:
:return:
"""
if not isinstance(api_key, str):
raise DGCInvalidApiKey("API key must be a string.")
if not api_key:
raise DGCInvalidApiKey("API key cannot be empty.")
13 changes: 13 additions & 0 deletions data_golf/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class DGConfig:
def __init__(
self,
api_key: str,
verbose: bool = False,
timeout: int = 15,
ssl_verify: bool = True,
) -> None:
self.api_key = api_key
self.verbose = verbose
self.timeout = timeout
self.ssl_verify = ssl_verify
self.base_url = "https://feeds.datagolf.com"
48 changes: 48 additions & 0 deletions data_golf/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from data_golf.request_helpers import RequestHelpers

import httpx
import logging


class HttpClient:
def __init__(self, config) -> None:
self._config = config
if self._config.verbose:
logging.basicConfig(level=logging.INFO)

def _build_url(self, resource: str, format: str):
"""
Private method to build the URL for the Data Golf API.
:param resource:
:param format:
:return:
"""
params = [f"key={self._config.api_key}", f"file_format={format}"]
url = ""

if len(resource.split("?")) > 1:
url = f"{self._config.base_url}{resource}&{'&'.join(params)}"
else:
url = f"{self._config.base_url}{resource}?{'&'.join(params)}"
return url

@RequestHelpers.prepare_request
def get(self, resource: str, format: str = "json", **kwargs) -> httpx.request:
"""
Private method to make a get request to the Data Golf API. This wraps the lib httpx functionality.
:param format:
:param resource:
:return:
"""
with httpx.Client(
verify=self._config.ssl_verify, timeout=self._config.timeout
) as client:
r: httpx.request = client.get(
url=self._build_url(resource, format), **kwargs
)

if self._config.verbose:
logging.info(f"API URL: {r.url}")
logging.info(kwargs["headers"])

return r.json()
32 changes: 32 additions & 0 deletions data_golf/request_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from functools import wraps

ALLOWED_FORMATS = ["json"]


class RequestHelpers:

@classmethod
def _set_headers(cls, f_format) -> dict[str, str]:
headers = {}
if f_format not in ALLOWED_FORMATS:
raise ValueError("format must be 'json'. CSV support is coming")

if f_format == "json":
headers["Content-Type"] = "application/json"
elif f_format == "csv":
headers["Content-Type"] = "text/csv; charset=utf-8"

return headers

@staticmethod
def prepare_request(func):
@wraps(func)
def wrapper(*args, **kwargs):
f_format = kwargs.get("format", "json")

headers = RequestHelpers._set_headers(f_format=f_format)

kwargs["headers"] = {**kwargs.get("headers", {}), **headers}
return func(*args, **kwargs)

return wrapper
Loading

0 comments on commit 9df7a61

Please sign in to comment.