Added methods for getting the account_id and account_info, refactored

make_request
This commit is contained in:
Roger Gonzalez 2023-08-17 09:29:25 -03:00
parent fc1ac46c78
commit 5b8f75fb03
Signed by: rogs
GPG Key ID: C7ECE9C6C36EC2E6
3 changed files with 79 additions and 47 deletions

View File

@ -1,3 +1,2 @@
DEVICE_ID=12345
EMAIL=your@email.com
PHONE_NUMBER=598123456

View File

@ -1,56 +1,14 @@
import os
from datetime import datetime, timedelta
from time import sleep
from typing import Optional
from typing import List, Optional
import requests
from dotenv import load_dotenv
from utils import get_average_price
from utils import get_average_price, make_request
load_dotenv()
DEVICE_ID = os.environ.get("DEVICE_ID")
EMAIL = os.environ.get("EMAIL")
PHONE_NUMBER = os.environ.get("PHONE_NUMBER")
def make_request(method: str, url: str, authorization: str = None, data: Optional[dict] = None) -> requests.Response:
"""
Make a HTTP request
Args:
method (str): The HTTP method to use. Accepted methods are ``GET``, ``POST``.
url (str): The URL to use for the request.
authorization (str): Authorization token
data (dict): The data to send in the body of the request.
Returns:
requests.Response: The response object.
Raises:
Exception: If the method is not supported.
"""
headers = {
"X-Client-Type": "Android",
"User-Agent": "okhttp/3.8.1",
"Content-Type": "application/json; charset=utf-8",
"Connection": "Keep-Alive",
"User-Agent": "okhttp/3.8.1",
}
if authorization:
headers["Authorization"] = f"Bearer {authorization}"
if method == "GET":
return requests.get(url, headers=headers)
if method == "POST":
return requests.post(url, headers=headers, json=data)
raise Exception("Method not supported")
def login(email: str, phone_number: str) -> str:
"""
@ -73,6 +31,33 @@ def login(email: str, phone_number: str) -> str:
return make_request("POST", url, data=data).text
def get_ute_device_list() -> List[dict]:
"""
Get UTE device list
Returns:
List[dict]: List of devices
"""
return make_request("GET", "https://rocme.ute.com.uy/api/v1/accounts", authorization=authorization).json()["data"]
def get_ute_account_info(device_id: str) -> dict:
"""
Get UTE account info from device id
Args:
device_id (str): UTE Device id
Returns:
dict: UTE info
"""
return make_request(
"GET", f"https://rocme.ute.com.uy/api/v1/accounts/{device_id}", authorization=authorization
).json()["data"]
def get_ute_historic_info(
device_id: str,
authorization: str,
@ -191,8 +176,14 @@ def get_current_usage_info(device_id: str, authorization: str) -> dict:
if __name__ == "__main__":
date_start = "2023-08-01"
date_end = "2023-08-31"
EMAIL = os.environ.get("EMAIL")
PHONE_NUMBER = os.environ.get("PHONE_NUMBER")
average_price = get_average_price("triple")
authorization = login(EMAIL, PHONE_NUMBER)
device_list = get_ute_device_list()
device_id = device_list[0]["accountServicePointId"]
account_info = get_ute_account_info(device_id)
ute_historic_usage = get_ute_historic_info(DEVICE_ID, authorization, average_price, date_start, date_end)
ute_current_usage = get_current_usage_info(DEVICE_ID, authorization)
ute_historic_usage = get_ute_historic_info(device_id, authorization, average_price, date_start, date_end)
ute_current_usage = get_current_usage_info(device_id, authorization)

View File

@ -1,3 +1,45 @@
from typing import Optional
import requests
def make_request(method: str, url: str, authorization: str = None, data: Optional[dict] = None) -> requests.Response:
"""
Make a HTTP request
Args:
method (str): The HTTP method to use. Accepted methods are ``GET``, ``POST``.
url (str): The URL to use for the request.
authorization (str): Authorization token
data (dict): The data to send in the body of the request.
Returns:
requests.Response: The response object.
Raises:
Exception: If the method is not supported.
"""
headers = {
"X-Client-Type": "Android",
"User-Agent": "okhttp/3.8.1",
"Content-Type": "application/json; charset=utf-8",
"Connection": "Keep-Alive",
"User-Agent": "okhttp/3.8.1",
}
if authorization:
headers["Authorization"] = f"Bearer {authorization}"
if method == "GET":
return requests.get(url, headers=headers)
if method == "POST":
return requests.post(url, headers=headers, json=data)
raise Exception("Method not supported")
def get_average_price(plan: str) -> float:
"""
Get the average price for a plan