Skip to content

Commit a3858c0

Browse files
committed
fixed #19. Added BaseModel with string method
1 parent e0e78de commit a3858c0

9 files changed

Lines changed: 34 additions & 40 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ media
1414

1515
### Other ###
1616
test*.py
17+
testing
1718

1819
### Linux ###
1920
*~

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def requirements():
2222

2323
setup(
2424
name='venmo-api',
25-
version='0.2.0',
25+
version='0.2.2',
2626
author="Mark Mohades",
2727
license="GNU General Public License v3",
2828
url='https://github.com/mmohades/venmo',

venmo_api/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .utils.model_util import (string_to_timestamp, get_phone_model_from_json, random_device_id)
22
from .models.exception import *
3+
from .models.base_model import BaseModel
34
from .models.json_schema import JSONSchema
45
from .models.user import User
56
from .models.transaction import Transaction
@@ -18,6 +19,6 @@
1819
"get_phone_model_from_json", "random_device_id",
1920
"deserialize", "wrap_callback", "warn", "confirm", "get_user_id", "validate_access_token",
2021
"JSONSchema", "User", "Transaction", "Payment", "PaymentStatus", "PaymentMethod", "PaymentRole",
21-
"PaymentPrivacy", "ApiClient", "AuthenticationApi", "UserApi", "PaymentApi",
22+
"BaseModel", "PaymentPrivacy", "ApiClient", "AuthenticationApi", "UserApi", "PaymentApi",
2223
"Client"
2324
]

venmo_api/apis/payment_api.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@ def __init__(self, profile, api_client: ApiClient):
1818
"no_pending_payment_error2": 2905
1919
}
2020

21-
def get_charge_payments(self, callback=None):
21+
def get_charge_payments(self, limit=100000, callback=None):
2222
"""
2323
Get a list of charge ongoing payments (pending request money)
24+
:param limit:
2425
:param callback:
2526
:return:
2627
"""
2728
return self.__get_payments(action="charge",
29+
limit=limit,
2830
callback=callback)
2931

30-
def get_pay_payments(self, callback=None):
32+
def get_pay_payments(self, limit=100000, callback=None):
3133
"""
3234
Get a list of pay ongoing payments (pending requested money from your profile)
35+
:param limit:
3336
:param callback:
3437
:return:
3538
"""
3639
return self.__get_payments(action="pay",
40+
limit=limit,
3741
callback=callback)
3842

3943
def remind_payment(self, payment: Payment = None, payment_id: int = None) -> bool:
@@ -166,7 +170,7 @@ def __update_payment(self, action, payment_id):
166170
method='PUT',
167171
ok_error_codes=list(self.__payment_update_error_codes.values()))
168172

