-
Notifications
You must be signed in to change notification settings - Fork 0
AAI-229 integrate Galaxy API into backend #28
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5d68fcf
Add config for galaxy
marius-mather d3ce911
Add galaxy vars to .env.example
marius-mather 1271ecb
Merge branch 'main' into galaxy-api
marius-mather fd2e517
Add galaxy client for API calls to Galaxy
marius-mather 3723d92
Schemas for data returned from galaxy
marius-mather b861a79
Test username_exists()
marius-mather 3f7393f
Mock admin token via dependency override
marius-mather 480f498
Docstrings for galaxy client
marius-mather 93a7658
Add data generation for galaxy tests
marius-mather 748222e
Merge branch 'main' into galaxy-api
marius-mather f181803
Dependency to get galaxy client
marius-mather d0c5c68
Need to ignore other vars when reading galaxy config from shared env …
marius-mather 23c6ba6
Check for existing username when registering for Galaxy
marius-mather c467faa
Need to ignore other vars in main settings as well
marius-mather b08ec3d
Add mock galaxy client for testing
marius-mather 99b4d99
Update test to mock call to galaxy
marius-mather 24f79da
Make sure we ignore env file in testing, mock galaxy settings where n…
marius-mather File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from typing import Annotated | ||
|
|
||
| from fastapi import Depends | ||
| from httpx import Client | ||
|
|
||
| from galaxy.config import GalaxySettings, get_galaxy_settings | ||
| from galaxy.schemas import GalaxyUserModel | ||
|
|
||
|
|
||
| class GalaxyClient: | ||
| """ | ||
| Client for making requests to the Galaxy API. | ||
| """ | ||
|
|
||
| def __init__(self, galaxy_url: str, api_key: str): | ||
| """ | ||
| :param galaxy_url: Base URL of the Galaxy instance. | ||
| :param api_key: API key for a galaxy user. | ||
| """ | ||
| self.url = galaxy_url | ||
| self.api_key = api_key | ||
| self.client = Client(base_url=self.url, headers={'x-api-key': self.api_key}) | ||
|
|
||
| def username_exists(self, username: str) -> bool: | ||
| """ | ||
| Check if a username already exists in Galaxy. | ||
| Note the user search in the current Galaxy API will return | ||
| partial matches, so we need to check the returned users | ||
| for an exact match. | ||
| """ | ||
| resp = self.client.get("/api/users", params={"f_name": username}) | ||
| returned_users = [GalaxyUserModel(**u) for u in resp.json()] | ||
| for user in returned_users: | ||
| if user.username == username: | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def get_galaxy_client(settings: Annotated[GalaxySettings, Depends(get_galaxy_settings)]): | ||
| return GalaxyClient(galaxy_url=settings.galaxy_url, api_key=settings.galaxy_api_key) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from functools import lru_cache | ||
|
|
||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
|
||
|
|
||
| class GalaxySettings(BaseSettings): | ||
| """ | ||
| Settings for the Galaxy API. | ||
|
|
||
| Note: these are currently read from the same .env file | ||
| as other settings (which pydantic-settings supports). | ||
| """ | ||
| galaxy_url: str | ||
| galaxy_api_key: str | ||
|
|
||
| model_config = SettingsConfigDict(env_file=".env", extra="ignore") | ||
|
|
||
|
|
||
| @lru_cache | ||
| def get_galaxy_settings(): | ||
| return GalaxySettings() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic import BaseModel, NaiveDatetime | ||
|
|
||
|
|
||
| class GalaxyUserModel(BaseModel): | ||
| """ | ||
| User data returned by Galaxy's users API | ||
| """ | ||
| model_class: str | ||
| id: str | ||
| username: str | ||
| email: str | ||
| deleted: bool | ||
| active: bool | ||
| last_password_change: Optional[NaiveDatetime] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from polyfactory.factories.pydantic_factory import ModelFactory | ||
|
|
||
| from galaxy.schemas import GalaxyUserModel | ||
|
|
||
|
|
||
| class GalaxyUserFactory(ModelFactory[GalaxyUserModel]): ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import httpx | ||
| import pytest | ||
|
|
||
| from galaxy.client import GalaxyClient | ||
| from tests.galaxy.data_generation import GalaxyUserFactory | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def galaxy_client(): | ||
| return GalaxyClient(galaxy_url="https://galaxy.example.com", | ||
| api_key="dummy-key") | ||
|
|
||
|
|
||
| def test_username_exists(galaxy_client, respx_mock): | ||
| user1 = GalaxyUserFactory.build(username="user1") | ||
| user2 = GalaxyUserFactory.build(username="user2") | ||
| respx_mock.get("https://galaxy.example.com/api/users").mock( | ||
| return_value=httpx.Response( | ||
| 200, | ||
| json=[user1.model_dump(mode="json"), | ||
| user2.model_dump(mode="json")] | ||
| ) | ||
| ) | ||
| assert galaxy_client.username_exists("user1") | ||
| assert not galaxy_client.username_exists("other_user") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.