def as_dict(self): base = super(HALTypesRepresentation, self).as_dict() representation = HALRepresentation(base) representation.add_link("self", url_for(self.endpoint)) representation.add_curie("hl", RELATIONS_CURIE) representation.add_link("hl:search", url_for("places.search") + "?type={type}") return representation.as_dict()
def get_routes(): path = request.path representation = HALRepresentation({}) representation.add_curie('hl', TRANSPORT_CURIE) representation.add_link('self', '{bp}'.format(bp=path)) representation.add_link('hl:p-r', '{bp}park-and-rides'.format(bp=path), title='Park and Rides Real-Time Information') response = make_response(representation.as_json(), 200) response.headers['Content-Type'] = "application/json" return response
def get_routes(): path = request.path representation = HALRepresentation({}) representation.add_curie('hl', 'http://moxie.readthedocs.org/en/latest/http_api/contact.html#{rel}') representation.add_link('self', '{bp}'.format(bp=path)) representation.add_link('hl:search', '{bp}search?q={{q}}&medium={{medium}}'.format(bp=path), templated=True, title='Search') response = make_response(representation.as_json(), 200) response.headers['Content-Type'] = "application/json" return response
def handle_request(self): ctx = _app_ctx_stack.top services = ctx.app.blueprints self.browser_url = ctx.app.config.get('HAL_BROWSER_REDIRECT', None) representation = HALRepresentation({}) representation.add_link('self', '/') representation.add_curie('app', APPS_CURIE) for service, conf in services.iteritems(): representation.add_link('app:{app}'.format(app=service), '{prefix}/'.format(prefix=conf.url_prefix)) return representation
def get_routes(): path = request.path representation = HALRepresentation({}) representation.add_curie('hl', 'http://moxie.readthedocs.org/en/latest/http_api/library.html#{rel}') representation.add_link('self', '{bp}'.format(bp=path)) representation.add_link('hl:search', '{bp}search?title={{title}}&author={{author}}&isbn={{isbn}}'.format(bp=path), templated=True, title='Search') representation.add_link('hl:item', '{bp}item:{{id}}'.format(bp=path), templated=True, title='POI detail') response = make_response(representation.as_json(), 200) response.headers['Content-Type'] = "application/json" return response
def get_routes(): path = request.path representation = HALRepresentation({}) representation.add_curie('hl', 'http://moxie.readthedocs.org/en/latest/http_api/oxford_dates.html#{rel}') representation.add_link('self', '{bp}'.format(bp=path)) representation.add_link('hl:today', '{bp}today'.format(bp=path), title='Today date') representation.add_link('hl:date', '{bp}{{yyyy}}-{{mm}}-{{dd}}'.format(bp=path), templated=True, title="Format arbitrary date") response = make_response(representation.as_json(), 200) response.headers['Content-Type'] = "application/json" return response
def get_routes(): path = request.path representation = HALRepresentation({}) representation.add_curie('hl', CURIE_ENDPOINT) representation.add_link('self', '{bp}'.format(bp=path)) representation.add_link('hl:bookings', '{bp}bookings'.format(bp=path), title="Bookings") representation.add_link('hl:subjects', '{bp}subjects'.format(bp=path), title="Subjects") representation.add_link('hl:search', '{bp}search?q={{q}}'.format(bp=path), templated=True, title='Search') representation.add_link('hl:course', '{bp}course/{{id}}'.format(bp=path), templated=True, title='Course details') response = make_response(representation.as_json(), 200) response.headers['Content-Type'] = "application/json" return response
def get_routes(): path = request.path representation = HALRepresentation({}) representation.add_curie('hl', PLACES_CURIE) representation.add_link('self', '{bp}'.format(bp=path)) representation.add_link('hl:search', '{bp}search?q={{q}}'.format(bp=path), templated=True, title='Search') representation.add_link('hl:types', '{bp}types'.format(bp=path), title='List of types') representation.add_link('hl:detail', '{bp}{{id}}'.format(bp=path), templated=True, title='POI detail') representation.add_link('hl:rti', '{bp}{{id}}/rti/{{type}}'.format(bp=path), templated=True, title='Real-Time Information for a given POI') response = make_response(representation.as_json(), 200) response.headers['Content-Type'] = "application/json" return response
def get_routes(): path = request.path representation = HALRepresentation({}) representation.add_curie( 'hl', 'http://moxie.readthedocs.org/en/latest/http_api/events.html#{rel}') representation.add_link('self', '{bp}'.format(bp=path)) representation.add_link('hl:search', '{bp}search'.format(bp=path), title="Search events") representation.add_link('hl:event', '{bp}event/{{id}}'.format(bp=path), templated=True, title="Event") response = make_response(representation.as_json(), 200) response.headers['Content-Type'] = "application/json" return response
def test_hal_json_helper_representation(self): representation = HALRepresentation({'a':'b'}) representation.add_link('self', '/a/b') representation.add_link('list', '/list', templated=True) representation.update_link('child', '/child/1') representation.update_link('child', '/child/2') representation.add_curie('cu', 'http://curie.com') representation.add_embed('rel', [HALRepresentation({'embed': 'yes'}, links={'self':{'href':'a'}}).as_dict()]) self.assertDictContainsSubset({'_links': { 'self': {'href': '/a/b'}, 'list': {'href': '/list', 'templated': True}, 'child': [{'href': '/child/1'}, {'href': '/child/2'}], 'curie': {'href': 'http://curie.com', 'name': 'cu', 'templated': True}}}, representation.as_dict()) self.assertDictContainsSubset( {'_embedded': {'rel': [{'embed': 'yes', '_links': {'self': {'href': 'a'}}}]}}, representation.as_dict())
def as_dict(self): base = super(HALPOIRepresentation, self).as_dict() representation = HALRepresentation(base) representation.add_link("self", url_for(self.endpoint, ident=self.poi.id)) if self.poi.files: reps = [FileRepresentation(r).as_dict() for r in self.poi.files] representation.add_embed("files", reps) try: poi_service = POIService.from_context() except NoConfiguredService: poi_service = None if poi_service and self.add_parent_children_links: # Merging all IDs (parent and children) into one set to # do only one query to the service pois_ids = set(self.poi.children) pois_ids.update(self.poi.parent) if self.poi.primary_place: pois_ids.add(self.poi.primary_place) if pois_ids: pois_objects = poi_service.get_places_by_identifiers(pois_ids) # ease lookup by having a dict with ID as key if pois_objects: pois = dict((poi.id, poi) for poi in pois_objects) else: pois = {} def add_link(relation, method, identifier): """Add a link w/ or w/o title depending if we found a POI :param relation: link rel (parent or child) :param method: method to apply (add_link or update if it should be an array) :param identifier: ID of the POI for lookup """ poi = pois.get(identifier, None) if poi and poi.name: method( relation, url_for(self.endpoint, ident=identifier), title=poi.name, type=poi.type, type_name=poi.type_name, ) else: method(relation, url_for(self.endpoint, ident=identifier)) if self.poi.parent: for parent in self.poi.parent: add_link("parent", representation.update_link, parent) if self.poi.primary_place: add_link("primary_place", representation.add_link, self.poi.primary_place) if self.poi.children: for child in self.poi.children: add_link("child", representation.update_link, child) try: transport_service = TransportService.from_context() except NoConfiguredService: # Transport service not configured so no RTI information logger.warning("No configured Transport service", exc_info=True) else: try: provider = transport_service.get_provider(self.poi) except ProviderException: logger.debug("No single provider found for: %s" % self.poi.id) else: representation.add_curie("rti", RTI_CURIE) for rtitype, title in provider.provides.items(): representation.add_link( "rti:%s" % rtitype, url_for("places.rti", ident=self.poi.id, rtitype=rtitype), title=title ) return representation.as_dict()