def get_detail_uri(self, model_instance): parts = [] if self.parent: param_name = self.parent.singular_name # bypass fetch hook and just grab the url directly parent_path = model_instance.get_bound().get(param_name) assert parent_path is not None host = url_to_host(self.api.base_url) parent_url = urljoin(host, parent_path) parts = [ parent_url, self.relative_path, getattr(model_instance, self.id_attribute), ] else: parts = [ self.api.base_url, self.relative_path, getattr(model_instance, self.id_attribute), ] return urljoin(*parts)
def get_detail_uri(self, model_instance): parts = [] if self.parent: param_name = self.parent.singular_name # bypass fetch hook and just grab the url directly parent_path = model_instance.get_bound().get(param_name) assert parent_path is not None host = url_to_host(self.api.base_url) parent_url = urljoin(host, parent_path) parts = [ parent_url, self.relative_path, getattr(model_instance, self.id_attribute), ] else: parts = [ self.api.base_url, self.relative_path, getattr(model_instance, self.id_attribute), ] return urljoin(*parts)
def traverse(dct, parent=None): resources = {} for name, res in dct.items(): host = url_to_host(self.base_url) apidoc_url = urljoin(host, res.get('apidoc')) schema_url = urljoin(host, res.get('schema')) resource = Resource( self, name, res.get('relativePath'), schema_url, apidoc_url, parent=parent, ) self._resources[name] = resource resources[name] = resource child_resources = {} for att, val in res.items(): if att == 'children': child_resources.update(traverse(val, parent=resource)) resource.children = child_resources return resources
def traverse(dct, parent=None): resources = {} for name, res in dct.items(): host = url_to_host(self.base_url) apidoc_url = urljoin(host, res.get('apidoc')) schema_url = urljoin(host, res.get('schema')) resource = Resource( self, name, res.get('relativePath'), schema_url, apidoc_url, parent=parent, ) self._resources[name] = resource resources[name] = resource child_resources = {} for att, val in res.items(): if att == 'children': child_resources.update( traverse(val, parent=resource) ) resource.children = child_resources return resources
def test_url_joining_exhaustive(self): self.assertEqual('http://www.host.com/a/b/', urljoin('http://www.host.com/a/', '/b/')) self.assertEqual('http://www.host.com/a/b/', urljoin('http://www.host.com/a/', '/b')) self.assertEqual('http://www.host.com/a/b/', urljoin('http://www.host.com/a/', 'b/')) self.assertEqual('http://www.host.com/a/b/', urljoin('http://www.host.com/a/', 'b')) self.assertEqual('http://www.host.com/a/b/', urljoin('http://www.host.com/a', '/b/')) self.assertEqual('http://www.host.com/a/b/', urljoin('http://www.host.com/a', '/b')) self.assertEqual('http://www.host.com/a/b/', urljoin('http://www.host.com/a', 'b/')) self.assertEqual('http://www.host.com/a/b/', urljoin('http://www.host.com/a', 'b')) self.assertEqual('http://www.host.com/a/b/c/', urljoin('http://www.host.com/a', 'b', 'c'))
def subview_put(self, view_name, obj, data, **kwargs): method = 'put' view = obj._resource.schema.views.get(view_name) if not view: raise InvalidView(view_name) url = urljoin(obj.base_url, view.get(method).relpath) return self.call_api(url, data=data, method=method)
def subview_put(self, view_name, obj, data, **kwargs): method = 'put' view = obj._resource.schema.views.get(view_name) if not view: raise InvalidView(view_name) url = urljoin(obj.base_url, view.get(method).relpath) return self.call_api(url, data=data, method=method)
def get_request_obj(self, path, data=None, method='get'): url = urljoin(self.base_url, path) auth = None if self.basic_auth: auth = HTTPBasicAuth(self.basic_auth[0], self.basic_auth[1]) # cache this object for clients that need to introspect it self.last_apireq = ApiRequest(url, data, method, auth=auth) return self.last_apireq
def get_request_obj(self, path, data=None, method='get'): url = urljoin(self.base_url, path) auth = None if self.basic_auth: auth = HTTPBasicAuth(self.basic_auth[0], self.basic_auth[1]) # cache this object for clients that need to introspect it self.last_apireq = ApiRequest(url, data, method, auth=auth) return self.last_apireq
def test_html_404(self): '''Test case for 404 infinite loop bug in debug middleware nav on arbitrary 404 urls.''' url = uri.urljoin(self.api.base_url, 'wrong/path/') apireq = self.api.get_request_obj(url) # request html because we want the browser view apireq.standard_headers.update({'Accept': 'text/html'}) with self.assertRaises(exc.Http404): apireq.perform()
def get_list_uri(self, model_instance_parent=None): parts = [] if model_instance_parent: parts = [ model_instance_parent.base_url, self.relative_path, ] else: parts = [ self.api.base_url, self.relative_path, ] return urljoin(*parts)
def get_list_uri(self, model_instance_parent=None): parts = [] if model_instance_parent: parts = [ model_instance_parent.base_url, self.relative_path, ] else: parts = [ self.api.base_url, self.relative_path, ] return urljoin(*parts)
def test_url_joining_exhaustive(self): self.assertEqual( 'http://www.host.com/a/b/', urljoin('http://www.host.com/a/', '/b/') ) self.assertEqual( 'http://www.host.com/a/b/', urljoin('http://www.host.com/a/', '/b') ) self.assertEqual( 'http://www.host.com/a/b/', urljoin('http://www.host.com/a/', 'b/') ) self.assertEqual( 'http://www.host.com/a/b/', urljoin('http://www.host.com/a/', 'b') ) self.assertEqual( 'http://www.host.com/a/b/', urljoin('http://www.host.com/a', '/b/') ) self.assertEqual( 'http://www.host.com/a/b/', urljoin('http://www.host.com/a', '/b') ) self.assertEqual( 'http://www.host.com/a/b/', urljoin('http://www.host.com/a', 'b/') ) self.assertEqual( 'http://www.host.com/a/b/', urljoin('http://www.host.com/a', 'b') ) self.assertEqual( 'http://www.host.com/a/b/c/', urljoin('http://www.host.com/a', 'b', 'c') )
def get_model_instance_by_path(self, path): '''Return a model instance from a path. This implementation is a bit clunky, as it relies on path comparisons and slicing.''' host = url_to_host(self.base_url) path = url_to_path(path) url = urljoin(host, path) # remove the base_path of the api path = re.sub('^{0}'.format(re.escape(self.base_url)), '', url) # in the remaining path, pattern match for the resource_name and id resource_name, id = re.findall('([^/]+)[/]([^/]+)[/]$', path)[0] resource = self.get_resource(resource_name) dct = self.call_api(url) obj = resource.model_class.get_instance(_atts=dct) return obj
def get_model_instance_by_path(self, path): '''Return a model instance from a path. This implementation is a bit clunky, as it relies on path comparisons and slicing.''' host = url_to_host(self.base_url) path = url_to_path(path) url = urljoin(host, path) # remove the base_path of the api path = re.sub('^{0}'.format(re.escape(self.base_url)), '', url) # in the remaining path, pattern match for the resource_name and id resource_name, id = re.findall('([^/]+)[/]([^/]+)[/]$', path)[0] resource = self.get_resource(resource_name) dct = self.call_api(url) obj = resource.model_class.get_instance(_atts=dct) return obj
def get(self, model_class, **kwargs): atts = {} # lookup by id id = kwargs.get('id') if id: uri, kwargs = self.get_list_uri(model_class, **kwargs) uri = urljoin(uri, id) atts = self.call_api(uri) # lookup by other params else: dct = self._filter_raw(model_class, **kwargs) objs = dct.get('objects') if not objs: raise ObjectDoesNotExist elif len(objs) > 1: raise MultipleObjectsReturned atts = objs[0] return model_class.get_instance(_atts=atts)
def get(self, model_class, **kwargs): atts = {} # lookup by id id = kwargs.get('id') if id: uri, kwargs = self.get_list_uri(model_class, **kwargs) uri = urljoin(uri, id) atts = self.call_api(uri) # lookup by other params else: dct = self._filter_raw(model_class, **kwargs) objs = dct.get('objects') if not objs: raise ObjectDoesNotExist elif len(objs) > 1: raise MultipleObjectsReturned atts = objs[0] return model_class.get_instance(_atts=atts)