예제 #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()