# Importing requests module for talking to the API
# Importing JSON for reading the response body
# Importing simplejson for error handling when downloading a file
from urllib.parse import urlencode
def __init__(self, identifier: str):
self.base_url = "https://api.leet-auth.dev/public"
def url(self, path: str) -> str:
return self.base_url + path
def format_json(json_body: dict) -> str:
return json.dumps(json_body, indent=4, ensure_ascii=True)
def headers(self) -> dict:
"Authorization": self.auth_token,
def login(self, username: str, password: str, **kwargs) -> str:
response = requests.post(self.url("/authenticate"), params={
"hwid": kwargs.get("hwid", ""),
"checksum": kwargs.get("checksum", "")
# Getting the JSON response from our API
json_body = response.json()
# Assigning the JWT to our session
self.auth_token = json_body["token"]
return self.format_json(json_body)
def register(self, username: str, password: str, license_key: str, **kwargs) -> str:
response = requests.post(self.url("/register"), params={
"hwid": kwargs.get("hwid", ""),
"checksum": kwargs.get("checksum", "")
return self.format_json(response.json())
def renew_plan(self, username: str, license: str) -> str;
response = requests.post(self.url("/renew"), params={
return self.format_json(response.json())
def get_properties(self) -> str:
response = requests.get(self.url("/properties"), params={
return self.format_json(response.json())
def change_password(self, new_password: str, confirm_new_password: str) -> str:
response = requests.post(self.url("/change_password"), params={
"new_password": new_password,
"cnf_password": confirm_new_password
return self.format_json(response.json())
def get_login_logs(self) -> str:
response = requests.get(self.url("/logins"), params={
return self.format_json(response.json())
def download_file(self, file_name: str, path_to_save: str) -> str:
response = requests.get(self.url("/files/") + file_name, params={
json_body = response.json()
except simplejson.errors.JSONDecodeError:
# Writing the file in chunks, its a better way of writing if the file is big
with open(path_to_save, 'wb') as downloaded_file:
for chunk in response.iter_content(chunk_size=128):
downloaded_file.write(chunk)
# Returning a hardocded sucess response
return self.format_json({
"note": "Successfully downloaded the file"
return self.format_json(json_body)
def get_variable(self, variable_name: str) -> str:
response = requests.get(self.url("/variables/") + variable_name, params={
return self.format_json(response.json())