Skip to content

Commit

Permalink
get_my_profile added and fixed a bug\n bug fixed with request money w…
Browse files Browse the repository at this point in the history
…hen <User> is passed
  • Loading branch information
mmohades committed Feb 22, 2020
1 parent 647c6c5 commit 5d6dc6e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
7 changes: 7 additions & 0 deletions venmo_api/apis/payment_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ def __send_or_request_money(self, amount: float,
if not target_user and not target_user_id:
raise ArgumentMissingError(arguments=('target_user_id', 'target_user'))

if not target_user_id:

if type(target_user) != User:
raise ArgumentMissingError(f"Expected {User} for target_user, but received {type(target_user)}")

target_user_id = target_user.id

amount = abs(amount)
if not is_send_money:
amount = -amount
Expand Down
22 changes: 22 additions & 0 deletions venmo_api/apis/user_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ def __init__(self, api_client):
super().__init__()
self.__api_client = api_client

def get_my_profile(self, callback=None):
"""
Get my profile info
:return my_profile: <User>
"""

# Prepare the request
resource_path = '/account'
nested_response = ['user']
wrapped_callback = wrap_callback(callback=callback,
data_type=User,
nested_response=nested_response)
# Make the request
response = self.__api_client.call_api(resource_path=resource_path,
method='GET',
callback=wrapped_callback)
# Return the thread
if callback:
return response

return deserialize(response=response, data_type=User, nested_response=nested_response)

def search_for_users(self, query: str, callback=None,
page: int = 1, count: int = 50) -> Union[List[User], Thread]:
"""
Expand Down
38 changes: 31 additions & 7 deletions venmo_api/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from random import randint, choice
from string import ascii_uppercase
from enum import Enum
from typing import Dict
from typing import Dict, List


def string_to_timestamp(utc):
Expand All @@ -20,14 +20,22 @@ def string_to_timestamp(utc):


def get_phone_model_from_json(app_json):
"""
extract the phone model from app_info json.
:param app_json:
:return:
"""
app = {1: "iPhone", 4: "Android"}
_id = app_json['id']

return app.get(int(_id)) or "undefined"


def random_device_id():

"""
Generate a random device id that can be used for logging in.
:return:
"""
BASE_DEVICE_ID = "88884260-05O3-8U81-58I1-2WA76F357GR9"

result = []
Expand All @@ -43,31 +51,37 @@ def random_device_id():
return "".join(result)


def deserialize(response: Dict, data_type):
def deserialize(response: Dict, data_type, nested_response: List[str] = None):
"""Extract one or a list of Objects from the api_client structured response.
:param response: <Dict>
:param data_type: <Generic>
:param nested_response: <List[str]> Optional. Loop through the body
:return:
"""

body = response.get('body')

if not body:
raise Exception("Can't process an empty response body.")

data = body.get('data')
for nested in nested_response:
temp = data.get(nested)
if not temp:
raise ValueError(f"Couldn't find {nested} in the {data}.")
data = temp

# Return a list of <class>
# Return a list of <class> data_type
if isinstance(data, list):
return __get_objs_from_json_list(json_list=data, data_type=data_type)

return data_type.from_json(json=data)


def wrap_callback(callback, data_type):
def wrap_callback(callback, data_type, nested_response: List[str] = None):
"""
:param callback: <function> Function that was provided by the user
:param data_type: <class> It can be either User or Transaction
:param nested_response: <List[str]> Optional. Loop through the body
:return wrapped_callback: <function> or <NoneType> The user callback wrapped for json parsing.
"""
if not callback:
Expand All @@ -78,7 +92,7 @@ def wrapper(response):
if not data_type:
return callback(True)

deserialized_data = deserialize(response=response, data_type=data_type)
deserialized_data = deserialize(response=response, data_type=data_type, nested_response=nested_response)
return callback(deserialized_data)

return wrapper
Expand All @@ -105,8 +119,18 @@ class Colors(Enum):


def warn(message):
"""
print message in Red Color
:param message:
:return:
"""
print(Colors.WARNING + message + Colors.ENDC)


def confirm(message):
"""
print message in Blue Color
:param message:
:return:
"""
print(Colors.OKBLUE + message + Colors.ENDC)

0 comments on commit 5d6dc6e

Please sign in to comment.