func newTestResponse() *Response { return &Response{ Response: &http.Response{}, MediaHeader: &mediaheader.MediaHeader{ Relations: hypermedia.Relations{"next": hypermedia.Hyperlink("/path")}, }, } }
// One accesses the root URI templates and assigns them to result // // https://developer.github.com/v3/#root-endpoint 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 }
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) }
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 }
// Url generates full URL to API endpoint for given test case. // urlpath must provide full URL to the endpoint with no query parameters. func (testCase *ApiTestCase) Url(urlpath string) (string, error) { // TODO: what if url template contains something, but it's not provided by test case? sawyerHyperlink := hypermedia.Hyperlink(urlpath) params := hypermedia.M{} for name, p := range testCase.PathParams { params[name] = p.Value } u, err := sawyerHyperlink.Expand(params) if err != nil { return "", err } if testCase.QueryParams != nil { query := url.Values{} for key, param := range testCase.QueryParams { valueStr := fmt.Sprintf("%v", param.Value) query.Set(key, valueStr) } u.RawQuery = query.Encode() } return u.String(), nil }
// Expand utilizes the sawyer expand method to convert a URI template into a full // URL func (l Hyperlink) Expand(m M) (u *url.URL, err error) { sawyerHyperlink := hypermedia.Hyperlink(string(l)) u, err = sawyerHyperlink.Expand(hypermedia.M(m)) return }
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) }