From 75b632c2099ec7011c1a9228a89dd6bfa45a6701 Mon Sep 17 00:00:00 2001 From: Forkhammer Date: Thu, 26 Nov 2020 16:12:52 +0500 Subject: [PATCH 1/3] Config for memcached host --- pysendpulse/pysendpulse.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pysendpulse/pysendpulse.py b/pysendpulse/pysendpulse.py index 2d38d86..e69c3cb 100644 --- a/pysendpulse/pysendpulse.py +++ b/pysendpulse/pysendpulse.py @@ -41,11 +41,12 @@ class PySendPulse: __token_hash_name = None __storage_type = "FILE" __refresh_token = 0 + __memcached_host = "127.0.0.1:11211" MEMCACHED_VALUE_TIMEOUT = 3600 ALLOWED_STORAGE_TYPES = ['FILE', 'MEMCACHED'] - def __init__(self, user_id, secret, storage_type="FILE"): + def __init__(self, user_id, secret, storage_type="FILE", memcached_host="127.0.0.1:11211"): """ SendPulse API constructor @param user_id: string REST API ID from SendPulse settings @@ -60,6 +61,7 @@ def __init__(self, user_id, secret, storage_type="FILE"): self.__user_id = user_id self.__secret = secret self.__storage_type = storage_type.upper() + self.__memcached_host = memcached_host m = md5() m.update("{}::{}".format(user_id, secret).encode('utf-8')) self.__token_hash_name = m.hexdigest() @@ -69,7 +71,7 @@ def __init__(self, user_id, secret, storage_type="FILE"): self.__storage_type = 'FILE' logger.debug("Try to get security token from '{}'".format(self.__storage_type, )) if self.__storage_type == "MEMCACHED": - mc = memcache.Client(['127.0.0.1:11211']) + mc = memcache.Client([self.__memcached_host]) self.__token = mc.get(self.__token_hash_name) else: # file filepath = "{}{}".format(self.__token_file_path, self.__token_hash_name) @@ -102,7 +104,7 @@ def __get_token(self): logger.debug("Got: '{}'".format(self.__token, )) if self.__storage_type == "MEMCACHED": logger.debug("Try to set token '{}' into 'MEMCACHED'".format(self.__token, )) - mc = memcache.Client(['127.0.0.1:11211']) + mc = memcache.Client([self.__memcached_host]) mc.set(self.__token_hash_name, self.__token, self.MEMCACHED_VALUE_TIMEOUT) else: filepath = "{}{}".format(self.__token_file_path, self.__token_hash_name) From d675244351f8b5498dbb906de16bb14d210b7456 Mon Sep 17 00:00:00 2001 From: Forkhammer Date: Thu, 26 Nov 2020 16:17:47 +0500 Subject: [PATCH 2/3] Add example for memcached host --- pysendpulse/examples/sendpulse-rest-api-example.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pysendpulse/examples/sendpulse-rest-api-example.py b/pysendpulse/examples/sendpulse-rest-api-example.py index 83ac27c..83d91a3 100644 --- a/pysendpulse/examples/sendpulse-rest-api-example.py +++ b/pysendpulse/examples/sendpulse-rest-api-example.py @@ -13,7 +13,8 @@ REST_API_ID = '' REST_API_SECRET = '' TOKEN_STORAGE = 'memcached' - SPApiProxy = PySendPulse(REST_API_ID, REST_API_SECRET, TOKEN_STORAGE) + MEMCACHED_HOST = '127.0.0.1:11211' + SPApiProxy = PySendPulse(REST_API_ID, REST_API_SECRET, TOKEN_STORAGE, memcached_host=MEMCACHED_HOST) # Get list of tasks SPApiProxy.push_get_tasks() From 002e2355a8b0ff4455b1a62530a5f96169400154 Mon Sep 17 00:00:00 2001 From: Forkhammer Date: Thu, 26 Nov 2020 16:21:56 +0500 Subject: [PATCH 3/3] Fix docstring of PySendPulse constructor for memcached host --- pysendpulse/pysendpulse.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pysendpulse/pysendpulse.py b/pysendpulse/pysendpulse.py index e69c3cb..b58e3be 100644 --- a/pysendpulse/pysendpulse.py +++ b/pysendpulse/pysendpulse.py @@ -52,6 +52,7 @@ def __init__(self, user_id, secret, storage_type="FILE", memcached_host="127.0.0 @param user_id: string REST API ID from SendPulse settings @param secret: string REST API Secret from SendPulse settings @param storage_type: string FILE|MEMCACHED + @param memcached_host: string Host for Memcached server, default is 127.0.0.1:11211 @raise: Exception empty credentials or get token failed """ logger.info("Initialization SendPulse REST API Class")