Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pysendpulse/examples/sendpulse-rest-api-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions pysendpulse/pysendpulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down