コード例 #1
0
ファイル: base.py プロジェクト: campenberger/gl2api
    def _get(self, ObjectSchema, method_info, timeout=2.0, auth=None, *args, **kwargs): # noqa
        headers={"Accept": "application/json"}
        url=self._makeUrl(method_info, ObjectSchema, kwargs)
        r=API.retry(requests.get, url, timeout=timeout, auth=auth, headers=headers)
        unmarshaller=Unmarshaller()
        if r.status_code==200:
            # if we have a field, in the method info, we expect a collection back
            # otherwise it will be just a single element
            if 'field' in method_info:
                field_def={
                    method_info['field']: fields.Nested(ObjectSchema, many=True),
                    "total": fields.Integer()
                }
                data=unmarshaller(r.json(), field_def, partial=True)
                return data[method_info['field']]

            elif 'dict' in method_info and method_info['dict']:
                data=r.json()
                schema=ObjectSchema(strict=True)
                ret={
                    k: schema.load(v).data
                    for (k, v) in data.iteritems()
                }
                return ret

            elif 'list' in method_info and method_info['list']:
                data=r.json()
                schema=ObjectSchema(strict=True)
                return [schema.load(v).data for v in data]
                
            else:
                data=ObjectSchema(strict=True).load(r.json())
                return data.data

        else:
            r.raise_for_status()
コード例 #2
0
 def unmarshal(self):
     return Unmarshaller()