Exemplo n.º 1
0
Arquivo: root.go Projeto: ZBNA/deploy
func (r *RootService) One() (root *Root, result *Result) {
	root = &Root{HALResource: &hypermedia.HALResource{}}
	result = r.client.get(r.URL, &root)
	if root != nil {
		// Cached hyperlinks
		root.PullsURL = hypermedia.Hyperlink(PullRequestsURL)
	}

	return
}
Exemplo n.º 2
0
func TestResolveClientRelativeHyperlink(t *testing.T) {
	client, err := NewFromString("http://github.enterprise.com/api/v3/", nil)
	if err != nil {
		t.Fatal(err.Error())
	}
	link := hypermedia.Hyperlink("repos/{repo}")
	expanded, err := link.Expand(hypermedia.M{"repo": "foo"})

	u, err := client.ResolveReferenceString(expanded.String())
	if err != nil {
		t.Fatal(err.Error())
	}

	assert.Equal(t, "http://github.enterprise.com/api/v3/repos/foo", u)
}
Exemplo n.º 3
0
func (d *Decoder) Decode(header http.Header) (mediaHeader *MediaHeader) {
	mediaHeader = &MediaHeader{Relations: hypermedia.Relations{}}

	link := header.Get("Link")
	if len(link) == 0 {
		return
	}

	for _, l := range strings.Split(link, ",") {
		l = strings.TrimSpace(l)
		segments := strings.Split(l, ";")

		if len(segments) < 2 {
			continue
		}

		if !strings.HasPrefix(segments[0], "<") || !strings.HasSuffix(segments[0], ">") {
			continue
		}

		url, err := url.Parse(segments[0][1 : len(segments[0])-1])
		if err != nil {
			continue
		}

		link := hypermedia.Hyperlink(url.String())

		for _, segment := range segments[1:] {
			switch strings.TrimSpace(segment) {
			case `rel="next"`:
				mediaHeader.Relations["next"] = link
			case `rel="prev"`:
				mediaHeader.Relations["prev"] = link
			case `rel="first"`:
				mediaHeader.Relations["first"] = link
			case `rel="last"`:
				mediaHeader.Relations["last"] = link
			}
		}
	}

	return
}
Exemplo n.º 4
0
func TestNewResult_Pageable(t *testing.T) {
	resp := &Response{MediaHeader: &mediaheader.MediaHeader{Relations: hypermedia.Relations{"next": hypermedia.Hyperlink("/path")}}}
	result := newResult(resp, nil)

	assert.Equal(t, "/path", string(*result.NextPage))
	assert.T(t, result.PrevPage == nil)
	assert.T(t, result.LastPage == nil)
	assert.T(t, result.FirstPage == nil)
}
Exemplo n.º 5
0
func (l Hyperlink) Expand(m M) (u *url.URL, err error) {
	sawyerHyperlink := hypermedia.Hyperlink(string(l))
	u, err = sawyerHyperlink.Expand(hypermedia.M(m))
	return
}