def create_customer_account(self, customerId, account_type, nickname, rewards, balance, account_number=None): header = {"Content-Type": "application/json"} payload = {"key": self.key} if not Account.typeIsValid(account_type): raise ValueError( "Account type is '{}', but it must be one of: {}".format( account_type, str(Account.TYPES))) body = { "type": account_type, "nickname": nickname, "rewards": rewards, "balance": balance } if account_number is not None: if not Account.accountNumberIsValid(account_number): raise ValueError( "Account number '{}' must be 16 digits".format( account_number)) body["account_number"] = account_number r = requests.post(urlConstants.CUSTOMERS_ID_URL % customerId, headers=header, params=payload, data=json.dumps(body)) data = r.json() if data.get("code") != 201: raise NessieApiError(r.text) return Account.fromJson(data.get("objectCreated"))
def create_customer(self, first_name: str, last_name: str, address): if first_name is None or last_name is None: raise CustomerValidationError(utils.constants.createCustomerMissingFields) val_address = validate_address(address) if val_address != utils.constants.success: raise AddressValidationError(val_address) header = {"Content-Type": "application/json"} payload = {"key": self.key} body = { "first_name": first_name, "last_name": last_name, "address": address.to_dict() } r = requests.post(utils.constants.customersUrl, headers=header, params=payload, data=json.dumps(body)) if r.status_code != 201: raise NessieApiError(r) data = r.json() created_customer = Customer() created_customer.first_name = first_name created_customer.last_name = last_name created_customer.address = address created_customer.customer_id = data.get("objectCreated").get("_id") return created_customer
def get_account(self, accountId): header = {"Content-Type": "application/json"} payload = {"key": self.key} r = requests.get(urlConstants.ACCOUNTS_ID_URL % accountId, headers=header, params=payload) if r.status_code != 200: raise NessieApiError(r) return Account.fromJson(r.json())
def delete_account(self, accountId): header = {"Content-Type": "application/json"} payload = {"key": self.key} r = requests.delete(urlConstants.ACCOUNTS_ID_URL % accountId, headers=header, params=payload) data = r.json() if data.get("code") != 202: raise NessieApiError(r.text)
def get_customer_by_id(self, customer_id): if customer_id is None: raise CustomerValidationError(utils.constants.customerIdMissingField) header = {"Content-Type": "application/json"} payload = {"key": self.key} url = utils.constants.customersIdUrl % customer_id r = requests.get(url, headers=header, params=payload) if r.status_code != 200: raise NessieApiError(r) return Customer(r.json())
def getAtmById(self, idCode): reqUrl = "%s/atms/%s" % (self.baseUrl, idCode) par = {'key': self.key} r = requests.get(reqUrl, params=par) if r.status_code != 200: raise NessieApiError(r) return ATM(r.json())
def get_all_customers(self): header = {"Content-Type": "application/json"} payload = {"key": self.key} r = requests.get(utils.constants.customersUrl, headers=header, params=payload) if r.status_code != 200: raise NessieApiError(r) data = r.json() customer_list = [] for c in data: customer_list.append(Customer(c)) return customer_list
def getAtms(self, lat=None, lng=None, rad=None): reqUrl = "%s/atms" % self.baseUrl par = self.__buildParams(lat, lng, rad) self.__validateParams(par) r = requests.get(reqUrl, params=par) if r.status_code != 200: raise NessieApiError(r) jsonAtms = r.json() while ('next' in r.json()['paging']): reqUrl = "%s%s" % (self.baseUrl, r.json()['paging']['next']) r = requests.get(reqUrl) if r.status_code != 200: raise NessieApiError(r) jsonAtms['data'] = jsonAtms['data'] + r.json()['data'] return self.__formatResponse(jsonAtms)
def get_branch_by_id(self, id_string): self.__validate_path(id_string) req_url = '%s/branches/%s' % (self.base_url, id_string) par = self.__build_params() r = requests.get(req_url, params=par) if r.status_code != 200: raise NessieApiError(r) return Branch(r.json())
def get_branches(self): req_url = '%s/branches' % self.base_url par = self.__build_params() r = requests.get(req_url, params=par) if r.status_code != 200: raise NessieApiError(r) branch_list = r.json() return self.__format_response(branch_list)
def get_customer_accounts(self, customerId): header = {"Content-Type": "application/json"} payload = {"key": self.key} r = requests.get(urlConstants.CUSTOMERS_ID_URL % customerId, headers=header, params=payload) data = r.json() if r.status_code != 200: raise NessieApiError(r) accounts = [] for account in data: accounts.append(Account.fromJson(account)) return accounts
def update_customer(self, customer_id, new_address): if customer_id is None: raise CustomerValidationError(utils.constants.customerIdMissingField) val_address = validate_address(new_address) if val_address != utils.constants.success: raise AddressValidationError(val_address) header = {"Content-Type": "application/json"} payload = {"key": self.key} body = {"address": new_address.to_dict()} url = utils.constants.customersIdUrl % customer_id r = requests.put(url, headers=header, params=payload, data=json.dumps(body)) if r.status_code != 202: raise NessieApiError(r) else: return True
def get_all_accounts(self, accountType=None): header = {"Content-Type": "application/json"} payload = {"key": self.key} if accountType is not None: if not Account.typeIsValid(accountType): raise ValueError("accountType must be one of: {}".format( str(Account.TYPES))) payload["type"] = accountType r = requests.get(urlConstants.ACCOUNTS_URL, headers=header, params=payload) data = r.json() accounts = [] for account in data: accounts.append(Account.fromJson(account)) if r.status_code != 200: raise NessieApiError(r) return accounts
def update_account(self, accountId, nickname, account_number=None): header = {"Content-Type": "application/json"} payload = {"key": self.key} body = {"nickname": nickname} if account_number is not None: if not Account.accountNumberIsValid(account_number): raise ValueError( "Account number '{}' must be 16 digits".format( account_number)) body["account_number"] = account_number r = requests.put(urlConstants.ACCOUNTS_ID_URL % accountId, headers=header, params=payload, data=json.dumps(body)) data = r.json() # TODA: probably need to change nessier api error to # NessierApiError(r) if data.get("code") != 202: raise NessieApiError(r.text)