def __init__(self, rc=None, use_in_memory_on_failure=True): # format: {user: [<type project>]} try: from redis_pubsub_dict import RedisDict import redis_pubsub_dict import functools import json redis_pubsub_dict.dumps = lambda x: json.dumps(x) redis_pubsub_dict.loads = lambda x: Util.try_function_on_dict( [Project.fromJSON])(x) redis_pubsub_dict.RedisDict.to_json = lambda x: dict(x.items()) redis_pubsub_dict.RedisDict.__eq__ = ( lambda x, other: dict(x.items()) == other) redis_pubsub_dict.RedisDict.keys = keys # runs in RDS ecosystem if rc is None: logger.debug("No redis client was given. Create one.") startup_nodes = [{ "host": os.getenv("REDIS_HOST", "localhost"), "port": os.getenv("REDIS_PORT", "6379"), }] try: logger.debug("first try cluster") from rediscluster import RedisCluster rc = RedisCluster( startup_nodes=startup_nodes, decode_responses=True, ) except Exception as e: logger.error(e) logger.debug( "Cluster has an error, try standardalone redis") from redis import Redis rc = Redis( **(startup_nodes[0]), db=0, decode_responses=True, ) rc.info() # provoke an error message logger.debug("set redis backed dict") self.projects = RedisDict(rc, "researchmanager_projects") except Exception as e: logger.error(e) logger.info("no redis found.") if not use_in_memory_on_failure: logger.info("exit...") import sys sys.exit() logger.info("use in-memory") self.projects = {}
def load_service_with_tokens(jsonStr): d = json.loads(jsonStr) user = User.from_json(json.dumps(d["data"])) tokens = [] for t in d["tokens"]: tokens.append( Util.try_function_on_dict([OAuth2Token.from_json, Token.from_json])(json.dumps(t))) return {"data": user, "tokens": tokens}
from flask import jsonify, request, abort import logging from RDS import Util, User, Token, OAuth2Token import utility import json from lib.Exceptions.StorageException import UserHasTokenAlreadyError, UserNotExistsError init_object = Util.try_function_on_dict( [OAuth2Token.from_dict, Token.from_dict, Util.initialize_object_from_json]) logger = logging.getLogger() def index(user_id): try: tokens = utility.storage.getTokens(user_id) except UserNotExistsError as e: abort(404, description=str(e)) data = {"length": len(tokens), "list": tokens} logger.debug(f"Found tokens: {json.dumps(data)}") return jsonify(data) def post(user_id): logger.debug(f"get token string: {request.json}.") from RDS import Token token = Util.getTokenObject(request.json)
def __init__(self, rc=None, use_in_memory_on_failure=True): logger.info("try to use redis as backend.") try: import redis_pubsub_dict import functools redis_pubsub_dict.dumps = lambda x: json.dumps(x) redis_pubsub_dict.loads = lambda x: Util.try_function_on_dict([ User.from_json, OAuth2Service.from_json, LoginService.from_json, OAuth2Token.from_json, Token.from_json, load_service_with_tokens, ])(x) redis_pubsub_dict.RedisDict.to_json = lambda x: dict(x.items()) redis_pubsub_dict.RedisDict.__eq__ = ( lambda x, other: dict(x.items()) == other) redis_pubsub_dict.RedisDict.keys = keys redis_pubsub_dict.RedisDict.__len__ = lambda self: self.size() # runs in RDS ecosystem, use redis as backend if rc is None: logger.debug("No redis client was given. Create one.") startup_nodes = [{ "host": os.getenv("REDIS_HOST", "localhost"), "port": os.getenv("REDIS_PORT", "6379"), }] try: logger.debug("first try cluster") from rediscluster import RedisCluster rc = RedisCluster( startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=True, cluster_down_retry_attempts=1, ) rc.cluster_info() # provoke an error message except Exception as e: logger.error(e) logger.debug("Cluster has an error, try standalone redis") from redis import Redis rc = Redis( **(startup_nodes[0]), db=0, decode_responses=True, ) rc.info() # provoke an error message logger.debug("set redis backed dict") self._storage = redis_pubsub_dict.RedisDict( rc, "tokenstorage_storage") self._services = redis_pubsub_dict.RedisDict( rc, "tokenstorage_services") self.__rc = rc self.__rc_helper = None try: from redis import Redis logger.debug("try to initialize the helper redis conn.") rc_helper = Redis( host="{}-master".format( os.getenv("REDIS_HELPER_HOST", "localhost")), port=os.getenv("REDIS_HELPER_PORT", "6379"), db=0, decode_responses=True, ) rc_helper.info() # provoke an error message self.__rc_helper = rc_helper self.__redis_helper_channel = os.getenv( "REDIS_CHANNEL", "TokenStorage_Refresh_Token") logger.debug("initialized helper redis conn.") except Exception as e: logger.error("cannot initialize helper redis conn.") logger.error(e, exc_info=True) logger.debug("set methods to redis backed dict to use it as list") self._services.append = append.__get__(self._services) except Exception as e: logger.error(e) logger.info("no redis found.") if not use_in_memory_on_failure: logger.info("exit...") import sys sys.exit() logger.info("use in-memory") self._storage = {} self._services = []
from flask import jsonify, request, Response import json from RDS import BaseService, LoginService, OAuth2Service, Util from RDS.ServiceException import ( ServiceExistsAlreadyError, ServiceNotExistsError, ) from werkzeug.exceptions import abort import logging import utility logger = logging.getLogger(__name__) init_object = Util.try_function_on_dict( [OAuth2Service.from_dict, LoginService.from_dict, Util.initialize_object_from_json] ) def index(): services = utility.storage.getServices() data = {"length": len(services), "list": services} return jsonify(data) def get(servicename: str): servicename = servicename.lower() svc = utility.storage.getService(servicename) if svc is not None: return jsonify(svc)