@@ -39,12 +39,21 @@ def __init__(self, hosted: bool = False) -> None:
3939 self ._hosted = self ._hosted or hosted
4040 self .semaphore = asyncio .Semaphore (N_CONCURRENT_VIEWER_REQUESTS )
4141 self ._async_client = None
42+ self .api_key = os .environ .get ("BESPOKE_API_KEY" )
43+ if self .api_key :
44+ logger .info ("Authenticated with Curator API key" )
45+ self ._headers = {"Authorization" : f"Bearer { self .api_key } " } if self .api_key else {}
4246
4347 @property
4448 def session (self ):
4549 """Get the session ID."""
4650 return self ._session
4751
52+ @property
53+ def authenticated (self ):
54+ """Check if the client is authenticated."""
55+ return self ._authenticated
56+
4857 @property
4958 def hosted (self ):
5059 """Check if the client is hosted."""
@@ -67,7 +76,7 @@ def create_session(self, metadata: t.Dict, session_id: str | None = None) -> str
6776 return self .session
6877 metadata .update ({"status" : _SessionStatus .STARTED })
6978
70- response = requests .post (f"{ BASE_CLIENT_URL } /sessions" , json = metadata )
79+ response = requests .post (f"{ BASE_CLIENT_URL } /sessions" , json = metadata , headers = self . _headers )
7180
7281 if response .status_code == 200 :
7382 self ._session = response .json ().get ("session_id" )
@@ -79,7 +88,7 @@ def create_session(self, metadata: t.Dict, session_id: str | None = None) -> str
7988
8089 async def _update_state (self ):
8190 async with httpx .AsyncClient () as client :
82- response = await client .put (f"{ BASE_CLIENT_URL } /sessions/{ self .session } " , json = {"status" : self ._state })
91+ response = await client .put (f"{ BASE_CLIENT_URL } /sessions/{ self .session } " , json = {"status" : self ._state }, headers = self . _headers )
8392 if response .status_code != 200 :
8493 logger .debug (f"Failed to update session status: { response .status_code } , { response .text } " )
8594
@@ -113,7 +122,7 @@ async def stream_response(self, response_data: str, idx: int):
113122
114123 response_data = json .dumps ({"response_data" : response_data })
115124 async with self .semaphore :
116- response = await self ._async_client .post (f"{ BASE_CLIENT_URL } /sessions/{ self .session } /responses/{ idx } " , data = response_data )
125+ response = await self ._async_client .post (f"{ BASE_CLIENT_URL } /sessions/{ self .session } /responses/{ idx } " , data = response_data , headers = self . _headers )
117126
118127 if response .status_code != 200 :
119128 logger .debug (f"Failed to stream response to curator Viewer: { response .status_code } , { response .text } " )
0 commit comments