diff --git a/mailwizz/client.py b/mailwizz/client.py index 8eff165..462dee9 100644 --- a/mailwizz/client.py +++ b/mailwizz/client.py @@ -46,6 +46,10 @@ class Client(Base): # the DELETE params sent in the request. params_delete = {} + # Whether or not to pass data to requests using json kwarg + # Only applies (currently) to PUT + send_as_json = False + def __init__(self, options): self.__populate(options) @@ -119,3 +123,6 @@ def __populate(self, options): if 'headers' in options: self.headers = options['headers'] + + if 'send_as_json' in options: + self.send_as_json = options['send_as_json'] diff --git a/mailwizz/endpoint/list_subscribers.py b/mailwizz/endpoint/list_subscribers.py index f30e57a..7d35b7b 100644 --- a/mailwizz/endpoint/list_subscribers.py +++ b/mailwizz/endpoint/list_subscribers.py @@ -59,7 +59,7 @@ def create(self, list_uid: str, data: dict): client = Client({ 'method': Client.METHOD_POST, 'url': self.config.get_api_url('lists/{list_uid}/subscribers'.format(list_uid=list_uid)), - 'params_post': data + 'params_post': data, }) return client.request() @@ -81,12 +81,14 @@ def create_bulk(self, list_uid: str, data): return client.request() - def update(self, list_uid: str, subscriber_uid: str, data: dict): + def update(self, list_uid: str, subscriber_uid: str, + data: dict, send_as_json: bool = False): """ Update existing subscriber in given list :param list_uid: :param subscriber_uid: :param data: + :param send_as_json: whether or not to send body as json :return: """ @@ -98,7 +100,8 @@ def update(self, list_uid: str, subscriber_uid: str, data: dict): subscriber_uid=subscriber_uid ) ), - 'params_put': data + 'params_put': data, + 'send_as_json': send_as_json }) return client.request() diff --git a/mailwizz/request.py b/mailwizz/request.py index 8273f41..e301596 100644 --- a/mailwizz/request.py +++ b/mailwizz/request.py @@ -56,38 +56,30 @@ def _make_request(self): """ client = self.client + kwargs = { + 'url': client.url, + 'headers': client.headers, + 'timeout': client.timeout + } if client.is_get_method(): - return requests.get( - url=client.url, - params=client.params_get, - headers=client.headers, - timeout=client.timeout - ) + kwargs['params'] = client.params_get + return requests.get(**kwargs) if client.is_post_method(): - return requests.post( - url=client.url, - data=client.params_post, - headers=client.headers, - timeout=client.timeout - ) + kwargs['data'] = client.params_post + return requests.post(**kwargs) if client.is_put_method(): - return requests.put( - url=client.url, - data=client.params_put, - headers=client.headers, - timeout=client.timeout - ) + if client.send_as_json: + kwargs['json'] = client.params_put + else: + kwargs['data'] = client.params_put + return requests.put(**kwargs) if client.is_delete_method(): - return requests.delete( - url=client.url, - data=client.params_put, - headers=client.headers, - timeout=client.timeout - ) + kwargs['data'] = client.params_put + return requests.delete(**kwargs) def _sign(self, request_url): """