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']
Esempio n. 4
0
def gdata (url, key, prt='no'):                 # get the data from the soaring spot and return it as a HAL document
        global auth                             # auth and apiurl are globals 
        global apiurl 
        j_obj = getapidata(url, auth)           # call the fuction that get it
                                                # convert to HAL
        if prt == 'yes':                        # if print is required 
                print json.dumps(j_obj, indent=4)
        cd=HalDocument.get_data(HalDocument.from_python(j_obj), apiurl+'rel/' + key) # get the data from the HAL document
        return cd
    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. 6
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. 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_simple_consumption():

    data = {
        u'_links': {
            u'self': {
                u'href': u'/hi'
            }
        },
        'title': 'cow list',
        '_embedded': {
            'cow': [{
                'name': 'how'
            }, {
                'name': 'now'
            }, {
                'name': 'brown'
            }]
        }
    }

    doc = HalDocument.from_python(data)
    info = json.loads(doc.to_json())

    assert info['_links'] == data['_links']
    assert info['title'] == data['title']
    assert info['_embedded'] == data['_embedded']

    assert doc.links == data['_links']
    assert doc.links == info['_links']

    assert doc.get_data('cow') == data['_embedded']['cow']
Esempio n. 9
0
def test_handle_curie():
    data = {
        '_links': {
            'self': {
                'href': '/hi'
            },
            'curie': {
                'href': 'http://example.com/barnacle/{rel}',
                'name': 'barnacle',
                'templated': True
            },
            'barnacle:shell': {
                'href': '/some/path'
            }
        },
        '_embedded': {
            'barnacle:cow': [{
                'name': 'how'
            }, {
                'name': 'now'
            }, {
                'name': 'brown'
            }]
        }
    }

    doc = HalDocument.from_python(data)
    curies = doc.get_curies()

    assert 'barnacle' in curies

    resolver = Resolver(curies)

    assert resolver.expand('barnacle:cow') == 'http://example.com/barnacle/cow'
Esempio n. 10
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. 11
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. 12
0
    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. 13
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. 14
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()]
Esempio n. 15
0
def test_handle_curie():
    data = {
        "_links": {
            "self": {"href": "/hi"},
            "curie": {"href": "http://example.com/barnacle/{rel}", "name": "barnacle", "templated": True},
            "barnacle:shell": {"href": "/some/path"},
        },
        "_embedded": {"barnacle:cow": [{"name": "how"}, {"name": "now"}, {"name": "brown"}]},
    }

    doc = HalDocument.from_python(data)
    curies = doc.get_curies()

    assert "barnacle" in curies

    resolver = Resolver(curies)

    assert resolver.expand("barnacle:cow") == "http://example.com/barnacle/cow"
Esempio n. 16
0
def test_simple_consumption():

    data = {
        u"_links": {u"self": {u"href": u"/hi"}},
        "title": "cow list",
        "_embedded": {"cow": [{"name": "how"}, {"name": "now"}, {"name": "brown"}]},
    }

    doc = HalDocument.from_python(data)
    info = json.loads(doc.to_json())

    assert info["_links"] == data["_links"]
    assert info["title"] == data["title"]
    assert info["_embedded"] == data["_embedded"]

    assert doc.links == data["_links"]
    assert doc.links == info["_links"]

    assert doc.get_data("cow") == data["_embedded"]["cow"]
Esempio n. 17
0
def test_go_the_distance():
    # GET root
    response, content = http.request('http://0.0.0.0:8080/',
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200'
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'

    document = HalDocument.from_json(content)
    resolver = Resolver(document.get_curies())
    links = document.links

    assert 'tiddlyweb:bags' in links
    assert (resolver.expand('tiddlyweb:bags')
            == 'http://tiddlyweb.com/relations/bags')

    target_link = links['tiddlyweb:bags']['href']
    assert target_link == 'http://0.0.0.0:8080/bags'

    # GET bags
    response, content = http.request(target_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200'
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'

    document = HalDocument.from_json(content)
    links = document.links

    data = document.get_data('tiddlyweb:bag')
    assert len(data) == 0

    bags_link = target_link

    # Make a bag
    assert 'tiddlyweb:bag' in links
    target_link = expand(links['tiddlyweb:bag']['href'], {'bag': 'mybag'})
    assert target_link == 'http://0.0.0.0:8080/bags/mybag'

    response, content = http.request(target_link,
            method='PUT',
            headers={'Content-Type': 'application/json'},
            body='{"desc": "my own special bag"}')

    assert response['status'] == '204'
    assert response['location'] == target_link

    # GET bags again, this time there is one
    response, content = http.request(bags_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200'
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'

    document = HalDocument.from_json(content)
    links = document.links

    data = document.get_data('tiddlyweb:bag')
    assert len(data) == 1
    assert data[0]['name'] == 'mybag'

    # GET the single bag
    response, content = http.request(target_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200'
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'
    document = HalDocument.from_json(content)
    links = document.links

    assert 'tiddlyweb:tiddlers' in links

    target_link = links['tiddlyweb:tiddlers']['href']

    # GET the (empty) tiddlers
    response, content = http.request(target_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200', content
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'
    document = HalDocument.from_json(content)
    links = document.links

    data = document.get_data('tiddlyweb:tiddler')
    assert len(data) == 0

    tiddlers_link = target_link

    target_link = expand(links['tiddlyweb:tiddler']['href'],
            {'tiddler': 'mytiddler'})
    response, content = http.request(target_link,
            method='PUT',
            headers={'Content-Type': 'application/json'},
            body='{"text": "oh hai", "tags": ["tagone", "tagtwo"]}')

    assert response['status'] == '204'
    assert response['location'] == target_link

    # GET the (no longer empty) tiddlers
    response, content = http.request(tiddlers_link,
            headers={'Accept': 'application/hal+json'})
    assert response['status'] == '200', content
    assert response['content-type'] == 'application/hal+json; charset=UTF-8'
    document = HalDocument.from_json(content)
    links = document.links

    data = document.get_data('tiddlyweb:tiddler')
    assert len(data) == 1

    assert data[0]['title'] == 'mytiddler'