import wsmeext.pecan as wsme_pecan … classResourcesController(rest.RestController): “””Works on resources.”””
def_resource_links(self, resource_id, meter_links=1): links = [_make_link(‘self’, pecan.request.host_url, ‘resources’, resource_id)] if meter_links: for meter in pecan.request.storage_conn.get_meters( resource=resource_id): query = {‘field’: ‘resource_id’, ‘value’: resource_id} links.append(_make_link(meter.name, pecan.request.host_url, ‘meters’, meter.name, query=query)) return links
#这里的get_one对应的就是GET http://<your ip>:8777/v2/resources/<resource_id> #返回的为一个Resource类型 #参数为unicode类型 @wsme_pecan.wsexpose(Resource, unicode) defget_one(self, resource_id): “””Retrieve details about one resource. :param resource_id: The UUID of the resource. “”” authorized_project = acl.get_limited_to_project(pecan.request.headers) resources = list(pecan.request.storage_conn.get_resources( resource=resource_id, project=authorized_project)) ifnot resources: raise EntityNotFound(_(‘Resource’), resource_id) return Resource.from_db_and_links(resources[0], self._resource_links(resource_id)) #这里的get_all对应的就是GET http://<your ip>:8777/v2/resources[?meter_links={meter_links}& #q.field={field}&q.op={operator}&q.type={type}&q.value={value}] #返回类型是包含Resource的序列 #参数类型是包含Query的序列 @wsme_pecan.wsexpose([Resource], [Query], int) defget_all(self, q=None, meter_links=1): “””Retrieve definitions of all of the resources. :param q: Filter rules for the resources to be returned. :param meter_links: option to include related meter links “”” q = q or [] kwargs = _query_to_kwargs(q, pecan.request.storage_conn.get_resources) resources = [ Resource.from_db_and_links(r, self._resource_links(r.resource_id, meter_links)) for r in pecan.request.storage_conn.get_resources(**kwargs)] return resources