Coverage for src/receptiviti/status.py: 100%
28 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-20 11:35 -0500
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-20 11:35 -0500
1"""Check the status of the API."""
3import os
4import re
5from typing import Union
7import requests
9from receptiviti.readin_env import readin_env
12def status(
13 url: str = os.getenv("RECEPTIVITI_URL", ""),
14 key: str = os.getenv("RECEPTIVITI_KEY", ""),
15 secret: str = os.getenv("RECEPTIVITI_SECRET", ""),
16 dotenv: Union[bool, str] = False,
17 verbose=True,
18) -> Union[requests.Response, None]:
19 """
20 Check the API's status.
22 Ping the Receptiviti API to see if it's available, and if your credentials are valid.
24 Args:
25 url (str): The URL of the API.
26 key (str): Your API key.
27 secret (str): Your API secret.
28 dotenv (bool | str): Path to a .env file to read environment variables from, or `True`
29 to look for a file in the current directory.
30 verbose (bool): If `False`, will not print status messages.
32 Returns:
33 Response from the API server.
35 Examples:
36 >>> receptiviti.status()
37 """
38 if dotenv is not None and dotenv:
39 readin_env("." if isinstance(dotenv, bool) else dotenv)
40 if not url:
41 url = os.getenv("RECEPTIVITI_URL", "https://api.receptiviti.com")
42 if not key:
43 key = os.getenv("RECEPTIVITI_KEY", "")
44 if not secret:
45 secret = os.getenv("RECEPTIVITI_SECRET", "")
46 url = ("https://" if re.match("http", url, re.I) is None else "") + re.sub(
47 "/[Vv]\\d(?:/.*)?$|/+$", "", url
48 )
49 if re.match("https?://[^.]+[.:][^.]", url, re.I) is None:
50 raise TypeError("`url` does not appear to be valid: " + url)
51 try:
52 res = requests.get(url.lower() + "/v1/ping", auth=(key, secret), timeout=9999)
53 except requests.exceptions.RequestException:
54 if verbose:
55 print("Status: ERROR\nMessage: URL is unreachable")
56 return None
57 content = res.json() if res.text[:1] == "{" else {"message": res.text}
58 if verbose:
59 print("Status: " + ("OK" if res.status_code == 200 else "ERROR"))
60 print(
61 "Message: "
62 + (
63 str(res.status_code)
64 + (" (" + str(content["code"]) + ")" if "code" in content else "")
65 + ": "
66 + content["pong" if "pong" in content else "message"]
67 )
68 )
69 return res