Ejemplo n.º 1
0
    def _init_session(self, config):
        if config['WECHAT_SESSION_TYPE'] == 'redis':
            from wechatpy.session.redisstorage import RedisStorage
            from redis import Redis

            if config.get('WECHAT_SESSION_REDIS_URL'):
                redis = Redis.from_url(config['WECHAT_SESSION_REDIS_URL'])
            else:
                redis = Redis(host=config.get('WECHAT_SESSION_REDIS_HOST',
                                              'localhost'),
                              port=config.get('WECHAT_SESSION_REDIS_PORT',
                                              6379),
                              db=config.get('WECHAT_SESSION_REDIS_DB', 0),
                              password=config.get('WECHAT_SESSION_REDIS_PASS',
                                                  None))
            prefix = config['WECHAT_SESSION_PREFIX']
            self._redis_prefix = prefix
            self._redis = redis
            session_interface = RedisStorage(redis, prefix=prefix)

        elif config['WECHAT_SESSION_TYPE'] == 'memcached':
            from wechatpy.session.memcachedstorage import MemcachedStorage
            mc = self._get_mc_client(config['WECHAT_SESSION_MEMCACHED'])
            session_interface = MemcachedStorage(
                mc, prefix=config['WECHAT_SESSION_PREFIX'])

        elif config['WECHAT_SESSION_TYPE'] == 'leancloud':
            from .storage import LeanCloudStorage
            session_interface = LeanCloudStorage()

        else:
            from wechatpy.session.memorystorage import MemoryStorage
            session_interface = MemoryStorage()
        return session_interface
Ejemplo n.º 2
0
    def init_app(self, app):
        self.app = app

        config = app.config
        config.setdefault('WECHAT_APPID', None)
        config.setdefault('WECHAT_SECRET', None)
        config.setdefault('WECHAT_TYPE', 0)
        config.setdefault('WECHAT_SESSION_TYPE', None)
        config.setdefault('WECHAT_SESSION_PREFIX', 'flask-wechatpy')
        config.setdefault('WECHAT_AUTO_RETRY', True)
        config.setdefault('WECHAT_TIMEOUT', None)

        assert config['WECHAT_APPID'] is not None
        assert config['WECHAT_SECRET'] is not None

        if config['WECHAT_TYPE'] == 0:
            from wechatpy import WeChatClient
        else:
            from wechatpy.enterprise import WeChatClient

        if config['WECHAT_SESSION_TYPE'] == 'redis':
            from wechatpy.session.redisstorage import RedisStorage
            from redis import Redis
            if config.get('WECHAT_SESSION_REDIS_URL'):
                redis = Redis.from_url(config['WECHAT_SESSION_REDIS_URL'])
            else:
                redis = Redis(host=config.get('WECHAT_SESSION_REDIS_HOST',
                                              'localhost'),
                              port=config.get('WECHAT_SESSION_REDIS_PORT',
                                              6379),
                              db=config.get('WECHAT_SESSION_REDIS_DB', 0),
                              password=config.get('WECHAT_SESSION_REDIS_PASS',
                                                  None))
            session_interface = RedisStorage(
                redis, prefix=config['WECHAT_SESSION_PREFIX'])
        elif config['WECHAT_SESSION_TYPE'] == 'memcached':
            from wechatpy.session.memcachedstorage import MemcachedStorage
            mc = self._get_mc_client(config['WECHAT_SESSION_MEMCACHED'])
            session_interface = MemcachedStorage(
                mc, prefix=config['WECHAT_SESSION_PREFIX'])
        elif config['WECHAT_SESSION_TYPE'] == 'shove':
            pass
        else:
            session_interface = None

        self._wechat_client = WeChatClient(
            config['WECHAT_APPID'],
            config['WECHAT_SECRET'],
            session=session_interface,
            timeout=config['WECHAT_TIMEOUT'],
            auto_retry=config['WECHAT_AUTO_RETRY'],
        )

        if not hasattr(app, 'extensions'):
            app.extensions = {}
        app.extensions['wechatpy'] = self
Ejemplo n.º 3
0
 def set_session(self, redis=None, shove=None, mc=None, prefix="wechatpy"):
     if redis is not None:
         from wechatpy.session.redisstorage import RedisStorage
         self.session = RedisStorage(redis, prefix)
     elif shove is not None:
         from wechatpy.session.shovestorage import ShoveStorage
         self.session = ShoveStorage(shove, prefix)
     elif mc is not None:
         from wechatpy.session.memcachedstorage import MemcachedStorage
         self.session = MemcachedStorage(mc, prefix)
Ejemplo n.º 4
0
    def test_memcached_storage_init(self):
        if platform.system() == 'Windows':
            return

        from pymemcache.client import Client
        from wechatpy.session.memcachedstorage import MemcachedStorage

        servers = ("127.0.0.1", 11211)
        memcached = Client(servers)
        session = MemcachedStorage(memcached)
        client = WeChatClient(self.app_id, self.secret, session=session)
        self.assertTrue(isinstance(client.session, MemcachedStorage))
Ejemplo n.º 5
0
    def test_memcached_storage_access_token(self):
        if platform.system() == 'Windows':
            return

        from pymemcache.client import Client
        from wechatpy.session.memcachedstorage import MemcachedStorage

        servers = ("127.0.0.1", 11211)
        memcached = Client(servers)
        session = MemcachedStorage(memcached)
        client = WeChatClient(self.app_id, self.secret, session=session)
        with HTTMock(wechat_api_mock):
            token = client.fetch_access_token()
            self.assertEqual('1234567890', token['access_token'])
            self.assertEqual(7200, token['expires_in'])
            self.assertEqual('1234567890', client.access_token)