def user_context(cls): """ :rtype: UserContext """ if cls._user_context is not None: return cls._user_context raise BunqException(cls._ERROR_USER_CONTEXT_HAS_NOT_BEEN_LOADED)
def api_context(cls): """ :rtype: ApiContext """ if cls._api_context is not None: return cls._api_context raise BunqException(cls._ERROR_API_CONTEXT_HAS_NOT_BEEN_LOADED)
def init_main_monetary_account(self): all_monetary_account = endpoint.MonetaryAccountBank.list().value for account in all_monetary_account: if account.status == self._STATUS_ACTIVE: self._primary_monetary_account = account return raise BunqException(self._ERROR_NO_ACTIVE_MONETARY_ACCOUNT_FOUND)
def get_first_pointer_iban(cls, monetary_account_bank): """ :rtype: object_.Pointer """ for alias in monetary_account_bank.alias: if alias.type_ == cls._POINTER_TYPE_IBAN: return alias raise BunqException(cls._ERROR_COULD_NOT_FIND_IBAN_POINTER)
def main(): parser = argparse.ArgumentParser() parser.add_argument(OPTION_API_KEY) all_option = parser.parse_args() if all_option.api_key is None: raise BunqException(ERROR_OPTION_MISSING_API_KEY) api_context = ApiContext(ApiEnvironmentType.PRODUCTION, all_option.api_key, socket.gethostname()) api_context.save(DEFAULT_BUNQ_CONFIGURATION_FILE_NAME_PRODUCTION)
def _set_user(self, user): if isinstance(user, endpoint.UserPerson): self._user_person = user elif isinstance(user, endpoint.UserCompany): self._user_company = user elif isinstance(user, endpoint.UserApiKey): self._user_api_key = user else: raise BunqException( self._ERROR_UNEXPECTED_USER_INSTANCE.format(user.__class__))
def create(cls, description, secret, permitted_ips=None, custom_headers=None, api_context=None): """ Create a new DeviceServer providing the installation token in the header and signing the request with the private part of the key you used to create the installation. The API Key that you are using will be bound to the IP address of the DeviceServer which you have created.<br/><br/>Using a Wildcard API Key gives you the freedom to make API calls even if the IP address has changed after the POST device-server.<br/><br/>Find out more at this link <a href="https://bunq.com/en/apikey-dynamic-ip" target="_blank">https://bunq.com/en/apikey-dynamic-ip</a>. :param description: The description of the DeviceServer. This is only for your own reference when reading the DeviceServer again. :type description: str :param secret: The API key. You can request an API key in the bunq app. :type secret: str :param permitted_ips: An array of IPs (v4 or v6) this DeviceServer will be able to do calls from. These will be linked to the API key. :type permitted_ips: list[str] :type custom_headers: dict[str, str]|None :type api_context: context.ApiContext :rtype: BunqResponseInt """ if api_context is None: raise BunqException(cls._ERROR_API_CONTEXT_IS_NULL) if custom_headers is None: custom_headers = {} request_map = { cls.FIELD_DESCRIPTION: description, cls.FIELD_SECRET: secret, cls.FIELD_PERMITTED_IPS: permitted_ips } api_client = client.ApiClient(api_context) request_bytes = converter.class_to_json(request_map).encode() endpoint_url = cls._ENDPOINT_URL_CREATE response_raw = api_client.post(endpoint_url, request_bytes, custom_headers) return BunqResponseInt.cast_from_bunq_response( cls._process_for_id(response_raw))
def alias_first(self): """ :rtype: Pointer """ if context.BunqContext.user_context().is_only_user_company_set(): return context.BunqContext.user_context().user_company.alias[ self._FIRST_INDEX] if context.BunqContext.user_context().is_only_user_person_set(): return context.BunqContext.user_context().user_person.alias[ self._FIRST_INDEX] raise BunqException(self.__ERROR_COULD_NOT_DETERMINE_USER)
def get_referenced_user(self): """ :rtype: BunqModel """ if self._user_person is not None: return self._user_person if self._user_company is not None: return self._user_company if self._user_api_key is not None: return self._user_api_key raise BunqException(self._ERROR_ALL_FIELD_IS_NULL)
def setup_context(self): if isfile(self.determine_bunq_conf_filename()): pass # Config is already present elif self.env == ApiEnvironmentType.SANDBOX: sandbox_user = self.generate_new_sandbox_user() ApiContext(ApiEnvironmentType.SANDBOX, sandbox_user.api_key, socket.gethostname()).save( self.determine_bunq_conf_filename()) else: raise BunqException(self._ERROR_COULD_NOT_DETIRMINE_CONF) api_context = ApiContext.restore(self.determine_bunq_conf_filename()) api_context.ensure_session_active() api_context.save(self.determine_bunq_conf_filename()) BunqContext.load_api_context(api_context)
def _get_object_class(cls, class_name): """ :type class_name: str :rtype: core.BunqModel """ try: return getattr(endpoint, class_name) except AttributeError: pass try: return getattr(object_, class_name) except AttributeError: pass raise BunqException(cls._ERROR_MODEL_NOT_FOUND.format(class_name))
def main(): parser = argparse.ArgumentParser() parser.add_argument(OPTION_API_KEY) all_option = parser.parse_args() if all_option.api_key is None: raise BunqException(ERROR_OPTION_MISSING_API_KEY) api_context = ApiContext(ApiEnvironmentType.PRODUCTION, all_option.api_key, '*') BunqContext.load_api_context(api_context) end = 50 for i in range(0, end): endpoint.Payment.create(amount=Amount('0.01', 'EUR'), counterparty_alias=Pointer('IBAN', '', ''), description=str(round(i / end * 100, 2)) + " Prozent von deinem Geld", monetary_account_id=) time.sleep(0.33333)
def _get_session_timeout_seconds(cls, session_server): """ :type session_server: core.SessionServer :rtype: int """ if session_server.user_company is not None: return session_server.user_company.session_timeout elif session_server.user_person is not None: return session_server.user_person.session_timeout elif session_server.user_api_key is not None: return session_server \ .user_api_key \ .requested_by_user \ .get_referenced_object() \ .session_timeout else: raise BunqException()
def generate_new_sandbox_user(self): url = "https://sandbox.bunq.com/v1/sandbox-user" headers = { "x-bunq-client-request-id": "uniqueness-is-required", "cache-control": "no-cache", "x-bunq-geolocation": "0 0 0 0 NL", "x-bunq-language": "en_US", "x-bunq-region": "en_US", } response = requests.request("POST", url, headers=headers) if response.status_code is 200: response_json = json.loads(response.text) return endpoint.SandboxUser.from_json( json.dumps(response_json["Response"][0]["ApiKey"]) ) raise BunqException("Could not create new sandbox user.")
def setup_context(self, reset_config_if_needed=True): if isfile(self.determine_bunq_conf_filename()): pass # Config is already present elif self.env == ApiEnvironmentType.SANDBOX: sandbox_user = self.generate_new_sandbox_user() ApiContext(ApiEnvironmentType.SANDBOX, sandbox_user.api_key, socket.gethostname()).save( self.determine_bunq_conf_filename()) else: raise BunqException(self._ERROR_COULD_NOT_DETERMINE_CONF) try: api_context = ApiContext.restore( self.determine_bunq_conf_filename()) api_context.ensure_session_active() api_context.save(self.determine_bunq_conf_filename()) BunqContext.load_api_context(api_context) except ForbiddenException as forbidden_exception: if reset_config_if_needed: self.__handle_forbidden_exception(forbidden_exception) else: raise forbidden_exception
def generate_new_sandbox_user(self): """ :rtype: SandboxUser """ url = "https://sandbox.public.api.bunq.com/v1/sandbox-user" headers = { 'x-bunq-client-request-id': "uniqueness-is-required", 'cache-control': "no-cache", 'x-bunq-geolocation': "0 0 0 0 NL", 'x-bunq-language': "en_US", 'x-bunq-region': "en_US", } response = requests.request("POST", url, headers=headers) if response.status_code is 200: response_json = json.loads(response.text) return endpoint.SandboxUser.from_json( json.dumps(response_json["Response"][0]["ApiKey"])) raise BunqException(self._ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER)
def __generate_new_sandbox_user(): """ :rtype: endpoint.SandboxUser """ url = ApiEnvironmentType.SANDBOX.uri_base + __ENDPOINT_SANDBOX_USER headers = { ApiClient.HEADER_REQUEST_ID: __UNIQUE_REQUEST_ID, ApiClient.HEADER_CACHE_CONTROL: ApiClient._CACHE_CONTROL_NONE, ApiClient.HEADER_GEOLOCATION: ApiClient._GEOLOCATION_ZERO, ApiClient.HEADER_LANGUAGE: ApiClient._LANGUAGE_EN_US, ApiClient.HEADER_REGION: ApiClient._REGION_NL_NL, } response = requests.request(ApiClient._METHOD_POST, url, headers=headers) if response.status_code is ApiClient._STATUS_CODE_OK: response_json = json.loads(response.text) return endpoint.SandboxUser.from_json( json.dumps(response_json[__FIELD_RESPONSE][__INDEX_FIRST] [__FIELD_API_KEY])) raise BunqException(_ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER)