Beispiel #1
0
    def add_money_to_savings_goal(self, access_token, savings_goal_id,
                                  transaction_id, amount, currency):
        """
        Add money to a specific savings goal

        :param access_token: the oauth bearer token
        :param savings_goal_id: the saving's goal's ID
        :param transaction_id:
        :param amount: an amount in minor unit
        :param currency: the currency of the savings goal

        :return: the json response dict
        """

        type_validation(
            [access_token, savings_goal_id, transaction_id, amount, currency],
            add_money_to_savings_goal_parameter_definition)

        url = "{api_url}/api/v1/savings-goals/{goal_id}/add-money/{transaction_id}".format(
            api_url=self.options["api_url"],
            goal_id=savings_goal_id,
            transaction_id=transaction_id)

        logging.debug("PUT {url}".format(url=url))

        return request.put(url,
                           headers=default_headers(access_token),
                           data={
                               "amount": {
                                   "currency": currency,
                                   "targetCurrency": currency
                               }
                           })
Beispiel #2
0
    def get_transactions(self, access_token, from_date, to_date, source):
        """
        Makes a payment on behalf of the customer to another UK bank account using the Faster Payments network

        :param access_token: the oauth bearer token
        :param from_date: filter transactions after this date. Format: YYYY-MM-DD
        :param to_date: filter transactions before this date. Format: YYYY-MM-DD
        :param source: the transaction type (e.g. faster payments, mastercard)
                       if not specified, results are not filtered by source
        :return: the json response dict
        """

        type_validation([access_token, from_date, to_date, source],
                        get_transactions_parameter_definition)

        url = "{api_url}/api/v1/transactions{source}".format(
            api_url=self.options["api_url"], source=transaction_source(source))

        logging.debug("GET {url}".format(url=url))

        return request.get(url,
                           headers=default_headers(access_token),
                           params={
                               "from": from_date,
                               "to": to_date
                           })
Beispiel #3
0
    def get_customer(self, access_token):
        """
        Gets a customer's details

        :param access_token: the oauth bearer token
        :return: the json response dict
        """
        type_validation([access_token], get_customer_parameter_definition)
        url = "{api_url}/api/v1/customers".format(api_url=self.options["api_url"])
        logging.debug("GET {url}".format(url=url))
        return request.get(url, headers=default_headers(access_token))
Beispiel #4
0
    def list_scheduled_payments(self, access_token):
        """
        Lists the customer's scheduled payments

        :param access_token: the oauth bearer token
        :return: the json response dict
        """
        type_validation([access_token], list_scheduled_payments_parameter_definition)
        url = "{api_url}/api/v1/payments/scheduled".format(api_url=self.options["api_url"])
        logging.debug("GET {url}".format(url=url))
        return request.get(url, headers=default_headers(access_token))
Beispiel #5
0
    def get_me(self, access_token):
        """
        Retrieves the customer UUID and permissions corresponding to the access token passed

        :param access_token: the oauth bearer token
        :return: the json response dict
        """
        type_validation([access_token], get_me_parameter_definition)
        url = "{api_url}/api/v1/me".format(api_url=self.options["api_url"])
        logging.debug("GET {url}".format(url=url))
        return request.get(url, headers=default_headers(access_token))
Beispiel #6
0
    def get_balance(self, access_token):
        """
        Retrieves the customer's balance

        :param access_token: the oauth bearer token
        :return: the json response dict
        """
        type_validation([access_token], get_balance_parameter_definition)
        url = "{api_url}/api/v1/accounts/balance".format(
            api_url=self.options["api_url"])
        logging.debug("GET {url}".format(url=url))
        return request.get(url, headers=default_headers(access_token))
Beispiel #7
0
    def list_mandates(self, access_token):
        """
        Gets a list of the customer's current direct debit mandates

        :param access_token: the oauth bearer token
        :return: the json response dict
        """
        type_validation([access_token], list_mandates_parameter_definition)
        url = "{api_url}/api/v1/direct-debit/mandates".format(
            api_url=self.options["api_url"])
        logging.debug("GET {url}".format(url=url))
        return request.get(url, headers=default_headers(access_token))
Beispiel #8
0
    def list_savings_goals(self, access_token):
        """
        Gets a lists of the customer's savings goals

        :param access_token: the oauth bearer token
        :return: the json response dict
        """
        type_validation([access_token],
                        list_savings_goals_parameter_definition)
        url = "{api_url}/api/v1/savings-goals".format(
            api_url=self.options["api_url"])
        logging.debug("GET {url}".format(url=url))
        return request.get(url, headers=default_headers(access_token))
Beispiel #9
0
    def get_mandate(self, access_token, mandate_id):
        """
        Gets a specific direct debit mandates

        :param access_token: the oauth bearer token
        :param mandate_id: the unique mandate ID
        :return: the json response dict
        """
        type_validation([access_token], list_mandates_parameter_definition)
        url = "{api_url}/api/v1/direct-debit/mandates/{mandate_id}".format(
            api_url=self.options["api_url"], mandate_id=mandate_id)
        logging.debug("GET {url}".format(url=url))
        return request.get(url, headers=default_headers(access_token))
Beispiel #10
0
    def get_savings_goal(self, access_token, savings_goal_id):
        """
        Gets a specific savings goal

        :param access_token: the oauth bearer token
        :param savings_goal_id: the savings goal's ID
        :return: the json response dict
        """
        type_validation([access_token, savings_goal_id],
                        get_savings_goals_parameter_definition)
        url = "{api_url}/api/v1/savings-goals/{goal_id}".format(
            api_url=self.options["api_url"], goal_id=savings_goal_id)
        logging.debug("GET {url}".format(url=url))
        return request.get(url, headers=default_headers(access_token))
