Skip to content

Commit

Permalink
Add auth to librarian client
Browse files Browse the repository at this point in the history
  • Loading branch information
JBorrow committed Feb 1, 2024
1 parent dd38024 commit 89ac2a7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion hera_librarian/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ class LibrarianClient:
host: str
port: int
user: str
password: str

def __init__(self, host: str, port: int, user: str):
def __init__(self, host: str, port: int, user: str, password: str):
"""
Create a new LibrarianClient.
Expand All @@ -55,6 +56,8 @@ def __init__(self, host: str, port: int, user: str):
The port of the Librarian server.
user : str
The name of the user.
password : str
The password of the user.
"""

if host[-1] == "/":
Expand All @@ -64,6 +67,7 @@ def __init__(self, host: str, port: int, user: str):

self.port = port
self.user = user
self.password = password

def __repr__(self):
return f"Librarian Client ({self.user}) for {self.host}:{self.port}"
Expand All @@ -88,6 +92,7 @@ def from_info(cls, client_info: ClientInfo):
host=client_info.host,
port=client_info.port,
user=client_info.user,
password=client_info.password,
)

@property
Expand Down Expand Up @@ -154,6 +159,7 @@ def post(
self.resolve(endpoint),
data=data,
headers={"Content-Type": "application/json"},
auth=(self.user, self.password),
)

if str(r.status_code)[0] != "2":
Expand Down
2 changes: 2 additions & 0 deletions hera_librarian/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ClientInfo(BaseModel):
"The port of this librarian server"
host: str
"The hostname of this librarian server"
password: str
"Your password on this librarian"


class ClientSettings(BaseSettings):
Expand Down
21 changes: 20 additions & 1 deletion tests/integration_test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,25 @@ def librarian_client(server) -> LibrarianClient:
Returns a LibrarianClient connected to the server.
"""

client = LibrarianClient(host="http://localhost", port=server.id, user="test-A")
connections = json.dumps(
{
"test-A": {
"user": "test-A",
"port": server.id,
"host": "http://localhost",
"password": "test_A_password",
}
}
)

os.environ["LIBRARIAN_CLIENT_CONNECTIONS"] = connections

client = LibrarianClient(
host="http://localhost",
port=server.id,
user="test-A",
password="test_A_password",
)

yield client

Expand All @@ -88,6 +106,7 @@ def librarian_client_command_line(server):
"user": "test-B",
"port": server.id,
"host": "http://localhost",
"password": "test_B_password",
}
}
)
Expand Down
1 change: 1 addition & 0 deletions tests/integration_test/test_secondary_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_secondary_server_simple(
response = test_client.post(
"/api/v2/ping",
content=PingRequest().model_dump_json(),
auth=("test-A", "test_A_password"),
)

assert response.status_code == 200
Expand Down

0 comments on commit 89ac2a7

Please sign in to comment.