-
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.
feat: add prolific integration (M2-8325)
- Loading branch information
Showing
6 changed files
with
82 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()" |
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 fastapi import HTTPException | ||
|
||
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.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 HTTPException(status_code=prolific_response.status_code, detail="Prolific token is invalid") | ||
|
||
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