Skip to content

Commit

Permalink
refactor: remove syncrhonous code
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
MrMarble committed Sep 20, 2021
1 parent 97e3c45 commit 5eb566b
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 237 deletions.
182 changes: 101 additions & 81 deletions datadis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from typing import List, Literal
from datadis.types import (
ConsumptionData, ContractDetail, MaxPower, Supplie, dict_to_typed)
ConsumptionData,
ContractDetail,
MaxPower,
Supplie,
dict_to_typed,
)
import httpx

_HOST = 'https://datadis.es'
_HOST = "https://datadis.es"
_ENDPOINTS = {
'get_token': f'{_HOST}/nikola-auth/tokens/login',
'get_supplies': f'{_HOST}/api-private/api/get-supplies',
'get_contract_detail': f'{_HOST}/api-private/api/get-contract-detail',
'get_consumption_data': f'{_HOST}/api-private/api/get-consumption-data',
'get_max_power': f'{_HOST}/api-private/api/get-max-power',
"get_token": f"{_HOST}/nikola-auth/tokens/login",
"get_supplies": f"{_HOST}/api-private/api/get-supplies",
"get_contract_detail": f"{_HOST}/api-private/api/get-contract-detail",
"get_consumption_data": f"{_HOST}/api-private/api/get-consumption-data",
"get_max_power": f"{_HOST}/api-private/api/get-max-power",
}


def get_token(username: str, password: str) -> str:
async def get_token(username: str, password: str) -> str:
"""Get authentication token for private api
Args:
Expand All @@ -26,15 +31,16 @@ def get_token(username: str, password: str) -> str:
Returns:
str: Bearer token
"""
credentials = {'username': username, 'password': password}
r = httpx.post(_ENDPOINTS['get_token'], data=credentials)
if r.status_code == 200:
return r.text
else:
raise ConnectionError(f'Error: {r.json()["message"]}')
credentials = {"username": username, "password": password}
async with httpx.AsyncClient() as client:
r = await client.post(_ENDPOINTS["get_token"], data=credentials)
if r.status_code == 200:
return r.text
else:
raise ConnectionError(f'Error: {r.json()["message"]}')


def get_supplies(token: str) -> List[Supplie]:
async def get_supplies(token: str) -> List[Supplie]:
"""Search all the supplies
Args:
Expand All @@ -46,19 +52,21 @@ def get_supplies(token: str) -> List[Supplie]:
Returns:
dict: A dictionary with the supplies
"""
headers = {'Authorization': f'Bearer {token}'}
r = httpx.get(_ENDPOINTS['get_supplies'], headers=headers)
if r.status_code == 200:
result = []
for supply in r.json():
result.append(dict_to_typed(supply, Supplie))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')


def get_contract_detail(token: str, cups: str,
distrubutor_code: int) -> List[ContractDetail]:
headers = {"Authorization": f"Bearer {token}"}
async with httpx.AsyncClient() as client:
r = await client.get(_ENDPOINTS["get_supplies"], headers=headers)
if r.status_code == 200:
result = []
for supply in r.json():
result.append(dict_to_typed(supply, Supplie))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')


async def get_contract_detail(
token: str, cups: str, distrubutor_code: int
) -> List[ContractDetail]:
"""Search the contract detail
Args:
Expand All @@ -72,25 +80,32 @@ def get_contract_detail(token: str, cups: str,
Returns:
dict: [description]
"""
headers = {'Authorization': f'Bearer {token}'}

r = httpx.get(_ENDPOINTS['get_contract_detail']
+ f'?cups={cups}&distributorCode={distrubutor_code}',
headers=headers)

if r.status_code == 200:
result = []
for contract in r.json():
result.append(dict_to_typed(contract, ContractDetail))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')


def get_consumption_data(token: str, cups: str,
distrubutor_code: int, start_date: str, end_date: str,
measurement_type: Literal[0, 1], point_type: int
) -> List[ConsumptionData]:
headers = {"Authorization": f"Bearer {token}"}
async with httpx.AsyncClient() as client:
r = await client.get(
_ENDPOINTS["get_contract_detail"]
+ f"?cups={cups}&distributorCode={distrubutor_code}",
headers=headers,
)

if r.status_code == 200:
result = []
for contract in r.json():
result.append(dict_to_typed(contract, ContractDetail))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')


async def get_consumption_data(
token: str,
cups: str,
distrubutor_code: int,
start_date: str,
end_date: str,
measurement_type: Literal[0, 1],
point_type: int,
) -> List[ConsumptionData]:
"""Search the consumption data
Args:
Expand All @@ -108,26 +123,29 @@ def get_consumption_data(token: str, cups: str,
Returns:
dict: [description]
"""
headers = {'Authorization': f'Bearer {token}'}

r = httpx.get(_ENDPOINTS['get_consumption_data']
+ f'?cups={cups}&distributorCode={distrubutor_code}'
+ f'&start_date={start_date}&end_date={end_date}'
+ f'&measurement_type={measurement_type}'
+ f'&point_type={point_type}',
headers=headers)

if r.status_code == 200:
result = []
for contract in r.json():
result.append(dict_to_typed(contract, ConsumptionData))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')


def get_max_power(token: str, cups: str, distrubutor_code: int,
start_date: str, end_date: str) -> List[MaxPower]:
headers = {"Authorization": f"Bearer {token}"}
async with httpx.AsyncClient() as client:
r = await client.get(
_ENDPOINTS["get_consumption_data"]
+ f"?cups={cups}&distributorCode={distrubutor_code}"
+ f"&start_date={start_date}&end_date={end_date}"
+ f"&measurement_type={measurement_type}"
+ f"&point_type={point_type}",
headers=headers,
)

if r.status_code == 200:
result = []
for contract in r.json():
result.append(dict_to_typed(contract, ConsumptionData))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')


async def get_max_power(
token: str, cups: str, distrubutor_code: int, start_date: str, end_date: str
) -> List[MaxPower]:
"""Search the maximum power and the result will appear in kW
Args:
Expand All @@ -143,17 +161,19 @@ def get_max_power(token: str, cups: str, distrubutor_code: int,
Returns:
dict: [description]
"""
headers = {'Authorization': f'Bearer {token}'}

r = httpx.get(_ENDPOINTS['get_max_power']
+ f'?cups={cups}&distributorCode={distrubutor_code}'
+ f'&start_date={start_date}&end_date={end_date}',
headers=headers)

if r.status_code == 200:
result = []
for contract in r.json():
result.append(dict_to_typed(contract, MaxPower))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')
headers = {"Authorization": f"Bearer {token}"}
async with httpx.AsyncClient() as client:
r = await client.get(
_ENDPOINTS["get_max_power"]
+ f"?cups={cups}&distributorCode={distrubutor_code}"
+ f"&start_date={start_date}&end_date={end_date}",
headers=headers,
)

if r.status_code == 200:
result = []
for contract in r.json():
result.append(dict_to_typed(contract, MaxPower))
return result
else:
raise ConnectionError(f'Error: {r.json()["message"]}')
156 changes: 0 additions & 156 deletions datadis/concurrent.py

This file was deleted.

0 comments on commit 5eb566b

Please sign in to comment.