44This module provides the client for the V4 version of the AgentOps API.
55"""
66
7- from typing import Optional , Union , Dict
7+ from typing import Optional , Union , Dict , Any
88
9+ import requests
910from agentops .client .api .base import BaseApiClient
11+ from agentops .client .http .http_client import HttpClient
1012from agentops .exceptions import ApiServerException
11- from agentops .client .api .types import UploadedObjectResponse
1213from agentops .helpers .version import get_agentops_version
1314
1415
1516class V4Client (BaseApiClient ):
1617 """Client for the AgentOps V4 API"""
1718
18- auth_token : str
19+ def __init__ (self , endpoint : str ):
20+ """Initialize the V4 API client."""
21+ super ().__init__ (endpoint )
22+ self .auth_token : Optional [str ] = None
1923
2024 def set_auth_token (self , token : str ):
2125 """
@@ -36,69 +40,106 @@ def prepare_headers(self, custom_headers: Optional[Dict[str, str]] = None) -> Di
3640 Headers dictionary with standard headers and any custom headers
3741 """
3842 headers = {
39- "Authorization" : f"Bearer { self .auth_token } " ,
4043 "User-Agent" : f"agentops-python/{ get_agentops_version () or 'unknown' } " ,
4144 }
45+
46+ # Only add Authorization header if we have a token
47+ if self .auth_token :
48+ headers ["Authorization" ] = f"Bearer { self .auth_token } "
49+
4250 if custom_headers :
4351 headers .update (custom_headers )
4452 return headers
4553
46- def upload_object (self , body : Union [str , bytes ]) -> UploadedObjectResponse :
54+ def post (self , path : str , body : Union [str , bytes ], headers : Optional [ Dict [ str , str ]] = None ) -> requests . Response :
4755 """
48- Upload an object to the API and return the response .
56+ Make a POST request to the V4 API .
4957
5058 Args:
51- body: The object to upload, either as a string or bytes.
59+ path: The API path to POST to
60+ body: The request body (string or bytes)
61+ headers: Optional headers to include
62+
5263 Returns:
53- UploadedObjectResponse: The response from the API after upload.
64+ The response object
5465 """
55- if isinstance ( body , bytes ):
56- body = body . decode ( "utf-8" )
66+ url = self . _get_full_url ( path )
67+ request_headers = headers or self . prepare_headers ( )
5768
58- response = self . post ("/v4/objects/upload/" , body , self . prepare_headers () )
69+ return HttpClient . get_session (). post (url , json = { "body" : body }, headers = request_headers , timeout = 30 )
5970
60- if response . status_code != 200 :
61- error_msg = f"Upload failed: { response . status_code } "
62- try :
63- error_data = response . json ()
64- if "error" in error_data :
65- error_msg = error_data [ "error" ]
66- except Exception :
67- pass
68- raise ApiServerException ( error_msg )
71+ def upload_object ( self , body : Union [ str , bytes ]) -> Dict [ str , Any ] :
72+ "" "
73+ Upload an object to the V4 API.
74+
75+ Args :
76+ body: The object body to upload
77+
78+ Returns:
79+ Dictionary containing upload response data
6980
81+ Raises:
82+ ApiServerException: If the upload fails
83+ """
7084 try :
71- response_data = response .json ()
72- return UploadedObjectResponse (** response_data )
73- except Exception as e :
74- raise ApiServerException (f"Failed to process upload response: { str (e )} " )
85+ # Convert bytes to string for consistency with test expectations
86+ if isinstance (body , bytes ):
87+ body = body .decode ("utf-8" )
88+
89+ response = self .post ("/v4/objects/upload/" , body , self .prepare_headers ())
90+
91+ if response .status_code != 200 :
92+ error_msg = f"Upload failed: { response .status_code } "
93+ try :
94+ error_data = response .json ()
95+ if "error" in error_data :
96+ error_msg = error_data ["error" ]
97+ except :
98+ pass
99+ raise ApiServerException (error_msg )
100+
101+ try :
102+ return response .json ()
103+ except Exception as e :
104+ raise ApiServerException (f"Failed to process upload response: { str (e )} " )
105+ except requests .exceptions .RequestException as e :
106+ raise ApiServerException (f"Failed to upload object: { e } " )
75107
76- def upload_logfile (self , body : Union [str , bytes ], trace_id : int ) -> UploadedObjectResponse :
108+ def upload_logfile (self , body : Union [str , bytes ], trace_id : str ) -> Dict [ str , Any ] :
77109 """
78- Upload an log file to the API and return the response .
110+ Upload a logfile to the V4 API .
79111
80112 Args:
81- body: The log file to upload, either as a string or bytes.
113+ body: The logfile content to upload
114+ trace_id: The trace ID associated with the logfile
115+
82116 Returns:
83- UploadedObjectResponse: The response from the API after upload.
84- """
85- if isinstance (body , bytes ):
86- body = body .decode ("utf-8" )
117+ Dictionary containing upload response data
87118
88- response = self .post ("/v4/logs/upload/" , body , {** self .prepare_headers (), "Trace-Id" : str (trace_id )})
119+ Raises:
120+ ApiServerException: If the upload fails
121+ """
122+ try :
123+ # Convert bytes to string for consistency with test expectations
124+ if isinstance (body , bytes ):
125+ body = body .decode ("utf-8" )
126+
127+ headers = {** self .prepare_headers (), "Trace-Id" : str (trace_id )}
128+ response = self .post ("/v4/logs/upload/" , body , headers )
129+
130+ if response .status_code != 200 :
131+ error_msg = f"Upload failed: { response .status_code } "
132+ try :
133+ error_data = response .json ()
134+ if "error" in error_data :
135+ error_msg = error_data ["error" ]
136+ except :
137+ pass
138+ raise ApiServerException (error_msg )
89139
90- if response .status_code != 200 :
91- error_msg = f"Upload failed: { response .status_code } "
92140 try :
93- error_data = response .json ()
94- if "error" in error_data :
95- error_msg = error_data ["error" ]
96- except Exception :
97- pass
98- raise ApiServerException (error_msg )
99-
100- try :
101- response_data = response .json ()
102- return UploadedObjectResponse (** response_data )
103- except Exception as e :
104- raise ApiServerException (f"Failed to process upload response: { str (e )} " )
141+ return response .json ()
142+ except Exception as e :
143+ raise ApiServerException (f"Failed to process upload response: { str (e )} " )
144+ except requests .exceptions .RequestException as e :
145+ raise ApiServerException (f"Failed to upload logfile: { e } " )
0 commit comments