def get_my_profile(self, callback=None, force_update=False) -> Union[User, None]: """ Get my profile info and return as a <User> :return my_profile: <User> """ if self.__profile and not force_update: return self.__profile # 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 self.__profile = deserialize(response=response, data_type=User, nested_response=nested_response) return self.__profile
def search_for_users(self, query: str, callback=None, offset: int = 0, limit: int = 50, username=False) -> Union[List[User], None]: """ search for [query] in users :param query: :param callback: :param offset: :param limit: :param username: default: False; Pass True if search is by username :return users_list: <list> A list of <User> objects or empty """ resource_path = '/users' wrapped_callback = wrap_callback(callback=callback, data_type=User) params = {'query': query, 'limit': limit, 'offset': offset} # update params for querying by username if username or '@' in query: params.update({'query': query.replace('@', ''), 'type': 'username'}) response = self.__api_client.call_api(resource_path=resource_path, params=params, method='GET', callback=wrapped_callback) # Return None if threaded if callback: return return deserialize(response=response, data_type=User).set_method(method=self.search_for_users, kwargs={"query": query, "limit": limit}, current_offset=offset )
def get_user_friends_list(self, user_id: str = None, user: User = None, callback=None, page: int = 1, count: int = 1337) -> Union[User, None]: """ Get ([user_id]'s or [user]'s) friends list as a list of <User>s :return users_list: <list> A list of <User> objects or empty """ 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, count=count) # Prepare the request resource_path = f'/users/{user_id}/friends' wrapped_callback = wrap_callback(callback=callback, data_type=User) # Make the request response = self.__api_client.call_api(resource_path=resource_path, method='GET', params=params, callback=wrapped_callback) # Return None if threaded if callback: return return deserialize(response=response, data_type=User)
def get_user_transactions(self, user_id: str = None, user: User = None, callback=None, limit: int = 50, before_id=None) -> Union[Page, None]: """ Get ([user_id]'s or [user]'s) transactions visible to yourself as a list of <Transaction>s :param user_id: :param user: :param callback: :param limit: :param before_id: :return: """ user_id = get_user_id(user, user_id) params = {'limit': limit} if before_id: params['before_id'] = before_id # Prepare the request resource_path = f'/stories/target-or-actor/{user_id}' wrapped_callback = wrap_callback(callback=callback, data_type=Transaction) # Make the request response = self.__api_client.call_api(resource_path=resource_path, method='GET', params=params, callback=wrapped_callback) # Return None if threaded if callback: return return deserialize(response=response, data_type=Transaction).set_method(method=self.get_user_transactions, kwargs={"user_id": user_id})
def get_user_friends_list(self, user_id: str = None, user: User = None, callback=None, offset: int = 0, limit: int = 3337) -> Union[Page, None]: """ Get ([user_id]'s or [user]'s) friends list as a list of <User>s :return users_list: <list> A list of <User> objects or empty """ user_id = get_user_id(user, user_id) params = {"limit": limit, "offset": offset} # Prepare the request resource_path = f'/users/{user_id}/friends' wrapped_callback = wrap_callback(callback=callback, data_type=User) # Make the request response = self.__api_client.call_api(resource_path=resource_path, method='GET', params=params, callback=wrapped_callback) # Return None if threaded if callback: return return deserialize( response=response, data_type=User).set_method(method=self.get_user_friends_list, kwargs={"user_id": user_id, "limit": limit}, current_offset=offset )
def search_for_users(self, query: str, callback=None, page: int = 1, count: int = 50) -> Union[List[User], None]: """ search for [query] in users :param query: <str> :param callback: <function> :param count: <int> :param page: <int> :return users_list: <list> A list of <User> objects or empty """ resource_path = '/users' wrapped_callback = wrap_callback(callback=callback, data_type=User) offset_limit_params = self.__prepare_offset_limit_params(page_number=page, max_number_per_page=50, max_offset=9900, count=count) params = {'query': query} params.update(offset_limit_params) response = self.__api_client.call_api(resource_path=resource_path, params=params, method='GET', callback=wrapped_callback) # Return None if threaded if callback: return return deserialize(response=response, data_type=User)
def __send_or_request_money(self, amount: float, note: str, is_send_money, funding_source_id: str = None, privacy_setting: str = PaymentPrivacy.PRIVATE.value, target_user_id: int = None, target_user: User = None, callback=None) -> Union[bool, None]: """ Generic method for sending and requesting money :param amount: :param note: :param is_send_money: :param funding_source_id: :param privacy_setting: :param target_user_id: :param target_user: :param callback: :return: """ target_user_id = str(get_user_id(target_user, target_user_id)) amount = abs(amount) if not is_send_money: amount = -amount body = { "user_id": target_user_id, "audience": privacy_setting, "amount": amount, "note": note } if is_send_money: if not funding_source_id: funding_source_id = self.get_default_payment_method().id body.update({"funding_source_id": funding_source_id}) resource_path = '/payments' wrapped_callback = wrap_callback(callback=callback, data_type=None) result = self.__api_client.call_api(resource_path=resource_path, method='POST', body=body, callback=wrapped_callback) # handle 200 status code errors error_code = result['body']['data'].get('error_code') if error_code: if error_code == self.__payment_error_codes['not_enough_balance_error']: raise NotEnoughBalanceError(amount, target_user_id) error = result['body']['data'] raise GeneralPaymentError(f"{error.get('title')}\n{error.get('error_msg')}") if callback: return # if no exception raises, then it was successful return True
def __send_or_request_money( self, amount: float, note: str, is_send_money, funding_source_id: str = None, privacy_setting: str = PaymentPrivacy.PRIVATE.value, target_user_id: int = None, target_user: User = None, callback=None) -> Union[bool, None]: """ Generic method for sending and requesting money :param amount: :param note: :param is_send_money: :param funding_source_id: :param privacy_setting: :param target_user_id: :param target_user: :param callback: :return: """ target_user_id = str(get_user_id(target_user, target_user_id)) amount = abs(amount) if not is_send_money: amount = -amount body = { "user_id": target_user_id, "audience": privacy_setting, "amount": amount, "note": note } if is_send_money: if not funding_source_id: funding_source_id = self.get_default_payment_method().id body.update({"funding_source_id": funding_source_id}) resource_path = '/payments' wrapped_callback = wrap_callback(callback=callback, data_type=None) self.__api_client.call_api(resource_path=resource_path, method='POST', body=body, callback=wrapped_callback) if callback: return # if no exception raises, then it was successful return True
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, count: int = 50, before_id=None) -> Union[Transaction, None]: """ Get the transactions between two users. Note that user_one must be the owner of the access token. Otherwise it raises an unauthorized error. :param user_id_one: :param user_id_two: :param user_one: :param user_two: :param callback: :param count: :param before_id: :return: """ user_id_one = get_user_id(user_one, user_id_one) user_id_two = get_user_id(user_two, user_id_two) params = {'limit': count} if before_id: params['before_id'] = before_id # Prepare the request resource_path = f'/stories/target-or-actor/{user_id_one}/target-or-actor/{user_id_two}' wrapped_callback = wrap_callback(callback=callback, data_type=Transaction) # Make the request response = self.__api_client.call_api(resource_path=resource_path, method='GET', params=params, callback=wrapped_callback) # Return None if threaded if callback: return return deserialize(response=response, data_type=Transaction)
def get_payment_methods(self, callback=None) -> Union[List[PaymentMethod], None]: """ Get a list of available payment_methods :param callback: :return: """ wrapped_callback = wrap_callback(callback=callback, data_type=PaymentMethod) resource_path = '/payment-methods' response = self.__api_client.call_api(resource_path=resource_path, method='GET', callback=wrapped_callback) # return the thread if callback: return return deserialize(response=response, data_type=PaymentMethod)
def get_user(self, user_id: str, callback=None) -> Union[User, None]: """ Get the user profile with [user_id] :param user_id: <str>, example: '2859950549165568970' :param callback: <function> :return user: <User> <NoneType> """ # Prepare the request resource_path = f'/users/{user_id}' wrapped_callback = wrap_callback(callback=callback, data_type=User) # 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)
def __get_payments(self, action, limit, callback=None): """ Get a list of ongoing payments with the given action :return: """ wrapped_callback = wrap_callback(callback=callback, data_type=Payment) resource_path = '/payments' parameters = { "action": action, "actor": self.__profile.id, "limit": limit } response = self.__api_client.call_api(resource_path=resource_path, params=parameters, method='GET', callback=wrapped_callback) if callback: return return deserialize(response=response, data_type=Payment)
def search_for_users(self, query: str, callback=None, page: int = 1, count: int = 50, username=False) -> Union[List[User], None]: """ search for [query] in users :param query: :param callback: :param page: :param count: :param username: default: False; Pass True if search is by username :return users_list: <list> A list of <User> objects or empty """ resource_path = '/users' wrapped_callback = wrap_callback(callback=callback, data_type=User) offset_limit_params = self.__prepare_offset_limit_params( page_number=page, max_number_per_page=50, max_offset=9900, count=count) params = {'query': query} if username or '@' in query: params = {'query': query.replace('@', ''), 'type': 'username'} params.update(offset_limit_params) response = self.__api_client.call_api(resource_path=resource_path, params=params, method='GET', callback=wrapped_callback) # Return None if threaded if callback: return return deserialize(response=response, data_type=User)