def __init__(self, resource_type, api, **kwargs): self.__api__ = api if isinstance(resource_type, dict): self.resource_type = resource_type['type'] else: self.resource_type = resource_type self.schema = loaders.load_schema_raw(self.resource_type) self.filters = dict() self.current_page = None self.total_pages = None self.total_entries = None self.per_page = 100 self.sort = u"ASC" self.sort_by = None self._items = [] self._types = DEFAULT_TYPES # type mapping self._last_query_str = None
def get_model_class( klass, api = None, use_request_api = True): """ Generates the Model Class based on the klass loads automatically the corresponding json schema file form schemes folder :param klass: json schema filename :param use_request_api: if True autoinitializes request class if api is None :param api: the transportation api if none the default settings are taken an instantiated """ if api is None and use_request_api: api = APIClient() _type = klass if isinstance(klass, dict): _type = klass['type'] schema = loaders.load_schema_raw(_type) model_cls = model_factory(schema, base_class = RemoteResource) model_cls.__api__ = api return model_cls
def __init__(self, resource_type, api, **kwargs): self.__api__ = api if isinstance(resource_type,dict): self.resource_type = resource_type['type'] else: self.resource_type = resource_type self.schema = loaders.load_schema_raw(self.resource_type) self.autoload = False self.filters = dict() self.items = [] self.current_page = None self.total_pages = None self.total_entries = None self.per_page = 100 self.sort = u"ASC" self.sort_by = None self._types = DEFAULT_TYPES # type mapping self._last_query_str = None
def set_resource_type(self, klass): """ set type to load and load schema """ self.resource_type = klass self.schema = loaders.load_schema_raw(self.resource_type)
def get_model_class(klass, api=None, use_request_api=True): """ Generates the Model Class based on the klass loads automatically the corresponding json schema file form schemes folder :param klass: json schema filename :param use_request_api: if True autoinitializes request class if api is None :param api: the transportation api if none the default settings are taken an instantiated """ if api is None and use_request_api: api = APIClient() _type = klass if isinstance(klass, dict): _type = klass['type'] schema = loaders.load_schema_raw(_type) model_cls = model_factory(schema, base_class=RemoteResource) model_cls.__api__ = api return model_cls #class CallableMixin(object): # """ # Resource provides the main functionality behind slumber. It handles the # attribute -> url, kwarg -> query param, and other related behind the scenes # python to HTTP transformations. It's goal is to represent a single resource # which may or may not have children. # # It assumes that a Meta class exists at self._meta with all the required # attributes. # """ # # def __init__(self, *args, **kwargs): # self._store = kwargs # # def __call__(self, id=None, format=None, url_override=None): # """ # Returns a new instance of self modified by one or more of the available # parameters. These allows us to do things like override format for a # specific request, and enables the api.resource(ID).get() syntax to get # a specific resource by it's ID. # """ # # # Short Circuit out if the call is empty # if id is None and format is None and url_override is None: # return self # # kwargs = {} # for key, value in self._store.iteritems(): # kwargs[key] = value # # if id is not None: # kwargs["base_url"] = url_join(self._store["base_url"], id) # # if format is not None: # kwargs["format"] = format # # if url_override is not None: # # @@@ This is hacky and we should probably figure out a better way # # of handling the case when a POST/PUT doesn't return an object # # but a Location to an object that we need to GET. # kwargs["base_url"] = url_override # # kwargs["session"] = self._store["session"] # # return self.__class__(**kwargs) #class ResourceProxy(object): # """Proxy object to a resource # # It lazily fetches data. # """ # # def __init__(self, url, service, api): # self._url = url # self._service = service # self._api = api # self._type, id = self._service.parse_resource_url(self._url) # self._id = int(id) # self._resource = None # # def __repr__(self): # if self._resource: # return repr(self._resource) # else: # return '<ResourceProxy %s/%s>' % (self._type, self._id) # # def __getattr__(self, attr): # return getattr(self._get(), attr) # # def __getitem__(self, item): # return self._get()[item] # # def __contains__(self, attr): # return attr in self._get() # # def _get(self): # """Load the resource # # Do nothing if already loaded. # """ # if not self._resource: # self._resource = self._api(self._type, self._id) # return self._resource # # #class Resource(object): # """A fetched resource""" # # def __init__(self, resource, type, id, url): # self._resource = resource # self._type = type # self._id = id # self._url = url # # def __repr__(self): # return '<Resource %s: %s>' % (self._url, self._resource) # # def __getattr__(self, attr): # if attr in self._resource: # return self._resource[attr] # else: # raise AttributeError(attr) # # def __getitem__(self, item): # if item in self._resource: # return self._resource[item] # else: # raise KeyError(item) # # def __contains__(self, attr): # return attr in self._resource
def set_resource_type(self,klass): """ set type to load and load schema """ self.resource_type = klass self.schema = loaders.load_schema_raw(self.resource_type)
def get_model_class( klass, api = None, use_request_api = True): """ Generates the Model Class based on the klass loads automatically the corresponding json schema file form schemes folder :param klass: json schema filename :param use_request_api: if True autoinitializes request class if api is None :param api: the transportation api if none the default settings are taken an instantiated """ if api is None and use_request_api: api = APIClient() _type = klass if isinstance(klass, dict): _type = klass['type'] schema = loaders.load_schema_raw(_type) model_cls = model_factory(schema, base_class = RemoteResource) model_cls.__api__ = api return model_cls #class CallableMixin(object): # """ # Resource provides the main functionality behind slumber. It handles the # attribute -> url, kwarg -> query param, and other related behind the scenes # python to HTTP transformations. It's goal is to represent a single resource # which may or may not have children. # # It assumes that a Meta class exists at self._meta with all the required # attributes. # """ # # def __init__(self, *args, **kwargs): # self._store = kwargs # # def __call__(self, id=None, format=None, url_override=None): # """ # Returns a new instance of self modified by one or more of the available # parameters. These allows us to do things like override format for a # specific request, and enables the api.resource(ID).get() syntax to get # a specific resource by it's ID. # """ # # # Short Circuit out if the call is empty # if id is None and format is None and url_override is None: # return self # # kwargs = {} # for key, value in self._store.iteritems(): # kwargs[key] = value # # if id is not None: # kwargs["base_url"] = url_join(self._store["base_url"], id) # # if format is not None: # kwargs["format"] = format # # if url_override is not None: # # @@@ This is hacky and we should probably figure out a better way # # of handling the case when a POST/PUT doesn't return an object # # but a Location to an object that we need to GET. # kwargs["base_url"] = url_override # # kwargs["session"] = self._store["session"] # # return self.__class__(**kwargs) #class ResourceProxy(object): # """Proxy object to a resource # # It lazily fetches data. # """ # # def __init__(self, url, service, api): # self._url = url # self._service = service # self._api = api # self._type, id = self._service.parse_resource_url(self._url) # self._id = int(id) # self._resource = None # # def __repr__(self): # if self._resource: # return repr(self._resource) # else: # return '<ResourceProxy %s/%s>' % (self._type, self._id) # # def __getattr__(self, attr): # return getattr(self._get(), attr) # # def __getitem__(self, item): # return self._get()[item] # # def __contains__(self, attr): # return attr in self._get() # # def _get(self): # """Load the resource # # Do nothing if already loaded. # """ # if not self._resource: # self._resource = self._api(self._type, self._id) # return self._resource # # #class Resource(object): # """A fetched resource""" # # def __init__(self, resource, type, id, url): # self._resource = resource # self._type = type # self._id = id # self._url = url # # def __repr__(self): # return '<Resource %s: %s>' % (self._url, self._resource) # # def __getattr__(self, attr): # if attr in self._resource: # return self._resource[attr] # else: # raise AttributeError(attr) # # def __getitem__(self, item): # if item in self._resource: # return self._resource[item] # else: # raise KeyError(item) # # def __contains__(self, attr): # return attr in self._resource