|
| 1 | +import logging |
| 2 | + |
| 3 | +from ..errors import IllegalArgumentError |
| 4 | + |
| 5 | +PATH = '/v2/customers' |
| 6 | +LICENSES_PATH = '/licenses' |
| 7 | + |
| 8 | +log = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class CustomersApiMixin(object): |
| 12 | + def get_customer_by_id(self, customer_id, with_meta=None): |
| 13 | + """ |
| 14 | + Get existing DevMate customer by given id, or error if input params are incorrect, or customer doesn't exist |
| 15 | + :param customer_id: `int` target customer id in DevMate |
| 16 | + :param with_meta: `bool` set True to return object with meta info in tuple |
| 17 | + :return: customer object in `dict`, or `tuple` with customer and meta info |
| 18 | + """ |
| 19 | + log.debug('Get customer by id %s, with meta : %s', customer_id, with_meta) |
| 20 | + |
| 21 | + if customer_id is None or customer_id <= 0: |
| 22 | + raise IllegalArgumentError('Id should not be negative or 0. Given : {}'.format(customer_id)) |
| 23 | + |
| 24 | + return self._dm_get(path='{}/{}'.format(PATH, customer_id), with_meta=with_meta) |
| 25 | + |
| 26 | + def get_customers(self, |
| 27 | + with_email=None, |
| 28 | + with_first_name=None, |
| 29 | + with_last_name=None, |
| 30 | + with_company=None, |
| 31 | + with_phone=None, |
| 32 | + with_address=None, |
| 33 | + with_key=None, |
| 34 | + with_identifier=None, |
| 35 | + with_invoice=None, |
| 36 | + with_order_id=None, |
| 37 | + with_activation_id=None, |
| 38 | + with_limit=None, |
| 39 | + with_offset=None, |
| 40 | + with_licenses=None, |
| 41 | + with_meta=None): |
| 42 | + """ |
| 43 | + Get existing DevMate customers by given filter params, or error if input params are incorrect |
| 44 | + :param with_email: `str` filter customer list by given email (contains) |
| 45 | + :param with_first_name: `str` filter customer list by given first name (contains) |
| 46 | + :param with_last_name: `str` filter customer list by given last name (contains) |
| 47 | + :param with_company: `str` filter customer list by given company name (contains) |
| 48 | + :param with_phone: `str` or `int` filter customer list by given phone number (contains) |
| 49 | + :param with_address: `str` filter customer list by given address (contains) |
| 50 | + :param with_key: `str` or `int` filter customer list by given activation key (equals) |
| 51 | + :param with_identifier: `str` or `int` filter customer list by given identifier, e.g. MAC address (contains) |
| 52 | + :param with_invoice: `str` or `int` filter customer list by given invoice (contains) |
| 53 | + :param with_order_id: `int` filter customer list by given order id (equals) |
| 54 | + :param with_activation_id: `int` filter customer list by given activation id (equals) |
| 55 | + :param with_limit: `int` max count of customers per page |
| 56 | + :param with_offset: `int` offset |
| 57 | + :param with_licenses: `bool` set True to include licenses to customer object |
| 58 | + :param with_meta: `bool` set True to return object with meta info in tuple |
| 59 | + :return: `list` of customer objects in `dict`, or `tuple` with customers and meta info |
| 60 | + """ |
| 61 | + params = { |
| 62 | + 'filter[email]': with_email, |
| 63 | + 'filter[first_name]': with_first_name, |
| 64 | + 'filter[last_name]': with_last_name, |
| 65 | + 'filter[company]': with_company, |
| 66 | + 'filter[phone]': with_phone, |
| 67 | + 'filter[address]': with_address, |
| 68 | + 'filter[key]': with_key, |
| 69 | + 'filter[identifier]': with_identifier, |
| 70 | + 'filter[order_id]': with_order_id, |
| 71 | + 'filter[activation_id]': with_activation_id, |
| 72 | + 'filter[invoice]': with_invoice, |
| 73 | + 'offset': with_offset, |
| 74 | + 'limit': with_limit, |
| 75 | + 'with': 'licenses' if with_licenses else None |
| 76 | + } |
| 77 | + |
| 78 | + log.debug('Get customers with params %s, with meta : %s', params, with_meta) |
| 79 | + |
| 80 | + not_none_params = dict((k, v) for k, v in params.items() if v is not None) |
| 81 | + |
| 82 | + return self._dm_get(path=PATH, params=not_none_params, with_meta=with_meta) |
| 83 | + |
| 84 | + def create_customer(self, customer, with_meta=None): |
| 85 | + """ |
| 86 | + Create new customer in DevMate, or error if input params are incorrect |
| 87 | + :param customer: customer data in `dict`, 'email' field should be set in `dict` |
| 88 | + :param with_meta: `bool` set True to return object with meta info in tuple |
| 89 | + :return: created customer object in `dict`, or `tuple` with customer and meta info |
| 90 | + """ |
| 91 | + log.debug('Create customer with details %s, with meta : %s', customer, with_meta) |
| 92 | + |
| 93 | + if 'email' not in customer or customer['email'] is None: |
| 94 | + raise IllegalArgumentError('"email" field should be set for the customer') |
| 95 | + |
| 96 | + return self._dm_post(path=PATH, json={'data': customer}, with_meta=with_meta) |
| 97 | + |
| 98 | + def update_customer(self, customer, with_meta=None): |
| 99 | + """ |
| 100 | + Update existing customer in DevMate, or error if input params are incorrect, or customer doesn't exist |
| 101 | + :param customer: updated customer data in `dict`, 'id' field should be set in `dict` |
| 102 | + :param with_meta: `bool` set True to return object with meta info in tuple |
| 103 | + :return: created customer object in `dict`, or `tuple` with customer and meta info |
| 104 | + """ |
| 105 | + log.debug('Update customer with details %s, with meta : %s', customer, with_meta) |
| 106 | + |
| 107 | + if 'id' not in customer or customer['id'] is None: |
| 108 | + raise IllegalArgumentError('Current customer "id" field should be set for the customer') |
| 109 | + |
| 110 | + return self._dm_put(path='{}/{}'.format(PATH, customer['id']), json={'data': customer}, with_meta=with_meta) |
| 111 | + |
| 112 | + def create_license_for_customer(self, customer_id, _license, with_meta=None): |
| 113 | + """ |
| 114 | + Create new license for existing customer in DevMate by license type id, |
| 115 | + or error if input params are incorrect, or customer doesn't exist |
| 116 | + :param customer_id: `int` id of customer in DevMate |
| 117 | + :param _license: license object in `dict`, 'license_type_id' field should be set in `dict` |
| 118 | + :param with_meta: `bool` set True to return object with meta info in tuple |
| 119 | + :return: created license object in `dict`, or `tuple` with license and meta info |
| 120 | + """ |
| 121 | + log.debug('Create license for customer %s with details %s, with meta : %s', customer_id, _license, with_meta) |
| 122 | + |
| 123 | + if customer_id is None or customer_id <= 0: |
| 124 | + raise IllegalArgumentError('Id should not be negative or 0. Given : {}'.format(customer_id)) |
| 125 | + |
| 126 | + if 'license_type_id' not in _license or _license['license_type_id'] is None: |
| 127 | + raise IllegalArgumentError('Current customer "license_type_id" field should be set for the customer') |
| 128 | + |
| 129 | + return self._dm_post( |
| 130 | + path='{}/{}{}'.format(PATH, customer_id, LICENSES_PATH), |
| 131 | + json={'data': _license}, |
| 132 | + with_meta=with_meta |
| 133 | + ) |
0 commit comments