-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add ProlificIntegrationService class with create_prolific_integration method * Add Prolific case in the retrieve method of integration service * Add Prolific case in the creation method of integration service * Add a new UnauthorizedError
- Loading branch information
Showing
8 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
class AvailableIntegrations(str, Enum): | ||
LORIS = "LORIS" | ||
PROLIFIC = "PROLIFIC" | ||
FUTURE = "FUTURE" | ||
|
||
|
||
|
Empty file.
This file contains 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,17 @@ | ||
import json | ||
|
||
from pydantic import BaseModel | ||
|
||
from apps.integrations.db.schemas import IntegrationsSchema | ||
|
||
|
||
class ProlificIntegration(BaseModel): | ||
api_key: str | ||
|
||
@classmethod | ||
def from_schema(cls, schema: IntegrationsSchema): | ||
configuration = json.loads(schema.configuration.replace("'", '"')) | ||
return cls(api_key=configuration["api_key"]) | ||
|
||
def __repr__(self): | ||
return "ProlificIntegration()" |
This file contains 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,7 @@ | ||
from gettext import gettext as _ | ||
|
||
from apps.shared.exception import UnauthorizedError | ||
|
||
|
||
class ProlificInvalidApiTokenError(UnauthorizedError): | ||
message = _("Prolific token is invalid.") |
Empty file.
This file contains 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,39 @@ | ||
import uuid | ||
|
||
import requests | ||
|
||
from apps.integrations.crud.integrations import IntegrationsCRUD | ||
from apps.integrations.db.schemas import IntegrationsSchema | ||
from apps.integrations.domain import AvailableIntegrations | ||
from apps.integrations.prolific.domain import ProlificIntegration | ||
from apps.integrations.prolific.errors import ProlificInvalidApiTokenError | ||
from apps.users.domain import User | ||
|
||
|
||
class ProlificIntegrationService: | ||
def __init__(self, applet_id: uuid.UUID, session, user: User) -> None: | ||
self.applet_id = applet_id | ||
self.session = session | ||
self.user = user | ||
self.type = AvailableIntegrations.PROLIFIC | ||
|
||
async def create_prolific_integration(self, api_key: str) -> ProlificIntegration: | ||
prolific_response = requests.get( | ||
"https://api.prolific.com/api/v1/users/me/", | ||
headers={"Authorization": f"Token {api_key}", "Content-Type": "application/json"}, | ||
) | ||
|
||
if prolific_response.status_code != 200: | ||
raise ProlificInvalidApiTokenError() | ||
|
||
integration_schema = await IntegrationsCRUD(self.session).create( | ||
IntegrationsSchema( | ||
applet_id=self.applet_id, | ||
type=self.type, | ||
configuration={ | ||
"api_key": api_key, | ||
}, | ||
) | ||
) | ||
|
||
return ProlificIntegration.from_schema(integration_schema) |
This file contains 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 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