-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup in progress. Don't step onto the working area!
- Loading branch information
1 parent
0a908b2
commit 8c178c9
Showing
6 changed files
with
169 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import smtplib | ||
from setup import logger | ||
from common import logger | ||
from email.message import EmailMessage | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,45 @@ | ||
import os | ||
import time | ||
import json | ||
from functools import lru_cache | ||
|
||
import requests | ||
from requests.adapters import HTTPAdapter | ||
from urllib3 import Retry | ||
|
||
from common import API_KEY, MAX_RETRIES, RETRY_BACKOFF_FACTOR, logger | ||
|
||
from setup import logger | ||
from http import HTTPStatus | ||
|
||
API_KEY = os.getenv("API_KEY") | ||
GET_URL = os.getenv("GET_URL") | ||
MAX_RETRIES = int(os.getenv("MAX_RETRIES", 5)) | ||
RETRY_DELAY_SECS = int(os.getenv("RETRY_DELAY_SECS", 15)) | ||
__all__ = ["request"] | ||
|
||
if not API_KEY: | ||
raise ValueError("API key not found. Please set the API_KEY environment variable.") | ||
if not GET_URL: | ||
raise ValueError("URL not found. Please set the GET_URL environment variable.") | ||
|
||
@lru_cache(1) | ||
def get_session(): | ||
retry_strategy = Retry( | ||
total=MAX_RETRIES, | ||
backoff_factor=RETRY_BACKOFF_FACTOR, | ||
) | ||
|
||
def get_session(api_key): | ||
session = requests.Session() | ||
session.headers.update({"Authorization": f"Bearer {api_key}"}) | ||
session.headers.update({ | ||
"Authorization": "Bearer " + API_KEY, | ||
# https://dashflo.net/docs/api/pterodactyl/v1/ | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
}) | ||
|
||
adapter = HTTPAdapter(max_retries=retry_strategy) | ||
|
||
session.mount("https://", adapter) | ||
session.mount("http://", adapter) | ||
|
||
return session | ||
|
||
|
||
def get_response(session): | ||
def request(url, method: str = "GET", data=None) -> dict: | ||
for retry in range(MAX_RETRIES): | ||
try: | ||
response = session.get(GET_URL) | ||
if response.status_code == HTTPStatus.OK: | ||
if "application/json" in response.headers["Content-Type"]: | ||
return response | ||
else: | ||
logger.error("Invalid API Key - received non-JSON response") | ||
raise ValueError("Invalid API Key") | ||
else: | ||
logger.error( | ||
f"Error: {response.status_code} - {HTTPStatus(response.status_code).phrase}" | ||
) | ||
raise requests.exceptions.HTTPError(response.status_code) | ||
response = get_session().request(method=method, url=url, data=data) | ||
response.raise_for_status() | ||
return response.json() | ||
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e: | ||
logger.error(f"Network Error: {str(e)}") | ||
except requests.exceptions.RequestException as e: | ||
logger.error(f"Request Exception: {str(e)}") | ||
|
||
if retry < MAX_RETRIES - 1: | ||
logger.info(f"Retrying in {RETRY_DELAY_SECS} seconds...") | ||
time.sleep(RETRY_DELAY_SECS) | ||
else: | ||
raise requests.exceptions.RetryError("Max retries exceeded.") | ||
|
||
|
||
def process_response(response): | ||
try: | ||
if "application/json" in response.headers["Content-Type"]: | ||
server_info = response.json()["data"] | ||
return server_info | ||
else: | ||
logger.error("Received non-JSON response") | ||
raise ValueError("Received non-JSON response") | ||
except json.JSONDecodeError: | ||
logger.error("Error decoding JSON response") | ||
raise | ||
except (ValueError, KeyError): | ||
logger.error("Error processing response") | ||
raise | ||
|
||
|
||
def main(): | ||
with get_session(API_KEY) as session: | ||
response = get_response(session) | ||
return process_response(response) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
result = main() | ||
logger.info(f"Request executed successfully. Result: {result}") | ||
except Exception as e: | ||
logger.error(f"Script execution failed: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.