Beispiel #11
0
    def create_savings_goal(self,
                            access_token,
                            savings_goal_id,
                            name,
                            target_amount,
                            currency="GBP",
                            target_currency="GBP",
                            base64_encoded_photo=None):
        """
        Creates a savings goal

        :param access_token: the oauth bearer token
        :param savings_goal_id: the saving's goal's ID
        :param name: the name of the savings goal
        :param target_amount: the target amount in minor units (e.g. 1234 => £12.34)
        :param currency: the currency of the savings goal, defaults to 'GBP'
        :param target_currency: the target currency, defaults to 'GBP'
        :param base64_encoded_photo: base64 encoded image to associate with the goal. (optional)
        :return: the json response dict
        """

        type_validation([
            access_token, name, currency, target_amount, target_currency,
            base64_encoded_photo
        ], create_savings_goals_parameter_definition)

        data = {
            "name": name,
            "currency": currency,
            "target": {
                "targetAmount": target_amount,
                "targetCurrency": target_currency
            }
        }

        if base64_encoded_photo is not None:
            data["base64EncodedPhoto"] = base64_encoded_photo

        url = "{api_url}/api/v1/savings-goals/{goal_id}".format(
            api_url=self.options["api_url"], goal_id=savings_goal_id)

        logging.debug("PUT {url}".format(url=url))

        return request.put(url,
                           headers=default_headers(access_token),
                           data=data)
Beispiel #12
0
    def refresh_access_token(self, refresh_token):
        """
        Exchanges the authorization code for an access token

        :param refresh_token: the oauth refresh token, used when the access token
                              expires to claim a new access token.
        :return: the json response dict
        """
        type_validation([refresh_token], refresh_token_parameter_definition)

        return self.get_oauth_token({
            "refresh_token":
            refresh_token,
            "grant_type":
            REFRESH_TOKEN_GRANT_TYPE,
            "client_id":
            self.options["client_id"],
            "client_secret":
            self.options["client_secret"],
        })
Beispiel #13
0
    def make_local_payment(self, access_token, destination_account_uid, reference, amount):
        """
        Makes a payment on behalf of the customer to another UK bank account using the Faster Payments network

        :param access_token: the oauth bearer token
        :param destination_account_uid:
        :param reference: the payment reference, max. 18 characters
        :param amount: the amount to be send
        :return: the json response dict
        """
        type_validation([access_token, destination_account_uid, reference, amount],
                        make_local_payment_parameter_definition)
        url = "{api_url}/api/v1/payments/local".format(api_url=self.options["api_url"])
        logging.debug("POST {url}".format(url=url))
        return request.post(url, headers=default_headers(access_token), data={
            "destinationAccountUid": destination_account_uid,
            "payment": {
                "amount": amount,
            },
            "reference": reference
        })
Beispiel #14
0
    def get_transaction(self, access_token, transaction_id, source):
        """
        Makes a payment on behalf of the customer to another UK bank account using the Faster Payments network

        :param access_token: the oauth bearer token
        :param transaction_id: the unique transaction ID
        :param source: the transaction type (e.g. faster payments, mastercard)
                       if not specified, only generic transaction information is provided
        :return: the json response dict
        """

        type_validation([access_token, transaction_id, source],
                        get_transaction_parameter_definition)

        url = "{api_url}/api/v1/transactions{source}/{id}".format(
            api_url=self.options["api_url"],
            source=transaction_source(source),
            id=transaction_id)

        logging.debug("GET {url}".format(url=url))

        return request.get(url, headers=default_headers(access_token))
Beispiel #15
0
    def get_access_token(self, authorization_code):
        """
        Exchanges the authorization code for an access token

        :param authorization_code: the authorization code, acquired from the user agent after the
                                   user authenticates with starling
        :return: the json response dict
        """
        type_validation([authorization_code],
                        authorization_code_parameter_definition)

        return self.get_oauth_token({
            "code":
            authorization_code,
            "grant_type":
            ACCESS_TOKEN_GRANT_TYPE,
            "client_id":
            self.options["client_id"],
            "client_secret":
            self.options["client_secret"],
            "redirect_uri":
            self.options["redirect_uri"]
        })
Beispiel #16
0
def test_correct_types():
    try:
        type_validation([{"a": 1}, 42, "HAI"], rule_set)
    except TypeValidationError:
        assert False
Beispiel #17
0
def test_optional_args_of_wrong_type():
    try:
        type_validation([1, 3, "sd"], second_rule_set)
        assert False
    except TypeValidationError as e:
        assert len(e.validation_errors) == 1
Beispiel #18
0
def test_accept_less_args_than_defs():
    try:
        type_validation([None, None, None], wonky_rule_set)
        assert False
    except TypeValidationError as e:
        assert len(e.validation_errors) == 2
Beispiel #19
0
def test_undefined_input_required():
    try:
        type_validation([], second_rule_set)
        assert False
    except TypeValidationError:
        assert True
Beispiel #20
0
def test_correct_types_with_excluded_optionals():
    try:
        type_validation([42, "HAI", None], second_rule_set)
    except TypeValidationError:
        assert False
Beispiel #21
0
def test_correct_types_and_additional_types():
    try:
        type_validation([{"a": 1}, 42, "HAI", 1, 2, 3, 4], rule_set)
    except TypeValidationError:
        assert False