Skip to content

Commit

Permalink
my_profile() added to Client; fixed a bug; changes in the structure
Browse files Browse the repository at this point in the history
bug fixed with send/request money, when <User> is passed.
Made it to check for user/user_id across user_api functions
Divided utils into two
  • Loading branch information
mmohades committed Feb 22, 2020
1 parent 647c6c5 commit 744ca6e
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 165 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def requirements():

setup(
name='venmo-api',
version='0.1.0',
version='0.1.1',
author="Mark Mohades",
license="GNU General Public License v3",
url='https://github.com/mmohades/venmo',
Expand Down
8 changes: 4 additions & 4 deletions venmo_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from .models.exception import *

from .utils import (string_to_timestamp, get_phone_model_from_json, random_device_id, deserialize, wrap_callback,
warn, confirm)
from .utils.model_util import (string_to_timestamp, get_phone_model_from_json, random_device_id)
from .utils.api_client import ApiClient

from .models.json_schema import JSONSchema
from .models.user import User
from .models.transaction import Transaction
from .models.payment_method import PaymentMethod, PaymentRole, PaymentPrivacy
from .models.payment_method import (PaymentMethod, PaymentRole, PaymentPrivacy)

from .utils.api_util import (deserialize, wrap_callback, warn, get_user_id, confirm)
from .apis.auth_api import AuthenticationApi
from .apis.payment_api import PaymentApi
from .apis.user_api import UserApi
Expand All @@ -19,7 +19,7 @@
__all__ = ["AuthenticationFailedError", "InvalidArgumentError", "InvalidHttpMethodError", "ArgumentMissingError",
"JSONDecodeError", "ResourceNotFoundError", "HttpCodeError", "NoPaymentMethodFoundError",
"string_to_timestamp", "get_phone_model_from_json", "random_device_id", "deserialize", "wrap_callback",
"warn", "confirm",
"warn", "confirm", "get_user_id",
"JSONSchema", "User", "Transaction", "PaymentMethod", "PaymentRole", "PaymentPrivacy",
"ApiClient", "AuthenticationApi", "UserApi", "PaymentApi",
"Client"
Expand Down
19 changes: 9 additions & 10 deletions venmo_api/apis/payment_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from venmo_api import ApiClient
from venmo_api import User, PaymentMethod, PaymentRole, PaymentPrivacy
from venmo_api import ArgumentMissingError, NoPaymentMethodFoundError
from venmo_api import deserialize, wrap_callback
from venmo_api import NoPaymentMethodFoundError
from venmo_api import deserialize, wrap_callback, get_user_id
from threading import Thread
from typing import List, Union

Expand All @@ -12,7 +12,7 @@ def __init__(self, api_client: ApiClient):
super().__init__()
self.__api_client = api_client

def get_payment_methods(self, callback=None) -> Union[List[PaymentMethod], Thread]:
def get_payment_methods(self, callback=None) -> Union[List[PaymentMethod], None]:

wrapped_callback = wrap_callback(callback=callback,
data_type=PaymentMethod)
Expand All @@ -23,7 +23,7 @@ def get_payment_methods(self, callback=None) -> Union[List[PaymentMethod], Threa
callback=wrapped_callback)
# return the thread
if callback:
return response
return

return deserialize(response=response, data_type=PaymentMethod)

Expand All @@ -32,7 +32,7 @@ def send_money(self, amount: float,
funding_source_id: str = None,
privacy_setting: str = PaymentPrivacy.private.value,
target_user_id: int = None, target_user: User = None,
callback=None) -> Union[bool, Thread]:
callback=None) -> Union[bool, None]:
"""
:param amount: <float>
:param note: <str>
Expand All @@ -57,7 +57,7 @@ def request_money(self, amount: float,
note: str,
target_user_id: int = None, target_user: User = None,
privacy_setting: str = PaymentPrivacy.private.value,
callback=None) -> Union[bool, Thread]:
callback=None) -> Union[bool, None]:
"""
Request money from a user.
:param amount: <float> amount of money to be requested
Expand All @@ -83,7 +83,7 @@ def __send_or_request_money(self, amount: float,
funding_source_id: str = None,
privacy_setting: str = PaymentPrivacy.private.value,
target_user_id: int = None, target_user: User = None,
callback=None) -> Union[bool, Thread]:
callback=None) -> Union[bool, None]:
"""
Generic method for sending and requesting money
:param amount:
Expand All @@ -96,8 +96,7 @@ def __send_or_request_money(self, amount: float,
:param callback:
:return:
"""
if not target_user and not target_user_id:
raise ArgumentMissingError(arguments=('target_user_id', 'target_user'))
target_user_id = get_user_id(target_user, target_user_id)

amount = abs(amount)
if not is_send_money:
Expand Down Expand Up @@ -125,7 +124,7 @@ def __send_or_request_money(self, amount: float,
body=body,
callback=wrapped_callback)
if callback:
return threaded
return
# if no exception raises, then it was successful
return True

