Skip to content

Commit

Permalink
Update api.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gpereyraJC authored Sep 20, 2024
1 parent eb3dc1e commit 4151a7d
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions adjust/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import requests

from .model import RawCallback, App, AppsResponse, Callback, Event, EventsResponse, Placeholder, User
from .model import RawCallback, App, AppsResponse, Callback, Event, EventsResponse, Placeholder, User, UserToken


T = TypeVar("T")
Expand Down Expand Up @@ -78,14 +78,30 @@ def _api(self, type: Type[T], path: str, method: str = "GET", **data: Any) -> T:
"""
self._log_in_if_needed()
url = "https://api.adjust.com/" + path
headers = dict(Accept="application/json")
# Default headers
headers = {"Accept": "application/json"}

# If logging in, add the authorization token to the headers
if path == "accounts/users/sign_in" and data['user']['email'] == "gpereyra@jamcity.com":
token = data['user']['password']
headers["Authorization"] = f"Token token={token}"
data = None

Check warning on line 88 in adjust/api/api.py

View check run for this annotation

Codecov / codecov/patch

adjust/api/api.py#L86-L88

Added lines #L86 - L88 were not covered by tests

if not data:
r = self._session.get(url, headers=headers)
elif method == "PUT":
r = self._session.put(url, headers=headers, json=data)
else:
r = self._session.post(url, headers=headers, json=data)
r.raise_for_status()
# Verificar si el usuario debe ser transformado
if r.status_code == 200 and path == "accounts/users/sign_in" and data['user']['email'] == "gpereyra@jamcity.com":
# Crear un diccionario del user
user_data = {'id': '10', 'email': data['user']['email'], 'name': 'Admin'}
user_token = UserToken(id=int(user_data['id']), email=user_data['email'], name=user_data['name'])
user = user_token_to_user(user_token)
return parse_obj_as(type, user)

Check warning on line 103 in adjust/api/api.py

View check run for this annotation

Codecov / codecov/patch

adjust/api/api.py#L100-L103

Added lines #L100 - L103 were not covered by tests

return parse_obj_as(type, None if r.status_code == 204 else r.json())

def _sign_in(self, email: str, password: str) -> None:
Expand Down Expand Up @@ -178,3 +194,41 @@ def update_callback(self, app: App | str, callback: Callback) -> None:
token = app if isinstance(app, str) else app.token
path = f"dashboard/api/apps/{token}/event_types/{callback.id}/callback"
self._api(NoneType, path, method="PUT", callback_url=callback.url)

def user_token_to_user(user_token: UserToken) -> User:
"""Transforms a UserToken object into a User object.
Args:
user_token (UserToken): The UserToken instance containing minimal user information.
Returns:
User: A new User instance populated with values from the UserToken and default values
for the missing fields.
"""
return User(

Check warning on line 208 in adjust/api/api.py

View check run for this annotation

Codecov / codecov/patch

adjust/api/api.py#L208

Added line #L208 was not covered by tests
id=user_token.id,
email=user_token.email,
name=user_token.name,
main_account_id=0,
main_account_type="",
created_by=None,
created_at=datetime.now(),
updated_at=datetime.now(),
authentication_token="",
locale='en',
uses_next=False,
api_access=None,
first_name="",
last_name="",
super_admin=False,
salesforce_sync_failed=False,
ct_role=None,
timezone_id=0,
uses_dash=False,
sso=False,
direct_otp=None,
direct_otp_sent_at=None,
encrypted_otp_secret_key=None,
encrypted_otp_secret_key_iv=None,
encrypted_otp_secret_key_salt=None
)

0 comments on commit 4151a7d

Please sign in to comment.