def _entity_as(self, entity_structure, entity_uri, container):
     """
     A single bag or recipe.
     """
     links = self._entity_links(entity_uri, container)
     hal_entity = HalDocument(links, data=entity_structure)
     return hal_entity.to_json()
Esempio n. 2
0
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']
Esempio n. 3
0
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 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()
Esempio n. 5
0
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']
Esempio n. 6
0
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'
Esempio n. 7
0
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']
Esempio n. 8
0
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_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()
Esempio n. 10
0
    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()
Esempio n. 11
0
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()]