ability to ping DelBar API
This commit is contained in:
0
src/delta_barth/api/__init__.py
Normal file
0
src/delta_barth/api/__init__.py
Normal file
38
src/delta_barth/api/common.py
Normal file
38
src/delta_barth/api/common.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import re
|
||||
from typing import Final
|
||||
|
||||
import requests
|
||||
from requests import Response
|
||||
|
||||
from delta_barth.errors import UnspecifiedRequestType
|
||||
from delta_barth.types import HttpRequestTypes
|
||||
|
||||
|
||||
def _strip_url_components(string: str) -> str:
|
||||
return re.sub(r"^[ /]+|[ /]+$", "", string)
|
||||
|
||||
|
||||
def combine_route(base_url: str, route: str) -> str:
|
||||
base_url = _strip_url_components(base_url)
|
||||
route = _strip_url_components(route)
|
||||
return "/".join((base_url, route))
|
||||
|
||||
|
||||
def ping(
|
||||
base_url: str,
|
||||
method: HttpRequestTypes,
|
||||
) -> Response:
|
||||
ROUTE: Final[str] = "ping"
|
||||
URL: Final = combine_route(base_url, ROUTE)
|
||||
|
||||
resp: Response
|
||||
if method == HttpRequestTypes.GET:
|
||||
resp = requests.get(URL)
|
||||
elif method == HttpRequestTypes.PUT:
|
||||
resp = requests.put(URL)
|
||||
elif method == HttpRequestTypes.DELETE:
|
||||
resp = requests.delete(URL)
|
||||
else:
|
||||
raise UnspecifiedRequestType(f"Request type {method} not defined for endpoint")
|
||||
|
||||
return resp
|
||||
2
src/delta_barth/errors.py
Normal file
2
src/delta_barth/errors.py
Normal file
@@ -0,0 +1,2 @@
|
||||
class UnspecifiedRequestType(Exception):
|
||||
"""exception raised if for a given API endpoint a not defined operation is requested"""
|
||||
@@ -5,6 +5,14 @@ from typing import TypeAlias
|
||||
import pandas as pd
|
||||
|
||||
|
||||
# ** API
|
||||
class HttpRequestTypes(enum.StrEnum):
|
||||
GET = enum.auto()
|
||||
POST = enum.auto()
|
||||
PUT = enum.auto()
|
||||
DELETE = enum.auto()
|
||||
|
||||
|
||||
# ** forecasts
|
||||
@dataclass(slots=True)
|
||||
class CustomerDataSalesForecast:
|
||||
|
||||
Reference in New Issue
Block a user