feat(api-hooks): Implement ApiHookClient with comprehensive tests
This commit is contained in:
48
api_hook_client.py
Normal file
48
api_hook_client.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
class ApiHookClient:
|
||||
def __init__(self, base_url="http://127.0.0.1:8999"):
|
||||
self.base_url = base_url
|
||||
|
||||
def _make_request(self, method, endpoint, data=None):
|
||||
url = f"{self.base_url}{endpoint}"
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
|
||||
try:
|
||||
if method == 'GET':
|
||||
response = requests.get(url, timeout=1)
|
||||
elif method == 'POST':
|
||||
response = requests.post(url, json=data, headers=headers, timeout=1)
|
||||
else:
|
||||
raise ValueError(f"Unsupported HTTP method: {method}")
|
||||
|
||||
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
||||
return response.json()
|
||||
except requests.exceptions.Timeout:
|
||||
raise requests.exceptions.Timeout(f"Request to {endpoint} timed out.")
|
||||
except requests.exceptions.ConnectionError:
|
||||
raise requests.exceptions.ConnectionError(f"Could not connect to API hook server at {self.base_url}.")
|
||||
except requests.exceptions.HTTPError as e:
|
||||
raise requests.exceptions.HTTPError(f"HTTP error {e.response.status_code} for {endpoint}: {e.response.text}")
|
||||
except json.JSONDecodeError:
|
||||
raise ValueError(f"Failed to decode JSON from response for {endpoint}: {response.text}")
|
||||
|
||||
|
||||
def get_status(self):
|
||||
return self._make_request('GET', '/status')
|
||||
|
||||
def get_project(self):
|
||||
return self._make_request('GET', '/api/project')
|
||||
|
||||
def post_project(self, project_data):
|
||||
return self._make_request('POST', '/api/project', data={'project': project_data})
|
||||
|
||||
def get_session(self):
|
||||
return self._make_request('GET', '/api/session')
|
||||
|
||||
def post_session(self, session_entries):
|
||||
return self._make_request('POST', '/api/session', data={'session': {'entries': session_entries}})
|
||||
|
||||
def post_gui(self, gui_data):
|
||||
return self._make_request('POST', '/api/gui', data=gui_data)
|
||||
Reference in New Issue
Block a user