Skip to content

Commit

Permalink
GUNDI-3355: MB v2 auth handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarcia13 committed Nov 7, 2024
1 parent 3bc8342 commit 8abdc92
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 27 deletions.
19 changes: 19 additions & 0 deletions app/actions/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from movebank_client import MovebankClient

from app.actions.configurations import AuthenticateConfig
from app.services.errors import ConfigurationNotFound
from app.services.utils import find_config_for_action


def get_auth_config(integration):
# Look for the login credentials, needed for any action
auth_config = find_config_for_action(
configurations=integration.configurations,
action_id="auth"
)
if not auth_config:
raise ConfigurationNotFound(
f"Authentication settings for integration {str(integration.id)} "
f"are missing. Please fix the integration setup in the portal."
)
return AuthenticateConfig.parse_obj(auth_config.data)
27 changes: 27 additions & 0 deletions app/actions/configurations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from datetime import datetime, timezone
from pydantic import Field

from app.actions import AuthActionConfiguration, PullActionConfiguration


class AuthenticateConfig(AuthActionConfiguration):
username: str
password: str


class FetchIndividualEventsConfig(PullActionConfiguration):
start_time: datetime
study_id: int
individual_id: int


class FetchStudyIndividualsConfig(PullActionConfiguration):
study_id: str = Field(
title='Movebank Study IDs',
description='ID of the desired Movebank Study.',
)
start_time: datetime = Field(
title='Start Datetime',
description='Datetime events are going to be fetched from.',
default=datetime.now(tz=timezone.utc)
)
38 changes: 38 additions & 0 deletions app/actions/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import datetime
import httpx
import logging
import stamina

import app.services.gundi as gundi_tools
import app.settings.integration as settings
import app.actions.client as client

from app.actions.configurations import AuthenticateConfig, FetchStudyIndividualsConfig, FetchIndividualEventsConfig
from app.services.activity_logger import activity_logger
from app.services.state import IntegrationStateManager


logger = logging.getLogger(__name__)


state_manager = IntegrationStateManager()


async def action_auth(integration, action_config: AuthenticateConfig):
logger.info(
f"Executing auth action with integration {integration} and action_config {action_config}..."
)
mb_client = client.MovebankClient(
base_url=integration.base_url,
username=action_config.username,
password=action_config.password
)

token = await mb_client.get_token()

if token:
logger.info(f"Auth successful for integration '{integration.name}'. Token: '{token['api-token']}'")
return True
else:
logger.error(f"Auth unsuccessful for integration {integration}.")
return False
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
movebank-client~=1.0.0
movebank-client~=1.1.0
52 changes: 26 additions & 26 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --output-file=requirements.txt requirements-base.in requirements-dev.in requirements.in
# pip-compile --output-file=requirements.txt requirements-base.in requirements.in
#
aiohappyeyeballs==2.3.5
# via aiohttp
Expand All @@ -12,17 +12,22 @@ aiosignal==1.3.1
# via aiohttp
anyio==3.7.1
# via
# asyncclick
# fastapi
# httpcore
# starlette
async-timeout==4.0.3
# via
# aiohttp
# redis
asyncclick==8.1.7.2
# via movebank-client
attrs==24.2.0
# via aiohttp
backoff==2.2.1
# via gcloud-aio-auth
# via
# gcloud-aio-auth
# movebank-client
certifi==2024.7.4
# via
# httpcore
Expand All @@ -37,15 +42,15 @@ click==8.1.7
# uvicorn
cryptography==43.0.0
# via gcloud-aio-auth
dateparser==1.2.0
# via movebank-client
environs==9.5.0
# via
# -r requirements-base.in
# gundi-client-v2
# movebank-client
exceptiongroup==1.2.2
# via
# anyio
# pytest
# via anyio
fastapi==0.103.2
# via -r requirements-base.in
frozenlist==1.4.1
Expand All @@ -56,9 +61,9 @@ gcloud-aio-auth==5.3.2
# via gcloud-aio-pubsub
gcloud-aio-pubsub==6.0.1
# via -r requirements-base.in
gundi-client-v2==2.3.4
gundi-client-v2==2.3.8
# via -r requirements-base.in
gundi-core==1.5.9
gundi-core==1.7.1
# via
# -r requirements-base.in
# gundi-client-v2
Expand All @@ -78,22 +83,16 @@ idna==3.7
# anyio
# httpx
# yarl
iniconfig==2.0.0
# via pytest
marshmallow==3.21.3
# via environs
movebank-client==1.0.0
movebank-client==1.1.0
# via -r requirements.in
multidict==6.0.5
# via
# aiohttp
# yarl
packaging==24.1
# via
# marshmallow
# pytest
pluggy==1.5.0
# via pytest
# via marshmallow
prometheus-client==0.20.0
# via gcloud-aio-pubsub
pycparser==2.22
Expand All @@ -108,23 +107,24 @@ pyjq==2.6.0
# via -r requirements-base.in
pyjwt==2.9.0
# via gcloud-aio-auth
pytest==7.4.4
# via
# -r requirements-dev.in
# pytest-asyncio
# pytest-mock
pytest-asyncio==0.21.2
# via -r requirements-dev.in
pytest-mock==3.12.0
# via -r requirements-dev.in
python-dateutil==2.9.0.post0
# via dateparser
python-dotenv==1.0.1
# via environs
python-json-logger==2.0.7
# via -r requirements-base.in
pytz==2024.2
# via dateparser
redis==5.0.8
# via -r requirements-base.in
regex==2024.11.6
# via dateparser
respx==0.20.2
# via
# gundi-client-v2
# movebank-client
six==1.16.0
# via python-dateutil
sniffio==1.3.1
# via
# anyio
Expand All @@ -136,13 +136,13 @@ starlette==0.27.0
# via fastapi
tenacity==9.0.0
# via stamina
tomli==2.0.1
# via pytest
typing-extensions==4.12.2
# via
# fastapi
# pydantic
# uvicorn
tzlocal==5.2
# via dateparser
uvicorn==0.23.2
# via -r requirements-base.in
yarl==1.9.4
Expand Down

0 comments on commit 8abdc92

Please sign in to comment.