def __init__(self, method, **kwargs): if hasattr(method, 'iteration_mode'): if method.iteration_mode == 'id': self.iterator = IdIterator(method, kwargs) elif method.iteration_mode == 'page': self.iterator = PageIterator(method, kwargs) else: raise AnApiOfIceAndFireError('Invalid iteration mode') else: raise AnApiOfIceAndFireError('This method does not pagination')
def build_path(self): for variable in re_path_template.findall(self.path): name = variable.strip('{}') try: value = quote(self.session.params[name]) except KeyError: raise AnApiOfIceAndFireError('No parameter value found for path variable: %s' % name) del self.session.params[name] self.path = self.path.replace(variable, value)
def execute(self): url = 'http://{0}{1}'.format(self.api.host, self.path) try: response = self.session.request(self.method, url) except Exception: raise AnApiOfIceAndFireError('Failed to send request') result = self.parser.parse(self, response.text) return result
def build_parameters(self, kwargs): for key, value in kwargs.items(): if value is None: continue try: if key in self.allowed_parameters: # We need to convert parameter keys to camelCase because the API uses camelCase convention camel_case_key = to_camel_case(key) self.session.params[camel_case_key] = convert_to_utf8_str(value) except IndexError: raise AnApiOfIceAndFireError('Invalid parameters supplied')
def parse(self, method, data): if method.model_type is None: return model = self.model_factory.create(method.model_type) if model is None: raise AnApiOfIceAndFireError('No model for: ' + method.model_type) json = JSONParser.parse(self, method, data) if method.is_data_list: return model.parse_list(method.api, json) else: return model.parse(method.api, json)
def items(self, limit=0): """ Return iterator for IDs :param limit: the number of items that should be returned :return: """ if limit > 0: self.iterator.limit = limit if type(self.iterator) is not IdIterator: raise AnApiOfIceAndFireError( 'This method does not support iterating over IDs') return self.iterator
def parse(self, method, data): try: json_data = json.loads(data) return json_data except Exception: raise AnApiOfIceAndFireError('Failed to parse JSON data')