def test_embedded_document(): links = Links() links.add(Link('self', '/somewhere')) # one embedded cow embed = { 'cow': [{ 'tags': ['hello', 'goodbye'], 'message': 'We got some new tags' }] } doc = HalDocument(links, embed=embed) json_doc = doc.to_json() info = json.loads(json_doc) assert '_links' in info assert '_embedded' in info embedded = info['_embedded'] assert 'cow' in embedded cows = embedded['cow'] assert len(cows) == 1 cow = cows[0] assert 'tags' in cow assert 'message' in cow assert cow['tags'] == ['hello', 'goodbye']
def test_embedded_document(): links = Links() links.add(Link('self', '/somewhere')) # one embedded cow embed = {'cow': [{'tags': ['hello', 'goodbye'], 'message': 'We got some new tags'}]} doc = HalDocument(links, embed=embed) json_doc = doc.to_json() info = json.loads(json_doc) assert '_links' in info assert '_embedded' in info embedded = info['_embedded'] assert 'cow' in embedded cows = embedded['cow'] assert len(cows) == 1 cow = cows[0] assert 'tags' in cow assert 'message' in cow assert cow['tags'] == ['hello', 'goodbye']
def test_data_document(): links = Links() links.add(Link('self', '/somewhere')) data = {'tags': ['hello', 'goodbye'], 'message': 'We got some new tags'} doc = HalDocument(links, data=data) json_doc = doc.to_json() info = json.loads(json_doc) assert '_links' in info assert 'tags' in info assert 'message' in info assert info['tags'] == ['hello', 'goodbye']
def _entity_links(self, entity_uri, container): """ Links for a bag or recipe entity. """ server_base = server_base_url(self.environ) links = Links() links.add(self.Curie) links.add(Link('tiddlyweb:%s' % container, '%s/%s' % (server_base, container))) links.add(Link('tiddlyweb:tiddlers', entity_uri + '/tiddlers')) links.add(Link('self', entity_uri)) return links
def test_dataless_document(): links = Links() links.add(Link('example', '/somewhere', type='text/plain')) doc = HalDocument(links) json_doc = doc.to_json() info = json.loads(json_doc) assert len(info) == 1 assert '_links' in info assert 'example' in info['_links'] example = info['_links']['example'] assert example['href'] == '/somewhere' assert example['type'] == 'text/plain'
def list_tiddlers(self, tiddlers): """ Create a list of embedded tiddlers. What link rels are needed is dependent on context, which we have to...guess. """ hal_entities, embed_name, info_tiddler = self._embedded_tiddlers( tiddlers) tiddler_links = self._tiddlers_links(tiddlers, info_tiddler) links = Links() for rel in tiddler_links: links.add(Link(rel, tiddler_links[rel])) links.add(self.Curie) hal_doc = HalDocument(links, embed={embed_name: hal_entities}) return hal_doc.to_json()
def test_simple_links(): link1 = Link('example', '/somewhere', type='text/plain', monkey='foobar', templated=True, name='cow') link2 = Link('self', '/somewhere-else') link3 = Link('example', '/other') links = Links() links.add(link1, link2, link3) links_info = links.structure assert 'example' in links_info assert 'self' in links_info assert links_info['example'][0]['href'] == '/somewhere' assert links_info['example'][1]['href'] == '/other' assert links_info['self']['href'] == '/somewhere-else'
def _embedded_tiddlers(self, tiddlers): """ Calculate the embedded tiddlers, return them, a embed rel and if appropriate a single tiddler holding bag and/or recipe information. """ hal_entities = [] tiddler = None embed_name = 'tiddlyweb:tiddler' for tiddler in tiddlers: links = Links() tiddler_link = tiddler_url(self.environ, tiddler, full=True) if tiddlers.is_revisions: tiddler_link += '/revisions/%s' % encode_name( unicode(tiddler.revision)) embed_name = 'tiddlyweb:revision' links.add(Link('self', tiddler_link)) hal_entity = HalDocument(links, data=self._tiddler_dict(tiddler)) hal_entities.append(hal_entity.structure) return (hal_entities, embed_name, tiddler)
def _hal_root(environ, start_response): """ Compose a root HAL document linking to bags and recipes. """ server_base = server_base_url(environ) links = Links() for rel in ROOT_LINKS: link = Link(rel, '%s%s' % (server_base, ROOT_LINKS[rel]['href'])) kwargs = {} for key in ROOT_LINKS[rel]: if key is not 'href': kwargs[key] = ROOT_LINKS[rel][key] link.kwargs = kwargs links.add(link) links.add(Serialization.Curie) hal = HalDocument(links) start_response('200 OK', [('Content-Type', 'application/hal+json; charset=UTF-8')]) return [hal.to_json()]
def tiddler_as(self, tiddler): links = Links() links.add(self.Curie) if 'revision' in self.environ['wsgiorg.routing_args'][1]: for link in self._revision_links(tiddler): links.add(link) else: for link in self._tiddler_links(tiddler): links.add(link) hal_entity = HalDocument(links, data=self._tiddler_dict( tiddler, fat=True)) return hal_entity.to_json()
def _list_collection(self, entities, self_name, embed_name, url_maker): """ Make a collection of either bags or recipes and returns as HAL JSON. """ server_base = server_base_url(self.environ) hal_entities = self._embedded_entities(entities, url_maker) links = Links() links.add(Link('self', '%s/%s' % (server_base, self_name))) links.add(Link('tiddlyweb:%s' % embed_name, '%s/%s/{%s}' % (server_base, self_name, embed_name), templated=True)) links.add(self.Curie) hal_doc = HalDocument(links, embed={ 'tiddlyweb:%s' % embed_name: hal_entities}) return hal_doc.to_json()
def make_document(entity): links = Links() links.add(Link('self', url_maker(self.environ, entity, full=True))) return HalDocument(links, data={'name': entity.name}).structure