def _post_load(self,response): """ post load processing """ if response is not None and response.status_code == 200: types = helpers.pluralize(self.resource_type) body = json.loads(response.content, encoding='utf-8') self.total_entries = body['collection']['total_entries'] self.total_pages = body['collection']['total_pages'] self.current_page = body['collection']['current_page'] # in case this obj gets reused to run another query reset the result if self.total_entries == 0 and self.total_pages == 1: self.items = [] ## now get the items from the class factory for object in body[types]: item_cls = resources.get_model_class(self.resource_type) properties_dict = object[self.resource_type] new_dict = helpers.remove_properties_containing_None(properties_dict) item = item_cls(new_dict) ## add the items self.items.append(item) #autoload is true, so lets fetch all the other pages recursivly if(self.autoload == True and self.total_pages > 1 and page == None): for x in xrange(2,self.total_pages): self.load(x) return self else: raise SalesKingException("LOAD_ERROR","Fetching failed, an error happend",response)
def _response_item_to_object(self, resp_item): """ take json and make a resource out of it """ item_cls = resources.get_model_class(self.resource_type) properties_dict = resp_item[self.resource_type] new_dict = helpers.remove_properties_containing_None(properties_dict) # raises exception if something goes wrong obj = item_cls(new_dict) return obj
def get_object_from_response(self, response): """ transforms the response into a new object :param response: valid respone status code 200 :returns new instance of current class """ klass = self.schema['title'] cls = get_model_class(klass, api=self.__api__) jdict = json.loads(response.content, encoding="utf-8") ### check if we have a response properties_dict = jdict[self.schema['title']] new_dict = helpers.remove_properties_containing_None(properties_dict) obj = cls(new_dict) return obj
def dict_to_instance(self, content): """ transforms the content to a new instace of object self.schema['title'] :param content: valid response :returns new instance of current class """ klass = self.schema['title'] cls = get_model_class(klass, api=self.__api__) # jdict = json.loads(content, encoding="utf-8") ### check if we have a response properties_dict = content[self.schema['title']][self.schema['title']] #@todo: find a way to handle the data # validation fails if the none values are not removed new_dict = helpers.remove_properties_containing_None(properties_dict) obj = cls(new_dict) #obj.links = content[self.schema['title']]['links'] return obj