Skip to content

Commit b20527a

Browse files
authored
Feat aws manager (#12)
* feat: uses aws secrets manager
1 parent 43f51ee commit b20527a

6 files changed

Lines changed: 19 additions & 32 deletions

File tree

aind-smartsheet-service-server/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ readme = "README.md"
1717
dynamic = ["version"]
1818

1919
dependencies = [
20-
'aind-settings-utils>=0.0.3',
20+
'aind-settings-utils>=0.1.0',
2121
'smartsheet-python-sdk>=3.0',
2222
'pydantic>=2.0',
2323
'pydantic-settings>=2.0',

aind-smartsheet-service-server/src/aind_smartsheet_service_server/configs.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
from typing import Optional
44

5-
from aind_settings_utils.aws import (
6-
ParameterStoreAppBaseSettings,
7-
)
5+
from aind_settings_utils.aws import SecretsManagerBaseSettings
86
from pydantic import Field, RedisDsn, SecretStr
97
from pydantic_settings import SettingsConfigDict
108

119

12-
class Settings(ParameterStoreAppBaseSettings):
10+
class Settings(SecretsManagerBaseSettings):
1311
"""Smartsheet configs with client settings and sheet IDs"""
1412

1513
access_token: SecretStr = Field(
@@ -33,11 +31,7 @@ class Settings(ParameterStoreAppBaseSettings):
3331
..., description="SmartSheet ID of protocols info"
3432
)
3533
redis_url: Optional[RedisDsn] = Field(default=None)
36-
model_config = SettingsConfigDict(
37-
env_prefix="SMARTSHEET_", case_sensitive=False
38-
)
34+
model_config = SettingsConfigDict(env_prefix="SMARTSHEET_")
3935

4036

41-
def get_settings() -> Settings:
42-
"""Return a settings object"""
43-
return Settings()
37+
settings = Settings()

aind-smartsheet-service-server/src/aind_smartsheet_service_server/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from redis.asyncio import from_url # noqa
1515

1616
from aind_smartsheet_service_server import __version__ as service_version
17-
from aind_smartsheet_service_server.configs import get_settings
17+
from aind_smartsheet_service_server.configs import settings
1818
from aind_smartsheet_service_server.route import router
1919

2020
# The log level can be set by adding an environment variable before launch.
@@ -32,7 +32,6 @@
3232
@asynccontextmanager
3333
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
3434
"""Init cache and add to lifespan of app"""
35-
settings = get_settings()
3635
if settings.redis_url is not None:
3736
redis = from_url(settings.redis_url.unicode_string())
3837
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")

aind-smartsheet-service-server/src/aind_smartsheet_service_server/route.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from asyncio import to_thread
44
from typing import List, Optional
55

6-
from fastapi import APIRouter, Depends, Query, status
6+
from fastapi import APIRouter, Query, status
77
from fastapi_cache.decorator import cache
88
from smartsheet import Smartsheet
99

10-
from aind_smartsheet_service_server.configs import Settings, get_settings
10+
from aind_smartsheet_service_server.configs import settings
1111
from aind_smartsheet_service_server.handler import SheetHandler
1212
from aind_smartsheet_service_server.models import (
1313
FundingModel,
@@ -20,12 +20,11 @@
2020

2121

2222
@cache(expire=120)
23-
async def get_smartsheet(settings: Settings, sheet_id: int) -> str:
23+
async def get_smartsheet(sheet_id: int) -> str:
2424
"""
2525
Download and cache smartsheet object as a json string.
2626
Parameters
2727
----------
28-
settings : Settings
2928
sheet_id : int
3029
3130
Returns
@@ -66,7 +65,6 @@ def get_health() -> HealthCheck:
6665
response_model=List[FundingModel],
6766
)
6867
async def get_funding(
69-
settings: Settings = Depends(get_settings),
7068
project_name: Optional[str] = Query(
7169
default=None,
7270
openapi_examples={
@@ -95,7 +93,7 @@ async def get_funding(
9593
"""
9694

9795
sheet_id = settings.funding_id
98-
raw_sheet = await get_smartsheet(settings=settings, sheet_id=sheet_id)
96+
raw_sheet = await get_smartsheet(sheet_id=sheet_id)
9997
handler = SheetHandler(raw_sheet=raw_sheet)
10098
funding_models = handler.get_project_funding_info(
10199
project_name=project_name,
@@ -108,13 +106,13 @@ async def get_funding(
108106
"/project_names",
109107
response_model=List[str],
110108
)
111-
async def get_project_names(settings: Settings = Depends(get_settings)):
109+
async def get_project_names():
112110
"""
113111
## Project Names
114112
Returns a list of project names.
115113
"""
116114
sheet_id = settings.funding_id
117-
raw_sheet = await get_smartsheet(settings=settings, sheet_id=sheet_id)
115+
raw_sheet = await get_smartsheet(sheet_id=sheet_id)
118116
handler = SheetHandler(raw_sheet=raw_sheet)
119117
content = handler.get_project_names()
120118
return content
@@ -137,15 +135,14 @@ async def get_protocols(
137135
),
138136
}
139137
},
140-
),
141-
settings: Settings = Depends(get_settings),
138+
)
142139
):
143140
"""
144141
## Protocols
145142
Returns protocols given a name.
146143
"""
147144
sheet_id = settings.protocols_id
148-
raw_sheet = await get_smartsheet(settings=settings, sheet_id=sheet_id)
145+
raw_sheet = await get_smartsheet(sheet_id=sheet_id)
149146
handler = SheetHandler(raw_sheet=raw_sheet)
150147
content = handler.get_protocols_info(protocol_name=protocol_name)
151148
return content
@@ -165,15 +162,14 @@ async def get_perfusions(
165162
"value": "689418",
166163
}
167164
},
168-
),
169-
settings: Settings = Depends(get_settings),
165+
)
170166
):
171167
"""
172168
## Perfusions
173169
Returns perfusions for a given subject_id.
174170
"""
175171
sheet_id = settings.perfusions_id
176-
raw_sheet = await get_smartsheet(settings=settings, sheet_id=sheet_id)
172+
raw_sheet = await get_smartsheet(sheet_id=sheet_id)
177173
handler = SheetHandler(raw_sheet=raw_sheet)
178174
content = handler.get_perfusions_info(subject_id=subject_id)
179175
return content

