Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: remove username and password auth #89

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions cohere/compass/clients/compass.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ def __init__(
self,
*,
index_url: str,
username: Optional[str] = None,
password: Optional[str] = None,
bearer_token: Optional[str] = None,
http_session: Optional[requests.Session] = None,
default_max_retries: int = DEFAULT_MAX_RETRIES,
Expand All @@ -101,14 +99,10 @@ def __init__(
Initialize the Compass client.

:param index_url: The base URL for the index API.
:param username (optional): The username for authentication.
:param password (optional): The password for authentication.
:param bearer_token (optional): The bearer token for authentication.
:param http_session (optional): An optional HTTP session to use for requests.
"""
self.index_url = index_url
self.username = username or os.getenv("COHERE_COMPASS_USERNAME")
self.password = password or os.getenv("COHERE_COMPASS_PASSWORD")
self.session = http_session or requests.Session()
self.bearer_token = bearer_token

Expand Down Expand Up @@ -845,7 +839,7 @@ def update_group_authorization(
return PutDocumentsResponse.model_validate(result.result)

# todo Simplify this method so we don't have to ignore the C901 complexity warning.
def _send_request( # noqa: C901
def _send_request(
self,
api_name: str,
max_retries: Optional[int] = None,
Expand Down Expand Up @@ -891,15 +885,11 @@ def _send_request_with_retry():
)

headers = None
auth = None
if self.username and self.password:
auth = (self.username, self.password)
if self.bearer_token:
headers = {"Authorization": f"Bearer {self.bearer_token}"}
auth = None

response = self.api_method[api_name](
target_path, json=data_dict, auth=auth, headers=headers
target_path, json=data_dict, headers=headers
)

if response.ok:
Expand All @@ -911,7 +901,7 @@ def _send_request_with_retry():

except requests.exceptions.HTTPError as e:
if e.response.status_code == 401:
error = "Unauthorized. Please check your username and password."
error = "Unauthorized. Please check your bearer token."
raise CompassAuthError(message=str(e))
elif 400 <= e.response.status_code < 500:
error = f"Client error occurred: {e.response.text}"
Expand Down
12 changes: 0 additions & 12 deletions cohere/compass/clients/parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Python imports
import json
import logging
import os
from collections.abc import Iterable
from concurrent.futures import ThreadPoolExecutor
from typing import Any, Callable, Optional, Union
Expand Down Expand Up @@ -63,8 +62,6 @@ def __init__(
parser_url: str,
parser_config: ParserConfig = ParserConfig(),
metadata_config: MetadataConfig = MetadataConfig(),
username: Optional[str] = None,
password: Optional[str] = None,
bearer_token: Optional[str] = None,
num_workers: int = 1,
):
Expand All @@ -84,16 +81,12 @@ def __init__(
:param metadata_config: the metadata configuration to use when processing files
if no metadata configuration is specified in the method calls (process_file
or process_files)
:param username (optional): The username for authentication.
:param password (optional): The password for authentication.
:param bearer_token (optional): The bearer token for authentication.
"""
self.parser_url = (
parser_url if not parser_url.endswith("/") else parser_url[:-1]
)
self.parser_config = parser_config
self.username = username or os.getenv("COHERE_COMPASS_USERNAME")
self.password = password or os.getenv("COHERE_COMPASS_PASSWORD")
self.bearer_token = bearer_token
self.session = requests.Session()
self.thread_pool = ThreadPoolExecutor(num_workers)
Expand Down Expand Up @@ -273,18 +266,13 @@ def process_file(
)

headers = None
auth = None
if self.username and self.password:
auth = (self.username, self.password)
if self.bearer_token:
headers = {"Authorization": f"Bearer {self.bearer_token}"}
auth = None

res = self.session.post(
url=f"{self.parser_url}/v1/process_file",
data={"data": json.dumps(params.model_dump())},
files={"file": (filename, doc.filebytes)},
auth=auth,
headers=headers,
)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "compass-sdk"
version = "0.15.2"
version = "0.16.0"
authors = []
description = "Compass SDK"
readme = "README.md"
Expand Down