Skip to content

Commit

Permalink
Readd http client
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tonic committed Feb 26, 2024
1 parent 8ebf556 commit 73bbd48
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
Empty file.
101 changes: 101 additions & 0 deletions tonic_ragas_logger/utils/http_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from typing import Any, Dict, Optional, Union
import requests
from urllib3.exceptions import InsecureRequestWarning # type: ignore

requests.packages.urllib3.disable_warnings( # type: ignore
category=InsecureRequestWarning
)


class HttpClient:
"""Client for handling requests to Tonic Validate instance.
Parameters
----------
base_url : str
URL to Tonic Validate instance.
access_token : str
The API token associated with your Tonic Validate account.
"""

def __init__(self, base_url: str, access_token: Optional[str] = None):
self.base_url = base_url
self.headers = None
if access_token is not None:
self.headers = {"Authorization": f"Bearer {access_token}"}

def http_get(
self, url: str, params: Dict[Any, Any] = {}, timeout: Union[int, None] = None
) -> Any:
"""Make a get request.
Parameters
----------
url : str
URL to make get request. Is appended to self.base_url.
params: dict
Passed as the params parameter of the requests.get request.
"""
res = requests.get(
self.base_url + url,
params=params,
headers=self.headers,
verify=False,
timeout=timeout,
)
res.raise_for_status()
return res.json()

def http_post(
self,
url: str,
params: Dict[Any, Any] = {},
data: Dict[Any, Any] = {},
timeout: Union[int, None] = None,
) -> Any:
"""Make a post request.
Parameters
----------
url : str
URL to make the post request. Is appended to self.base_url.
params: dict
Passed as the params parameter of the requests.post request.
data: dict
Passed as the data parameter of the requests.post request.
"""
res = requests.post(
self.base_url + url,
params=params,
json=data,
headers=self.headers,
verify=False,
timeout=timeout,
)
res.raise_for_status()
return res.json()

def http_put(
self, url: str, params: Dict[Any, Any] = {}, data: Dict[Any, Any] = {}
) -> Any:
"""Make a put request.
Parameters
----------
url : str
URL to make the put request. Is appended to self.base_url.
params: dict
Passed as the params parameter of the requests.put request.
data: dict
Passed as the data parameter of the requests.put request.
"""
res = requests.put(
self.base_url + url,
params=params,
json=data,
headers=self.headers,
verify=False,
)
res.raise_for_status()
return res.json()

0 comments on commit 73bbd48

Please sign in to comment.