From 5a884146569e7219a79fb83ea76941355de7e19e Mon Sep 17 00:00:00 2001 From: Wayne Smith Date: Wed, 16 Dec 2020 15:52:09 -0600 Subject: [PATCH 1/4] Add flag to send request body as json --- mailwizz/client.py | 7 ++++ mailwizz/endpoint/list_subscribers.py | 7 ++-- mailwizz/request.py | 46 +++++++++++++-------------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/mailwizz/client.py b/mailwizz/client.py index 8eff165..a5804a4 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 + # Does not apply to GET + 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..d7cf5a2 100644 --- a/mailwizz/endpoint/list_subscribers.py +++ b/mailwizz/endpoint/list_subscribers.py @@ -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..0f537dd 100644 --- a/mailwizz/request.py +++ b/mailwizz/request.py @@ -56,38 +56,36 @@ 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 - ) + if client.send_as_json: + kwargs['json'] = client.params_post + else: + 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 - ) + if client.send_as_json: + kwargs['json'] = client.params_put + else: + kwargs['data'] = client.params_put + return requests.delete(**kwargs) def _sign(self, request_url): """ From 48ad13e321199b48113a6a7ceda6dfba4c40c426 Mon Sep 17 00:00:00 2001 From: Wayne Smith Date: Thu, 17 Dec 2020 14:46:20 -0600 Subject: [PATCH 2/4] Add flag to create call --- mailwizz/endpoint/list_subscribers.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mailwizz/endpoint/list_subscribers.py b/mailwizz/endpoint/list_subscribers.py index d7cf5a2..cbe47c5 100644 --- a/mailwizz/endpoint/list_subscribers.py +++ b/mailwizz/endpoint/list_subscribers.py @@ -48,18 +48,21 @@ def get_subscriber(self, list_uid: str, subscriber_uid: str): return client.request() - def create(self, list_uid: str, data: dict): + def create(self, list_uid: str, data: dict, + send_as_json: bool = False): """ Create a new subscriber in the given list :param list_uid: :param data: + :param send_as_json: whether or not to send body as json :return: """ 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, + 'send_as_json': True }) return client.request() From a3cc5d4901fe0a92cb3fe5954bb24502e3ec8381 Mon Sep 17 00:00:00 2001 From: Wayne Smith Date: Thu, 17 Dec 2020 15:16:19 -0600 Subject: [PATCH 3/4] debugging bugfix --- mailwizz/endpoint/list_subscribers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mailwizz/endpoint/list_subscribers.py b/mailwizz/endpoint/list_subscribers.py index cbe47c5..51081aa 100644 --- a/mailwizz/endpoint/list_subscribers.py +++ b/mailwizz/endpoint/list_subscribers.py @@ -62,7 +62,7 @@ def create(self, list_uid: str, data: dict, 'method': Client.METHOD_POST, 'url': self.config.get_api_url('lists/{list_uid}/subscribers'.format(list_uid=list_uid)), 'params_post': data, - 'send_as_json': True + 'send_as_json': send_as_json }) return client.request() From 366c091319f3c26fcd1dd9b47ee41a8053ae7d91 Mon Sep 17 00:00:00 2001 From: Wayne Smith Date: Thu, 17 Dec 2020 15:19:32 -0600 Subject: [PATCH 4/4] only apply flag to PUT --- mailwizz/client.py | 2 +- mailwizz/endpoint/list_subscribers.py | 5 +---- mailwizz/request.py | 10 ++-------- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/mailwizz/client.py b/mailwizz/client.py index a5804a4..462dee9 100644 --- a/mailwizz/client.py +++ b/mailwizz/client.py @@ -47,7 +47,7 @@ class Client(Base): params_delete = {} # Whether or not to pass data to requests using json kwarg - # Does not apply to GET + # Only applies (currently) to PUT send_as_json = False def __init__(self, options): diff --git a/mailwizz/endpoint/list_subscribers.py b/mailwizz/endpoint/list_subscribers.py index 51081aa..7d35b7b 100644 --- a/mailwizz/endpoint/list_subscribers.py +++ b/mailwizz/endpoint/list_subscribers.py @@ -48,13 +48,11 @@ def get_subscriber(self, list_uid: str, subscriber_uid: str): return client.request() - def create(self, list_uid: str, data: dict, - send_as_json: bool = False): + def create(self, list_uid: str, data: dict): """ Create a new subscriber in the given list :param list_uid: :param data: - :param send_as_json: whether or not to send body as json :return: """ @@ -62,7 +60,6 @@ def create(self, list_uid: str, data: dict, 'method': Client.METHOD_POST, 'url': self.config.get_api_url('lists/{list_uid}/subscribers'.format(list_uid=list_uid)), 'params_post': data, - 'send_as_json': send_as_json }) return client.request() diff --git a/mailwizz/request.py b/mailwizz/request.py index 0f537dd..e301596 100644 --- a/mailwizz/request.py +++ b/mailwizz/request.py @@ -67,10 +67,7 @@ def _make_request(self): return requests.get(**kwargs) if client.is_post_method(): - if client.send_as_json: - kwargs['json'] = client.params_post - else: - kwargs['data'] = client.params_post + kwargs['data'] = client.params_post return requests.post(**kwargs) if client.is_put_method(): @@ -81,10 +78,7 @@ def _make_request(self): return requests.put(**kwargs) if client.is_delete_method(): - if client.send_as_json: - kwargs['json'] = client.params_put - else: - kwargs['data'] = client.params_put + kwargs['data'] = client.params_put return requests.delete(**kwargs) def _sign(self, request_url):