Expand Down
85 changes: 48 additions & 37 deletions venmo_api/apis/user_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from venmo_api import User
from venmo_api import Transaction
from venmo_api import InvalidArgumentError, ArgumentMissingError
from venmo_api import deserialize, wrap_callback
from threading import Thread
from venmo_api import InvalidArgumentError
from venmo_api import deserialize, wrap_callback, get_user_id
from typing import List, Union


Expand All @@ -11,8 +10,30 @@ 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 None if threaded
if callback:
return

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]:
page: int = 1, count: int = 50) -> Union[List[User], None]:
"""
:param query: <str>
:param callback: <function>
Expand All @@ -34,17 +55,17 @@ def search_for_users(self, query: str, callback=None,

response = self.__api_client.call_api(resource_path=resource_path, params=params,
method='GET', callback=wrapped_callback)
# return the Thread
# Return None if threaded
if callback:
return response
return

return deserialize(response=response, data_type=User)

def get_profile(self, user_id: str, callback=None) -> Union[User, Thread, None]:
def get_user(self, user_id: str, callback=None) -> Union[User, None]:
"""
:param user_id: <str>, example: '2859950549165568970'
:param callback: <function>
:return user: <User> <Thread> <NoneType>
:return user: <User> <NoneType>
"""

# Prepare the request
Expand All @@ -55,25 +76,22 @@ def get_profile(self, user_id: str, callback=None) -> Union[User, Thread, None]:
response = self.__api_client.call_api(resource_path=resource_path,
method='GET',
callback=wrapped_callback)
# Return the thread
# Return None if threaded
if callback:
return response
return

return deserialize(response=response, data_type=User)

def get_user_friends_list(self, user: User = None,
user_id: str = None,
callback=None,
page: int = 1,
count: int = 1337) -> Union[User, Thread, None]:
count: int = 1337) -> Union[User, None]:
"""
:return users_list: <list> A list of <User> objects or empty
"""
user_id = user_id or user.id

if not user_id:
raise ArgumentMissingError(arguments=("user", "user_id"))
user_id = get_user_id(user, user_id)
params = self.__prepare_offset_limit_params(page_number=page,
max_number_per_page=1337,
max_offset=9999999999999999999,
Expand All @@ -87,21 +105,18 @@ def get_user_friends_list(self, user: User = None,
response = self.__api_client.call_api(resource_path=resource_path,
method='GET', params=params,
callback=wrapped_callback)
# Return the Thread
# Return None if threaded
if callback:
return response
return

return deserialize(response=response, data_type=User)

def get_user_transactions(self, user: User = None, user_id: str = None,
def get_user_transactions(self, user_id: str = None, user: User = None,
callback=None,
page: int = 1,
count: int = 50) -> Union[Transaction, Thread, None]:
count: int = 50) -> Union[Transaction, None]:

user_id = user_id or user.id

if not user_id:
raise ArgumentMissingError(arguments=("user", "user_id"))
user_id = get_user_id(user, user_id)
params = self.__prepare_offset_limit_params(page_number=page,
max_number_per_page=50,
max_offset=9900,
Expand All @@ -116,26 +131,22 @@ def get_user_transactions(self, user: User = None, user_id: str = None,
response = self.__api_client.call_api(resource_path=resource_path,
method='GET', params=params,
callback=wrapped_callback)
# Return the Thread
# Return None if threaded
if callback:
return response
return

return deserialize(response=response, data_type=Transaction)

def get_transaction_between_two_users(self, user_one: User = None,
user_id_one: str = None,
user_two: User = None,
def get_transaction_between_two_users(self, user_id_one: str = None,
user_id_two: str = None,
user_one: User = None,
user_two: User = None,
callback=None,
page: int = 1,
count: int = 50) -> Union[Transaction, Thread, None]:

user_id_one = user_id_one or user_one.id
user_id_two = user_id_two or user_two.id
count: int = 50) -> Union[Transaction, None]:

if not user_id_one or not user_id_two:
raise ArgumentMissingError(arguments=("user", "user_id"),
reason="User or user_id must be provided for both users.")
user_id_one = get_user_id(user_one, user_id_one)
user_id_two = get_user_id(user_two, user_id_two)

params = self.__prepare_offset_limit_params(page_number=page,
max_number_per_page=50,
Expand All @@ -151,9 +162,9 @@ def get_transaction_between_two_users(self, user_one: User = None,
response = self.__api_client.call_api(resource_path=resource_path,
method='GET', params=params,
callback=wrapped_callback)
# Return the Thread
# Return None if threaded
if callback:
return response
return

return deserialize(response=response, data_type=Transaction)

Expand Down
2 changes: 1 addition & 1 deletion venmo_api/models/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ArgumentMissingError(Exception):
"""Raised when there is an argument missing in a function"""

def __init__(self, msg: str = None, arguments: tuple = None, reason=None):
self.msg = msg or f"One of f{arguments} must be passed to this method." + (reason or "")
self.msg = msg or f"One of {arguments} must be passed to this method." + (reason or "")
super(ArgumentMissingError, self).__init__(self.msg)


Expand Down

0 comments on commit 744ca6e

Please sign in to comment.