169-
def __get_payments(self, action, callback=None):
173+
def __get_payments(self, action, limit, callback=None):
170174
"""
171175
Get a list of ongoing payments with the given action
172176
:return:
@@ -178,7 +182,7 @@ def __get_payments(self, action, callback=None):
178182
parameters = {
179183
"action": action,
180184
"actor": self.__profile.id,
181-
"limit": 100000
185+
"limit": limit
182186
}
183187
response = self.__api_client.call_api(resource_path=resource_path,
184188
params=parameters,
@@ -248,6 +252,9 @@ def get_default_payment_method(self) -> PaymentMethod:
248252
payment_methods = self.get_payment_methods()
249253

250254
for p_method in payment_methods:
255+
if not p_method:
256+
continue
257+
251258
if p_method.role == PaymentRole.DEFAULT:
252259
return p_method
253260

venmo_api/models/base_model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class BaseModel(object):
2+
def __str__(self):
3+
return f"{type(self).__name__}:" \
4+
f" ({', '.join('%s=%s' % item for item in vars(self).items() if not item[0].startswith('_'))})"

venmo_api/models/payment.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from enum import Enum
22

3-
from venmo_api import string_to_timestamp, User
3+
from venmo_api import string_to_timestamp, User, BaseModel
44
from venmo_api import JSONSchema
55

66

7-
class Payment(object):
7+
class Payment(BaseModel):
88

99
def __init__(self, id_, actor, target, action, amount, audience, date_created, date_reminded, date_completed,
1010
note, status):
@@ -61,12 +61,6 @@ def from_json(cls, json):
6161
status=PaymentStatus(parser.get_status())
6262
)
6363

64-
def __str__(self) -> str:
65-
return '%s(%s)' % (
66-
type(self).__name__,
67-
', '.join('%s=%s' % item for item in vars(self).items())
68-
)
69-
7064

7165
class PaymentStatus(Enum):
7266
SETTLED = 'settled'

venmo_api/models/payment_method.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Dict
22
from enum import Enum
3-
from venmo_api import JSONSchema
3+
from venmo_api import JSONSchema, BaseModel
4+
import logging
45

56

6-
class PaymentMethod(object):
7+
class PaymentMethod(BaseModel):
78
def __init__(self, pid: str, p_role: str, p_name: str, p_type: str):
89
super().__init__()
910

@@ -23,23 +24,23 @@ def from_json(cls, json: Dict):
2324
p_type = payment_parser.get_payment_method_type()
2425

2526
# Get the class for this payment, must be either VenmoBalance or BankAccount
26-
payment_class = payment_type[p_type]
27+
payment_class = payment_type.get(p_type)
28+
if not payment_class:
29+
logging.warning(f"Skipped a payment_method; No schema existed for the payment_method: {p_type}")
30+
return
2731

2832
return payment_class(pid=pid,
2933
p_role=p_role,
3034
p_name=p_name,
3135
p_type=p_type)
3236

33-
def __str__(self):
34-
return f"Payment: id: {self.id}, role: {self.role}, name: {self.name}, type: {self.type}"
3537

36-
37-
class VenmoBalance(PaymentMethod):
38+
class VenmoBalance(PaymentMethod, BaseModel):
3839
def __init__(self, pid, p_role, p_name, p_type):
3940
super().__init__(pid, p_role, p_name, p_type)
4041

4142

42-
class BankAccount(PaymentMethod):
43+
class BankAccount(PaymentMethod, BaseModel):
4344
def __init__(self, pid, p_role, p_name, p_type):
4445
super().__init__(pid, p_role, p_name, p_type)
4546

venmo_api/models/transaction.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from enum import Enum
2-
from venmo_api import string_to_timestamp
2+
from venmo_api import string_to_timestamp, BaseModel
33
from venmo_api import User
44
from venmo_api import get_phone_model_from_json
55
from venmo_api import JSONSchema
66

77

8-
class Transaction(object):
8+
class Transaction(BaseModel):
99

1010
def __init__(self, story_id, payment_id, date_completed, date_created,
1111
date_updated, payment_type, amount, audience, status,
@@ -71,15 +71,6 @@ def from_json(cls, json):
7171
actor=actor,
7272
target=target)
7373

74-
def __str__(self):
75-
76-
return f'Transaction: story_id: {self.id}, payment_id: {self.payment_id}, date_completed: {self.date_completed},' \
77-
f'date_created: {self.date_created}, date_updated: {self.date_updated},' \
78-
f'payment_type: {self.payment_type}, amount: {self.amount},' \
79-
f'audience: {self.audience}, status: {self.status}, note: {self.note}, device_used: {self.device_used},\n' \
80-
f'actor_user: {self.actor},\n' \
81-
f'target_user: {self.target}\n'
82-
8374

8475
class TransactionType(Enum):
8576
PAYMENT = 'payment'

venmo_api/models/user.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from venmo_api import string_to_timestamp
1+
from venmo_api import string_to_timestamp, BaseModel
22
from venmo_api import JSONSchema
33

44

5-
class User(object):
5+
class User(BaseModel):
66

77
def __init__(self, user_id, username, first_name, last_name, display_name, phone,
88
profile_picture_url, about, date_joined, is_group, is_active):
@@ -61,8 +61,3 @@ def from_json(cls, json, is_profile=False):
6161
date_joined=date_joined_timestamp,
6262
is_group=parser.get_is_group(),
6363
is_active=parser.get_is_active())
64-
65-
def __str__(self):
66-
return f'User: id: {self.id}, username: {self.username}, first_name: {self.first_name}, last_name: {self.last_name}'\
67-
f' display_name: {self.display_name}, phone: {self.phone}, profile_picture_url: {self.profile_picture_url}, about: {self.about},'\
68-
f' joined: {self.date_joined}, is_group: {self.is_group}, is_active: {self.is_active}'

0 commit comments

Comments
 (0)