|
| 1 | +from bunq.sdk.model.generated.endpoint import DeviceServer |
| 2 | +from bunq.sdk.model.generated.endpoint import BunqResponseInt |
| 3 | +from bunq.sdk import client |
| 4 | +from bunq.sdk.json import converter |
| 5 | +from bunq.sdk.exception import BunqException |
| 6 | + |
| 7 | + |
| 8 | +class DeviceServerInternal(DeviceServer): |
| 9 | + _ERROR_API_CONTEXT_IS_NULL = 'ApiContext should not be None,' \ |
| 10 | + ' use the generated class instead.' |
| 11 | + |
| 12 | + @classmethod |
| 13 | + def create(cls, description, secret, permitted_ips=None, |
| 14 | + custom_headers=None, api_context=None): |
| 15 | + """ |
| 16 | + Create a new DeviceServer providing the installation token in the header |
| 17 | + and signing the request with the private part of the key you used to |
| 18 | + create the installation. The API Key that you are using will be bound to |
| 19 | + the IP address of the DeviceServer which you have |
| 20 | + created.<br/><br/>Using a Wildcard API Key gives you the freedom to make |
| 21 | + API calls even if the IP address has changed after the POST |
| 22 | + device-server.<br/><br/>Find out more at this link <a |
| 23 | + href="https://bunq.com/en/apikey-dynamic-ip" |
| 24 | + target="_blank">https://bunq.com/en/apikey-dynamic-ip</a>. |
| 25 | +
|
| 26 | + :param description: The description of the DeviceServer. This is only |
| 27 | + for your own reference when reading the DeviceServer again. |
| 28 | + :type description: str |
| 29 | + :param secret: The API key. You can request an API key in the bunq app. |
| 30 | + :type secret: str |
| 31 | + :param permitted_ips: An array of IPs (v4 or v6) this DeviceServer will |
| 32 | + be able to do calls from. These will be linked to the API key. |
| 33 | + :type permitted_ips: list[str] |
| 34 | + :type custom_headers: dict[str, str]|None |
| 35 | + :type api_context: context.ApiContext |
| 36 | +
|
| 37 | + :rtype: BunqResponseInt |
| 38 | + """ |
| 39 | + |
| 40 | + if api_context is None: |
| 41 | + raise BunqException(cls._ERROR_API_CONTEXT_IS_NULL) |
| 42 | + |
| 43 | + if custom_headers is None: |
| 44 | + custom_headers = {} |
| 45 | + |
| 46 | + request_map = { |
| 47 | + cls.FIELD_DESCRIPTION: description, |
| 48 | + cls.FIELD_SECRET: secret, |
| 49 | + cls.FIELD_PERMITTED_IPS: permitted_ips |
| 50 | + } |
| 51 | + |
| 52 | + api_client = client.ApiClient(api_context) |
| 53 | + request_bytes = converter.class_to_json(request_map).encode() |
| 54 | + endpoint_url = cls._ENDPOINT_URL_CREATE |
| 55 | + response_raw = api_client.post(endpoint_url, request_bytes, |
| 56 | + custom_headers) |
| 57 | + |
| 58 | + return BunqResponseInt.cast_from_bunq_response( |
| 59 | + cls._process_for_id(response_raw) |
| 60 | + ) |
0 commit comments