Skip to content

Commit

Permalink
enhance http methods
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Sep 27, 2024
1 parent 1a1dd75 commit c528099
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions keel_telegram_bot/client/api_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import enum
import logging
from collections import namedtuple
from typing import List

import requests
Expand All @@ -13,9 +15,11 @@

LOGGER = logging.getLogger(__name__)

GET = "get"
POST = "post"
PUT = "put"

class HttpMethod(namedtuple('HttpMethod', 'name method'), enum.Enum):
GET = "get", requests.get
POST = "post", requests.post
PUT = "put", requests.put


class KeelApiClient:
Expand All @@ -35,7 +39,7 @@ def get_resources(self) -> List[Resource]:
"""
Returns a list of all resources
"""
response = self._do_request(GET, self._base_url + "/v1/resources")
response = self._do_request(HttpMethod.GET, self._base_url + "/v1/resources")
result = [Resource.from_dict(resource) for resource in response]
return result

Expand All @@ -53,7 +57,7 @@ def get_tracked(self) -> List[TrackedImage]:
"""
Returns a list of all tracked images
"""
response = self._do_request(GET, self._base_url + "/v1/tracked")
response = self._do_request(HttpMethod.GET, self._base_url + "/v1/tracked")
result = [TrackedImage.from_dict(tracked) for tracked in response]
return result

Expand All @@ -65,7 +69,7 @@ def set_tracked(self, identifier: str, provider: Provider, trigger: Trigger, sch
:param trigger: the trigger of the image
:param schedule: the schedule of the image
"""
self._do_request(PUT, self._base_url + "/v1/tracked", json={
self._do_request(HttpMethod.PUT, self._base_url + "/v1/tracked", json={
"identifier": identifier,
"provider": provider.value,
"trigger": trigger.value,
Expand All @@ -81,7 +85,7 @@ def set_required_approvals_count(
:param provider: the name of the voter
:param votes_required: the required approvals count
"""
self._do_request(PUT, self._base_url + "/v1/approvals", json={
self._do_request(HttpMethod.PUT, self._base_url + "/v1/approvals", json={
"identifier": identifier,
"provider": provider.value,
"votesRequired": votes_required,
Expand All @@ -93,7 +97,7 @@ def get_approvals(self, rejected: bool = None, archived: bool = None) -> List[Ap
:param archived: True for archived, False for not archived, None for all
:return: a list of all approvals matching criteria
"""
response = self._do_request(GET, self._base_url + "/v1/approvals")
response = self._do_request(HttpMethod.GET, self._base_url + "/v1/approvals")
result = [Approval.from_dict(approval) for approval in response]

if rejected is not None:
Expand Down Expand Up @@ -138,7 +142,7 @@ def _run_approval_action(self, id: str, identifier: str, voter: str, action: Act
:param voter: name of the voter
:param action: the action to perform
"""
self._do_request(POST, self._base_url + "/v1/approvals", json={
self._do_request(HttpMethod.POST, self._base_url + "/v1/approvals", json={
"id": id,
"identifier": identifier,
"voter": voter,
Expand All @@ -149,15 +153,15 @@ def get_stats(self) -> DailyStats:
"""
Returns the stats
"""
result = self._do_request(GET, self._base_url + "/v1/stats")
result = self._do_request(HttpMethod.GET, self._base_url + "/v1/stats")
return DailyStats.from_dict(result)

def _do_request(self, method: str = GET, url: str = "/", params: dict = None,
def _do_request(self, method: HttpMethod = HttpMethod.GET, url: str = "/", params: dict = None,
json: dict = None) -> list or dict or None:
"""
Executes a http request based on the given parameters
:param method: the method to use (GET, POST)
:param method: the method to use (GET, PUT, POST)
:param url: the url to use
:param params: query parameters that will be appended to the url
:param json: request body
Expand All @@ -166,16 +170,7 @@ def _do_request(self, method: str = GET, url: str = "/", params: dict = None,
headers = []
url = self._create_request_url(url, params)

if method is GET:
method = requests.get
elif method is POST:
method = requests.post
elif method is PUT:
method = requests.put
else:
raise ValueError("Unsupported method: {}".format(method))

response = method(url, headers=headers, auth=self._auth, json=json, timeout=REQUESTS_TIMEOUT)
response = method.method(url, headers=headers, auth=self._auth, json=json, timeout=REQUESTS_TIMEOUT)

response.raise_for_status()
# some responses do not return data so we just ignore the body in that case
Expand Down

0 comments on commit c528099

Please sign in to comment.