Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two minor improvements - quiet variable and using stderr #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions venmo_api/apis/auth_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from venmo_api import random_device_id, warn, confirm, AuthenticationFailedError, ApiClient
from venmo_api import random_device_id, message, warn, confirm, AuthenticationFailedError, ApiClient


class AuthenticationApi(object):
Expand All @@ -11,7 +11,7 @@ def __init__(self, api_client: ApiClient = None, device_id: str = None):
self.__device_id = device_id or random_device_id()
self.__api_client = api_client or ApiClient()

def login_with_credentials_cli(self, username: str, password: str) -> str:
def login_with_credentials_cli(self, username: str, password: str, quiet: bool=False) -> str:
"""
Pass your username and password to get an access_token for using the API.
:param username: <str> Phone, email or username
Expand All @@ -20,23 +20,26 @@ def login_with_credentials_cli(self, username: str, password: str) -> str:
"""

# Give warnings to the user about device-id and token expiration
warn("IMPORTANT: Take a note of your device-id to avoid 2-factor-authentication for your next login.")
print(f"device-id: {self.__device_id}")
warn("IMPORTANT: Your Access Token will NEVER expire, unless you logout manually (client.log_out(token)).\n"
"Take a note of your token, so you don't have to login every time.\n")
if not quiet:
warn("IMPORTANT: Take a note of your device-id to avoid 2-factor-authentication for your next login.")
message(f"device-id: {self.__device_id}")
warn("IMPORTANT: Your Access Token will NEVER expire, unless you logout manually (client.log_out(token)).")
warn("Take a note of your token, so you don't have to login every time.")

response = self.authenticate_using_username_password(username, password)

# if two-factor error
if response.get('body').get('error'):
# note: this should print to stderr even if `quiet=True`
access_token = self.__two_factor_process_cli(response=response)
self.trust_this_device()
else:
access_token = response['body']['access_token']

confirm("Successfully logged in. Note your token and device-id")
print(f"access_token: {access_token}\n"
f"device-id: {self.__device_id}")
if not quiet:
confirm("Successfully logged in. Note your token and device-id")
message(f"access_token: {access_token}")
message(f"device-id: {self.__device_id}")

return access_token

Expand Down Expand Up @@ -162,7 +165,7 @@ def trust_this_device(self, device_id=None):
method='POST')

confirm(f"Successfully added your device id to the list of the trusted devices.")
print(f"Use the same device-id: {self.__device_id} next time to avoid 2-factor-auth process.")
message(f"Use the same device-id: {self.__device_id} next time to avoid 2-factor-auth process.")

def get_device_id(self):
return self.__device_id
Expand Down
17 changes: 13 additions & 4 deletions venmo_api/utils/api_util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from venmo_api import ArgumentMissingError, User, Page
from enum import Enum
from typing import Dict, List
Expand Down Expand Up @@ -94,22 +95,30 @@ class Colors(Enum):
UNDERLINE = '\033[4m'


def warn(message):
def warn(message: str) -> None:
"""
print message in Red Color
:param message:
:return:
"""
print(Colors.WARNING.value + message + Colors.ENDC.value)
message(Colors.WARNING.value + message + Colors.ENDC.value)


def confirm(message):
def confirm(message: str) -> None:
"""
print message in Blue Color
:param message:
:return:
"""
print(Colors.OKBLUE.value + message + Colors.ENDC.value)
message(Colors.OKBLUE.value + message + Colors.ENDC.value)

def message(message: str) -> None:
"""
print message for user
: param message:
:return:
"""
print(message, file=sys.stderr)


def get_user_id(user, user_id):
Expand Down
4 changes: 2 additions & 2 deletions venmo_api/venmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def my_profile(self, force_update=False):
return self.__profile

@staticmethod
def get_access_token(username: str, password: str, device_id: str = None) -> str:
def get_access_token(username: str, password: str, device_id: str = None, quiet: bool=False) -> str:
"""
Log in using your credentials and get an access_token to use in the API
:param username: <str> Can be username, phone number (without +1) or email address.
Expand All @@ -37,7 +37,7 @@ def get_access_token(username: str, password: str, device_id: str = None) -> str
:return: <str> access_token
"""
authn_api = AuthenticationApi(api_client=ApiClient(), device_id=device_id)
return authn_api.login_with_credentials_cli(username=username, password=password)
return authn_api.login_with_credentials_cli(username=username, password=password, quiet=quiet)

@staticmethod
def log_out(access_token) -> bool:
Expand Down