Skip to main content

Get Hakai database resources using http calls

Project description

Hakai Api Python Client

This project exports a single Python class that can be used to make HTTP requests to the Hakai API resource server. The exported Client class extends the functionality of the Python requests library to supply Hakai OAuth2 credentials with url requests.

PyPI tests License: MIT

Table of Contents

Installation

Quickstart

Methods

API endpoints

Advanced usage

Contributing

Installation

Python 3.8 or higher is required. Install with pip:

pip install hakai-api

Quickstart

from hakai_api import Client

# Get the api request client
client = Client()  # Follow stdout prompts to get an API token

# Make a data request for chlorophyll data (using relative endpoint)
response = client.get('/eims/views/output/chlorophyll?limit=50')

print(response.json())
# [{'action': '', 'event_pk': 7064, 'rn': '1', 'date': '2012-05-17', 'work_area': 'CALVERT'...

Desktop OAuth Flow

For native applications and automated scripts, use the desktop OAuth flow with PKCE:

from hakai_api import Client

# Use desktop OAuth flow (opens browser, more secure)
client = Client(auth_flow="desktop")

# Or use the factory method
client = Client.create_desktop_client()

# Make requests using relative endpoints
response = client.get('/eims/views/output/stations')
print(response.json())

User Agent Configuration

Important: Set a descriptive user agent to help identify your application on the backend. Often the repository url is a good way to identify yourself:

from hakai_api import Client
import os

# Set user agent during initialization
client = Client(user_agent="MyApp/1.0 (contact@example.com)")

# Or set via environment variable
os.environ['HAKAI_API_USER_AGENT'] = "MyApp/1.0 (contact@example.com)"
client = Client()

Methods

This library exports a single client name Client. Instantiating this class produces a requests.Session client from the Python requests library. The Hakai API Python Client inherits directly from requests.Session thus all methods available on that parent class are available. For details see the requests documentation.

The hakai_api Client class also contains a property api_root which is useful for constructing urls to access data from the API. The above Quickstart example demonstrates using this property to construct a url to access project names.

API endpoints

For details about the API, including available endpoints where data can be requested from, see the Hakai API documentation.

Advanced usage

Custom API Endpoints

You can specify which API to access when instantiating the Client. By default, the API uses https://portal.hakai.org/api as the API root. It may be useful to use this library to access a locally running API instance or to access the Goose API for testing purposes. If you are always going to be accessing data from a locally running API instance, you are better off using the requests.py library directly since Authorization is not required for local requests.

from hakai_api import Client

# Get a client for a locally running API instance
client = Client("http://localhost:8666")
print(client.api_root)  # http://localhost:8666

Relative Endpoint Support

The client supports relative endpoints that automatically prepend the API root:

from hakai_api import Client

client = Client()

# These are equivalent:
response1 = client.get('/eims/views/output/stations')
response2 = client.get('https://portal.hakai.org/api/eims/views/output/stations')

Credentials Configuration

Direct Credentials

You can pass in the credentials string retrieved from the hakai API login page while initiating the Client class.

from hakai_api import Client

# Pass a credentials token as the Client Class is initiated
client = Client(credentials="CREDENTIAL_TOKEN")

Environment Variables

Set credentials using the HAKAI_API_CREDENTIALS environment variable. This is useful for e.g. setting credentials in a docker container. The value of the environment variable should be the credentials token retrieved from the Hakai API login page.

export HAKAI_API_CREDENTIALS="your_credential_token_here"

Custom Credentials File Location

By default, credentials are saved to ~/.hakai-api-auth. You can customize this location:

from hakai_api import Client

# Set custom credentials file path
client = Client(credentials_file="/path/to/my/credentials")

# Or use environment variable
# export HAKAI_API_CREDENTIALS="/path/to/my/credentials"
client = Client()

Contributing

See CONTRIBUTING

License

See LICENSE

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

hakai_api-2.0.1.tar.gz (54.4 kB view details)

Uploaded Source

Built Distribution

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

hakai_api-2.0.1-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file hakai_api-2.0.1.tar.gz.

File metadata

  • Download URL: hakai_api-2.0.1.tar.gz
  • Upload date:
  • Size: 54.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for hakai_api-2.0.1.tar.gz
Algorithm Hash digest
SHA256 6b91c28c1ed905a669b606d80aae6d36c8c6d92cd1a48af156d57b1e9db5e1b9
MD5 fd425a1f19bf42fb097e728cc9750d30
BLAKE2b-256 2f0dad0f918f52e3b5239e1836697ae45235567b73f48075fb5d703851e8e4c8

See more details on using hashes here.

File details

Details for the file hakai_api-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: hakai_api-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for hakai_api-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cdb0f012d9697a56516168afddce0997c4cc6be80076cc7f43d5810214f6c460
MD5 8fe851bcc2868e35a44d0e7188b82169
BLAKE2b-256 a846e13943594a5b8c32ce38f57d8bafe05966e49573c87ef6105beb7e92db03

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