Skip to content

Commit

Permalink
Add support for get_products and get_product v1 API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
markallanson committed Jan 7, 2021
1 parent 32816bf commit 88b780f
Show file tree
Hide file tree
Showing 6 changed files with 708 additions and 69 deletions.
52 changes: 41 additions & 11 deletions octopus_energy/rest_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from http import HTTPStatus
from typing import Any
from typing import Any, Optional

from aiohttp import BasicAuth, ClientSession
from furl import furl
Expand All @@ -22,9 +22,18 @@ class OctopusEnergyRestClient:
resources this client uses.
"""

def __init__(self, api_token: str, base_url: str = _API_BASE):
def __init__(self, api_token: Optional[str] = None, base_url: str = _API_BASE):
"""Create a new instance of the Octopus API rest client.
Args:
api_token: [Optional] The API token to use to access the APIs. If not specified only
octopus public APIs can be called.
base_url: The Octopus Energy API address.
"""
self.base_url = furl(base_url)
self.session = ClientSession(auth=BasicAuth(api_token, ""))
self.session = ClientSession(
auth=BasicAuth(api_token, "") if api_token is not None else None
)

def __enter__(self):
raise TypeError("Use async context manager (await with) instead")
Expand Down Expand Up @@ -58,6 +67,21 @@ async def get_account_details(self, account_number: str):
"""
return await self._execute(["v1", "accounts", account_number])

async def get_electricity_consumption_v1(self, mpan: str, serial_number: str) -> dict:
"""Gets the consumption of electricity from a specific meter.
Args:
mpan: The MPAN (Meter Point Administration Number) of the meter to query.
serial_number: The serial number of the meter to query.
Returns:
A dictionary containing the electricity consumption response.
"""
return await self._execute(
["v1", "electricity-meter-points", mpan, "meters", serial_number, "consumption"]
)

async def get_gas_consumption_v1(self, mprn: str, serial_number: str) -> dict:
"""Gets the consumption of gas from a specific meter.
Expand All @@ -73,20 +97,26 @@ async def get_gas_consumption_v1(self, mprn: str, serial_number: str) -> dict:
["v1", "gas-meter-points", mprn, "meters", serial_number, "consumption"]
)

async def get_electricity_consumption_v1(self, mpan: str, serial_number: str) -> dict:
"""Gets the consumption of electricity from a specific meter.
async def get_products_v1(self) -> dict:
"""Gets octopus energy products.
Returns:
A dictionary containing the products response.
"""
return await self._execute(["v1", "products"])

async def get_product_v1(self, product_code: str) -> dict:
"""Gets detailed information about a specific octopus energy product.
Args:
mpan: The MPAN (Meter Point Administration Number) of the meter to query.
serial_number: The serial number of the meter to query.
product_code: The product code to retrieve.
Returns:
A dictionary containing the electricity consumption response.
A dictionary containing the product details response.
"""
return await self._execute(
["v1", "electricity-meter-points", mpan, "meters", serial_number, "consumption"]
)
return await self._execute(["v1", "products", product_code])

async def _execute(self, url_parts: list, **kwargs) -> Any:
"""Executes an API call to Octopus energy and maps the response."""
Expand Down
62 changes: 31 additions & 31 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "octopus-energy"
version = "0.1.3"
version = "0.1.4"
description = "Python client for the Octopus Energy RESTful API"
authors = ["Mark Allanson <[email protected]>"]
license = "MIT"
Expand Down
Loading

0 comments on commit 88b780f

Please sign in to comment.