Python library for Mercury.co.nz OAuth and API integration
Project description
PyMercury - Mercury.co.nz Python Library
A comprehensive Python library for interacting with Mercury.co.nz services, including OAuth authentication and selfservice API integration.
Features
- OAuth 2.0 PKCE Authentication - Secure authentication with Mercury.co.nz
- Selfservice API Integration - Access customer, account, and service data
- Multi-Service Support - Electricity and Gas service integration
- Clean Architecture - Modular design with separate OAuth and API clients
- Type Safety - Full type hints for better IDE support
- Comprehensive Error Handling - Detailed exceptions for different error scenarios
- Flexible Configuration - Environment variable support with sensible defaults
Installation
pip install pymercury
Quick Start
Simple Authentication
from pymercury import authenticate
# Get OAuth tokens
tokens = authenticate("your-email@example.com", "your-password")
print(f"Customer ID: {tokens.customer_id}")
print(f"Access Token: {tokens.access_token}")
Complete Account Data
from pymercury import get_complete_data
# Get everything in one call
data = get_complete_data("your-email@example.com", "your-password")
print(f"Customer ID: {data.customer_id}")
print(f"Account IDs: {data.account_ids}")
print(f"Electricity Services: {data.service_ids.electricity}")
print(f"Gas Services: {data.service_ids.gas}")
print(f"Broadband Services: {data.service_ids.broadband}")
Main Client (Recommended)
from pymercury import MercuryClient
# Create client and login
client = MercuryClient("your-email@example.com", "your-password")
client.login()
# Easy access to information
customer_id = client.customer_id
account_ids = client.account_ids
service_ids = client.service_ids
# Get complete data
complete_data = client.get_complete_account_data()
Advanced Usage
Separate OAuth and API Clients
from pymercury.oauth import MercuryOAuthClient
from pymercury.api import MercuryAPIClient
# OAuth authentication only
oauth_client = MercuryOAuthClient("email@example.com", "password")
tokens = oauth_client.authenticate()
# API calls with existing tokens
api_client = MercuryAPIClient(tokens.access_token)
customer_info = api_client.get_customer_info(tokens.customer_id)
accounts = api_client.get_accounts(tokens.customer_id)
Electricity Services
Electricity Usage and Billing
from pymercury.api import MercuryAPIClient
# Initialize API client with access token
api_client = MercuryAPIClient(access_token)
# Get electricity meter information
meter_info = api_client.get_electricity_meter_info(customer_id, account_id)
if meter_info:
print(f"Meter Number: {meter_info.meter_number}")
print(f"Meter Type: {meter_info.meter_type}")
# Get bill summary
bill_summary = api_client.get_bill_summary(customer_id, account_id)
if bill_summary:
print(f"Current Balance: ${bill_summary.current_balance}")
print(f"Due Date: {bill_summary.due_date}")
Electricity Usage Data
from pymercury.api import MercuryAPIClient
# Initialize API client with access token
api_client = MercuryAPIClient(access_token)
# Get electricity usage (defaults to last 14 days)
electricity_usage = api_client.get_electricity_usage(customer_id, account_id, service_id)
if electricity_usage:
print(f"Total Usage: {electricity_usage.total_usage} kWh")
print(f"Average Daily Usage: {electricity_usage.average_daily_usage} kWh")
print(f"Average Temperature: {electricity_usage.average_temperature}°C")
# Get hourly electricity usage (2 days ending yesterday)
hourly_electricity = api_client.get_electricity_usage_hourly(customer_id, account_id, service_id)
if hourly_electricity:
print(f"Hourly Usage Period: {hourly_electricity.start_date} to {hourly_electricity.end_date}")
print(f"Total Hourly Usage: {hourly_electricity.total_usage} kWh")
# Get monthly electricity usage (1 year period)
monthly_electricity = api_client.get_electricity_usage_monthly(customer_id, account_id, service_id)
if monthly_electricity:
print(f"Monthly Usage Period: {monthly_electricity.start_date} to {monthly_electricity.end_date}")
print(f"Total Monthly Usage: {monthly_electricity.total_usage} kWh")
# Get electricity usage content (disclaimer, modal info, etc.)
electricity_content = api_client.get_electricity_usage_content()
if electricity_content:
print(f"Usage content available for electricity service")
Gas Services
Gas Usage Content
from pymercury.api import MercuryAPIClient
# Initialize API client with access token
api_client = MercuryAPIClient(access_token)
# Get gas usage content (disclaimer, modal info, etc.)
gas_content = api_client.get_gas_usage_content()
if gas_content:
print(f"Content Name: {gas_content.content_name}") # "Gas/Usage"
print(f"Locale: {gas_content.locale}") # "en"
print(f"Usage Disclaimer: {gas_content.disclaimer_usage}") # Gas billing info
print(f"Modal Title: {gas_content.usage_info_modal_title}") # "USAGE GRAPH KEY"
Gas Usage Data
from pymercury.api import MercuryAPIClient
# Initialize API client with access token
api_client = MercuryAPIClient(access_token)
# Get gas usage data (defaults to last 14 days)
gas_usage = api_client.get_gas_usage(customer_id, account_id, service_id)
if gas_usage:
print(f"Service Type: {gas_usage.service_type}") # "Gas"
print(f"Usage Period: {gas_usage.usage_period}") # "Daily", "Monthly", etc.
print(f"Total Usage: {gas_usage.total_usage} units") # Gas consumption
print(f"Total Cost: ${gas_usage.total_cost}") # Total cost
print(f"Average Daily: {gas_usage.average_daily_usage} units") # Daily average
print(f"Data Points: {gas_usage.data_points}") # Number of readings
# Get hourly gas usage (2 days ending yesterday)
hourly_gas = api_client.get_gas_usage_hourly(customer_id, account_id, service_id)
if hourly_gas:
print(f"Hourly Usage Period: {hourly_gas.start_date} to {hourly_gas.end_date}")
print(f"Total Hourly Usage: {hourly_gas.total_usage} units")
# Get monthly gas usage (1 year period)
monthly_gas = api_client.get_gas_usage_monthly(customer_id, account_id, service_id)
if monthly_gas:
print(f"Monthly Usage Period: {monthly_gas.start_date} to {monthly_gas.end_date}")
print(f"Total Monthly Usage: {monthly_gas.total_usage} units")
Broadband Services
Fibre Broadband Usage and Service Information
from pymercury.api import MercuryAPIClient
# Initialize API client with access token
api_client = MercuryAPIClient(access_token)
# Get broadband service information and usage data
broadband_info = api_client.get_broadband_usage(customer_id, account_id, service_id)
if broadband_info:
# Service information
print(f"Plan Name: {broadband_info.plan_name}") # "FibreClassic Unlimited Naked"
print(f"Plan Code: {broadband_info.plan_code}") # "20398"
print(f"Service Type: {broadband_info.service_type}") # "Broadband"
# Usage summary
print(f"Total Data Used: {broadband_info.total_data_used} GB") # Total usage
print(f"Average Daily: {broadband_info.avg_daily_usage} GB") # Daily average
print(f"Max Daily: {broadband_info.max_daily_usage} GB") # Peak usage day
print(f"Usage Days: {broadband_info.usage_days}") # Days with usage > 0
print(f"Data Points: {broadband_info.data_points}") # Total days of data
# Usage period
print(f"Period: {broadband_info.start_date} to {broadband_info.end_date}")
# Daily usage breakdown (first 5 days)
for day in broadband_info.daily_usages[:5]:
date = day['date'][:10] # Just the date part
usage = day['usage']
print(f" {date}: {usage} GB")
# Alternative method (alias)
fibre_info = api_client.get_fibre_usage(customer_id, account_id, service_id)
Generic Usage Methods
from pymercury.api import MercuryAPIClient
# Initialize API client with access token
api_client = MercuryAPIClient(access_token)
# Generic usage content method (works for any service type)
electricity_content = api_client.get_usage_content("Electricity")
gas_content = api_client.get_usage_content("Gas")
# Generic usage data method (works for any service type)
electricity_usage = api_client.get_service_usage(customer_id, account_id, 'electricity', service_id)
gas_usage = api_client.get_service_usage(customer_id, account_id, 'gas', service_id)
Configuration
Environment Variables
Create a .env file or set environment variables:
# OAuth Configuration
MERCURY_CLIENT_ID=4c8c2c47-24cd-485d-aad9-12f3d95b3ceb
MERCURY_REDIRECT_URI=https://myaccount.mercury.co.nz
MERCURY_BASE_URL=https://login.mercury.co.nz/fc07dca7-cd6a-4578-952b-de7a7afaebdc
MERCURY_TIMEOUT=20
# API Configuration
MERCURY_API_BASE_URL=https://apis.mercury.co.nz/selfservice/v1
MERCURY_API_SUBSCRIPTION_KEY=f62040b20cf9401fb081880cb71c7dec
Programmatic Configuration
from pymercury import MercuryConfig, MercuryClient
config = MercuryConfig(
client_id="your-client-id",
api_subscription_key="your-api-key",
timeout=30
)
client = MercuryClient("email@example.com", "password", config=config)
Error Handling
from pymercury import (
MercuryClient,
MercuryAuthenticationError,
MercuryAPIError,
MercuryError
)
try:
client = MercuryClient("email@example.com", "password")
client.login()
data = client.get_complete_account_data()
except MercuryAuthenticationError:
print("Invalid credentials")
except MercuryAPIError as e:
print(f"API error: {e}")
except MercuryError as e:
print(f"Mercury.co.nz error: {e}")
API Methods
The library provides access to all Mercury.co.nz selfservice APIs, organized by functionality:
Account Management
get_customer_info()- Customer information and profile detailsget_accounts()- Account details and account listingget_services()- Service information across all accounts
Billing & Financial
get_bill_summary()- Current billing information and payment status
Electricity Services
get_electricity_usage_content()- Electricity usage content and disclaimersget_electricity_usage()- Daily electricity usage data with temperatureget_electricity_usage_hourly()- Hourly electricity usage dataget_electricity_usage_monthly()- Monthly electricity usage dataget_electricity_meter_info()- Electricity meter details and statusget_electricity_meter_reads()- Electricity meter reading historyget_electricity_plans()- Available electricity plans and pricing
Gas Services
get_gas_usage_content()- Gas usage content and disclaimersget_gas_usage()- Daily gas usage data (no temperature data)get_gas_usage_hourly()- Hourly gas usage dataget_gas_usage_monthly()- Monthly gas usage data
Broadband Services
get_broadband_usage()- Fibre broadband service info and daily usage dataget_fibre_usage()- Fibre broadband service info and usage (alias for broadband)
Generic/Cross-Service Methods
get_usage_content(service_type)- Generic usage content for any service typeget_service_usage(service_type, ...)- Generic usage data for any service type
Requirements
- Python 3.7+
requests>=2.25.0
Development
For development, install with optional dependencies:
pip install pymercury[dev]
Testing
Run the comprehensive test suite:
python test_mercury_library.py
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For support, please open an issue on the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mercury_co_nz_api-1.0.3.tar.gz.
File metadata
- Download URL: mercury_co_nz_api-1.0.3.tar.gz
- Upload date:
- Size: 62.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7756c513d194b02e4546f49bae56d1f21b0cc490fa64abf58eebe54fa1d538d
|
|
| MD5 |
a55b99e192beee191ef8b253d0d7034b
|
|
| BLAKE2b-256 |
11db36424857305d069f7260eac195524271488837dfa9882f3217482891a6bc
|
File details
Details for the file mercury_co_nz_api-1.0.3-py3-none-any.whl.
File metadata
- Download URL: mercury_co_nz_api-1.0.3-py3-none-any.whl
- Upload date:
- Size: 45.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4183d953c1fd81c93e00c99f6b6803e2ed5f70a0f52bbee1446d4f666f4d8d6e
|
|
| MD5 |
f3a8748acb04306d40dc4a258efa8eaf
|
|
| BLAKE2b-256 |
f472f36a86a026cf0473a2f27a7af55a509034abe48a984a6d1af60327c2574e
|