Skip to content

Commit

Permalink
Add client for legacy api
Browse files Browse the repository at this point in the history
  • Loading branch information
chelnak committed Nov 15, 2022
1 parent 74cccc2 commit dbbd072
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
102 changes: 101 additions & 1 deletion poetry.lock

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

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ packages = [{include = "status_cake_exporter"}]
include = ["status_cake_exporter"]
reportMissingImports = false

[tool.isort]
profile = "black"

[tool.poetry.dependencies]
python = "^3.10"
prometheus-client = "0.15.0"
statuscake-py = "^1.0.1b1"
typer = "^0.7.0"
requests = "^2.28.1"
types-requests = "^2.28.11.4"
typing-extensions = "^4.4.0"

[tool.poetry.group.dev.dependencies]
flake8 = "^5.0.4"
Expand Down
44 changes: 44 additions & 0 deletions status_cake_exporter/_status_cake_legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging
from typing import Any

import requests

logger = logging.getLogger("status_cake_legacy")


class PaymentRequiredException(Exception):
pass


class NotFoundException(Exception):
pass


class StatusCakeLegacyApiClient:
def __init__(self, username: str, api_key: str) -> None:
self.api_key = api_key
self.username = username
self.base_url = "https://app.statuscake.com/API/"

def __get(self, endpoint: str, params: dict[str, str]) -> requests.Response:
url = f"{self.base_url}{endpoint}"

logger.debug(f"Fetching {url} with params: {params}")
response = requests.get(url, auth=(self.username, self.api_key), params=params)
response.raise_for_status()

logger.debug(f"Response: {response.content}")
return response

def list_maintenance_windows(self) -> list[dict[str, Any]]:
try:
response: requests.Response = self.__get("Maintenance", {"state": "ACT"})
return response.json()["data"]

except requests.exceptions.HTTPError as e:
if e.response.status_code == 404:
raise NotFoundException(e)
elif e.response.status_code == 402:
raise PaymentRequiredException(e)

raise e

0 comments on commit dbbd072

Please sign in to comment.