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() diff --git a/pysendpulse/pysendpulse.py b/pysendpulse/pysendpulse.py index 2d38d86..b58e3be 100644 --- a/pysendpulse/pysendpulse.py +++ b/pysendpulse/pysendpulse.py @@ -41,16 +41,18 @@ 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 @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") @@ -60,6 +62,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 +72,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 +105,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)