Exemplo n.º 1
0
 def _decode_result(self, res):
     try:
         result = json.loads(res.body)
     except (TypeError, ValueError):
         # Return origin response object if we can not decode it as JSON
         return res
     return result
Exemplo n.º 2
0
 def get(self, key):
     key = self.key_name(key)
     value = self.mc.get(key)
     if not value:
         return None
     try:
         return json.loads(to_text(value))
     except ValueError:
         return value
Exemplo n.º 3
0
def wechat_api_mock(url, request):
    path = url.path.replace("/cgi-bin/", "").replace("/", "_")
    res_file = os.path.join(_FIXTURE_PATH, "%s.json" % path)
    content = {"errcode": 99999, "errmsg": "can not find fixture %s" % res_file}
    headers = {"Content-Type": "application/json"}
    try:
        with open(res_file, "rb") as f:
            content = json.loads(f.read().decode("utf-8"))
    except (IOError, ValueError) as e:
        content["errmsg"] = "Loads fixture {0} failed, error: {1}".format(res_file, e)
    return response(200, content, headers, request=request)
Exemplo n.º 4
0
def wechat_api_mock(url, request):
    path = url.path.replace('/cgi-bin/', '').replace('/', '_')
    res_file = os.path.join(_FIXTURE_PATH, '%s.json' % path)
    content = {
        'errcode': 99999,
        'errmsg': 'can not find fixture'
    }
    headers = {
        'Content-Type': 'application/json'
    }
    try:
        with open(res_file) as f:
            content = json.loads(f.read())
    except (IOError, ValueError):
        pass
    return response(200, content, headers, request=request)
Exemplo n.º 5
0
def wechat_api_mock(url, request):
    path = url.path.replace('/cgi-bin/', '').replace('/', '_')
    if path.startswith('_'):
        path = path[1:]
    res_file = os.path.join(_FIXTURE_PATH, '%s.json' % path)
    content = {
        'errcode': 99999,
        'errmsg': 'can not find fixture %s' % res_file,
    }
    headers = {
        'Content-Type': 'application/json'
    }
    try:
        with open(res_file, 'rb') as f:
            content = json.loads(f.read().decode('utf-8'))
    except (IOError, ValueError) as e:
        print(e)
    return response(200, content, headers, request=request)
Exemplo n.º 6
0
 def get(self, key, default=None):
     key = self.key_name(key)
     value = self.mc.get(key)
     if value is None:
         return default
     return json.loads(to_text(value))