|
| 1 | +import logging |
| 2 | + |
| 3 | +from feldera.rest.config import Config |
| 4 | + |
| 5 | +from feldera.rest.errors import FelderaAPIError, FelderaTimeoutError, FelderaCommunicationError |
| 6 | + |
| 7 | +import json |
| 8 | +import requests |
| 9 | +from typing import Callable, Optional, Any, Union, Mapping, Sequence, List |
| 10 | + |
| 11 | + |
| 12 | +class HttpRequests: |
| 13 | + def __init__(self, config: Config) -> None: |
| 14 | + self.config = config |
| 15 | + self.headers = { |
| 16 | + "User-Agent": "feldera-python-sdk/v1" |
| 17 | + } |
| 18 | + if self.config.api_key: |
| 19 | + self.headers["Authorization"] = f"Bearer {self.config.api_key}" |
| 20 | + |
| 21 | + def send_request( |
| 22 | + self, |
| 23 | + http_method: Callable, |
| 24 | + path: str, |
| 25 | + body: Optional[ |
| 26 | + Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str] |
| 27 | + ] = None, |
| 28 | + content_type: str = "application/json", |
| 29 | + params: Optional[Mapping[str, Any]] = None, |
| 30 | + stream: bool = False, |
| 31 | + ) -> Any: |
| 32 | + """ |
| 33 | + :param http_method: The HTTP method to use. Takes the equivalent `requests.*` module. (Example: `requests.get`) |
| 34 | + :param path: The path to send the request to. |
| 35 | + :param body: The HTTP request body. |
| 36 | + :param content_type: The value for `Content-Type` HTTP header. "application/json" by default. |
| 37 | + :param params: The query parameters part of this request. |
| 38 | + :param stream: True if the response is expected to be a HTTP stream. |
| 39 | + """ |
| 40 | + self.headers["Content-Type"] = content_type |
| 41 | + |
| 42 | + try: |
| 43 | + timeout = self.config.timeout |
| 44 | + headers = self.headers |
| 45 | + |
| 46 | + request_path = self.config.url + "/" + self.config.version + path |
| 47 | + |
| 48 | + logging.debug( |
| 49 | + "sending %s request to: %s with headers: %s, and params: %s", |
| 50 | + http_method.__name__, request_path, str(headers), str(params) |
| 51 | + ) |
| 52 | + |
| 53 | + if http_method.__name__ == "get": |
| 54 | + request = http_method( |
| 55 | + request_path, |
| 56 | + timeout=timeout, |
| 57 | + headers=headers, |
| 58 | + params=params, |
| 59 | + ) |
| 60 | + elif isinstance(body, bytes): |
| 61 | + request = http_method( |
| 62 | + request_path, |
| 63 | + timeout=timeout, |
| 64 | + headers=headers, |
| 65 | + data=body, |
| 66 | + params=params, |
| 67 | + stream=stream, |
| 68 | + ) |
| 69 | + else: |
| 70 | + request = http_method( |
| 71 | + request_path, |
| 72 | + timeout=timeout, |
| 73 | + headers=headers, |
| 74 | + data=json.dumps(body) if body else "" if body == "" else "null", |
| 75 | + params=params, |
| 76 | + stream=stream, |
| 77 | + ) |
| 78 | + if stream: |
| 79 | + return request |
| 80 | + resp = self.__validate(request) |
| 81 | + logging.debug("got response: %s", str(resp)) |
| 82 | + return resp |
| 83 | + |
| 84 | + except requests.exceptions.Timeout as err: |
| 85 | + raise FelderaTimeoutError(str(err)) from err |
| 86 | + except requests.exceptions.ConnectionError as err: |
| 87 | + raise FelderaCommunicationError(str(err)) from err |
| 88 | + |
| 89 | + def get( |
| 90 | + self, |
| 91 | + path: str, |
| 92 | + params: Optional[Mapping[str, Any]] = None |
| 93 | + ) -> Any: |
| 94 | + return self.send_request(requests.get, path, params) |
| 95 | + |
| 96 | + def post( |
| 97 | + self, |
| 98 | + path: str, |
| 99 | + body: Optional[ |
| 100 | + Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str] |
| 101 | + ] = None, |
| 102 | + content_type: Optional[str] = "application/json", |
| 103 | + params: Optional[Mapping[str, Any]] = None, |
| 104 | + stream: bool = False, |
| 105 | + ) -> Any: |
| 106 | + return self.send_request(requests.post, path, body, content_type, params, stream=stream) |
| 107 | + |
| 108 | + def patch( |
| 109 | + self, |
| 110 | + path: str, |
| 111 | + body: Optional[ |
| 112 | + Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str] |
| 113 | + ] = None, |
| 114 | + content_type: Optional[str] = "application/json", |
| 115 | + params: Optional[Mapping[str, Any]] = None |
| 116 | + ) -> Any: |
| 117 | + return self.send_request(requests.patch, path, body, content_type, params) |
| 118 | + |
| 119 | + def put( |
| 120 | + self, |
| 121 | + path: str, |
| 122 | + body: Optional[ |
| 123 | + Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str] |
| 124 | + ] = None, |
| 125 | + content_type: Optional[str] = "application/json", |
| 126 | + params: Optional[Mapping[str, Any]] = None |
| 127 | + ) -> Any: |
| 128 | + return self.send_request(requests.put, path, body, content_type, params) |
| 129 | + |
| 130 | + def delete( |
| 131 | + self, |
| 132 | + path: str, |
| 133 | + body: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str]]] = None, |
| 134 | + params: Optional[Mapping[str, Any]] = None |
| 135 | + ) -> Any: |
| 136 | + return self.send_request(requests.delete, path, body, params=params) |
| 137 | + |
| 138 | + @staticmethod |
| 139 | + def __to_json(request: requests.Response) -> Any: |
| 140 | + if request.content == b"": |
| 141 | + return request |
| 142 | + return request.json() |
| 143 | + |
| 144 | + @staticmethod |
| 145 | + def __validate(request: requests.Response) -> Any: |
| 146 | + try: |
| 147 | + request.raise_for_status() |
| 148 | + resp = HttpRequests.__to_json(request) |
| 149 | + return resp |
| 150 | + except requests.exceptions.HTTPError as err: |
| 151 | + raise FelderaAPIError(str(err), request) from err |
0 commit comments