-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathblocking_request.py
More file actions
42 lines (30 loc) · 1.41 KB
/
blocking_request.py
File metadata and controls
42 lines (30 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Any
import requests
from horizon.startup.exceptions import InvalidPDPTokenError
class BlockingRequest:
def __init__(self, token: str | None, extra_headers: dict[str, Any] | None = None, timeout: float = 60):
self._token = token
self._extra_headers = {k: v for k, v in (extra_headers or {}).items() if v is not None}
self._timeout = timeout
def _headers(self) -> dict[str, str]:
headers = {}
if self._token is not None:
headers["Authorization"] = f"Bearer {self._token}"
headers.update(self._extra_headers)
return headers
def get(self, url: str, params=None) -> dict:
"""
utility method to send a *blocking* HTTP GET request and get the response back.
"""
response = requests.get(url, headers=self._headers(), params=params, timeout=self._timeout)
if response.status_code == 401:
raise InvalidPDPTokenError()
return response.json()
def post(self, url: str, payload: dict | None = None, params=None) -> dict:
"""
utility method to send a *blocking* HTTP POST request with a JSON payload and get the response back.
"""
response = requests.post(url, json=payload, headers=self._headers(), params=params, timeout=self._timeout)
if response.status_code == 401:
raise InvalidPDPTokenError()
return response.json()