aind-smartsheet-service-server/tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from fastapi.testclient import TestClient
1111
from pydantic import RedisDsn
1212

13-
from aind_smartsheet_service_server.configs import get_settings
13+
from aind_smartsheet_service_server.configs import settings
1414

1515
RESOURCES_DIR = Path(os.path.dirname(os.path.realpath(__file__))) / "resources"
1616

@@ -62,13 +62,12 @@ def client_with_redis() -> Generator[TestClient, Any, None]:
6262
# Import moved to be able to mock cache
6363
from aind_smartsheet_service_server.main import app
6464

65-
settings = get_settings()
6665
settings_with_redis = settings.model_copy(
6766
update={"redis_url": RedisDsn("redis://example.com:1234")}, deep=True
6867
)
6968
with (
7069
patch(
71-
"aind_smartsheet_service_server.main.get_settings",
70+
"aind_smartsheet_service_server.main.settings",
7271
return_value=settings_with_redis,
7372
),
7473
patch(

aind-smartsheet-service-server/tests/test_route.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pytest
66
from starlette.testclient import TestClient
77

8-
from aind_smartsheet_service_server.configs import Settings
98
from aind_smartsheet_service_server.route import get_smartsheet
109

1110

@@ -25,7 +24,7 @@ async def test_get_smartsheet(self, mock_get_sheet: MagicMock):
2524
mock_sheet = MagicMock()
2625
mock_sheet.to_json.return_value = '{"a": "b"}'
2726
mock_get_sheet.return_value = mock_sheet
28-
sheet = await get_smartsheet(settings=Settings(), sheet_id=0)
27+
sheet = await get_smartsheet(sheet_id=0)
2928
mock_get_sheet.assert_has_calls([call(0), call().to_json()])
3029
assert '{"a": "b"}' == sheet
3130

0 commit comments

Comments
 (0)