Skip to content

Commit

Permalink
Adding bearer token in compass
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush-cohere committed Sep 25, 2024
1 parent 21c1690 commit 52303f8
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions compass_sdk/compass.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(
index_url: str,
username: Optional[str] = None,
password: Optional[str] = None,
bearer_token: Optional[str] = None,
logger_level: LoggerLevel = LoggerLevel.INFO,
):
"""
Expand All @@ -87,6 +88,7 @@ def __init__(
self.username = username or os.getenv("COHERE_COMPASS_USERNAME")
self.password = password or os.getenv("COHERE_COMPASS_PASSWORD")
self.session = requests.Session()
self.bearer_token = bearer_token

self.function_call = {
"create_index": self.session.put,
Expand Down Expand Up @@ -497,18 +499,23 @@ def _send_request(
)
def _send_request_with_retry():
nonlocal error

try:

data_dict = None
if data:
if isinstance(data, BaseModel):
data_dict = data.model_dump(mode="json")
elif isinstance(data, Dict):
data_dict = data

response = self.function_call[function](
target_path, json=data_dict, auth=(self.username, self.password)
)
else:
response = self.function_call[function](target_path, auth=(self.username, self.password))
headers = None
auth = (self.username, self.password)
if self.bearer_token:
headers = {"Authorization": f"Bearer {self.bearer_token}"}
auth = None

response = self.function_call[function](target_path, json=data_dict, auth=auth, headers=headers)

if response.ok:
error = None
Expand Down

0 comments on commit 52303f8

Please sign in to comment.