forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
link_builder.go
69 lines (55 loc) · 1.67 KB
/
link_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package hal
import (
"fmt"
"net/url"
"strings"
)
// StandardPagingOptions is a helper string to make creating paged collection
// URIs simpler.
const StandardPagingOptions = "{?cursor,limit,order}"
// LinkBuilder is a helper for constructing URLs in horizon.
type LinkBuilder struct {
Base *url.URL
}
// Link returns a hal.Link whose href is each of the
// provided parts joined by '/'
func (lb *LinkBuilder) Link(parts ...string) Link {
path := strings.Join(parts, "/")
href := lb.expandLink(path)
return NewLink(href)
}
// PagedLink creates a link using the `Link` method and
// appends the common paging options
func (lb *LinkBuilder) PagedLink(parts ...string) Link {
nl := lb.Link(parts...)
nl.Href += StandardPagingOptions
nl.PopulateTemplated()
return nl
}
// Linkf provides a helper function that returns a link with an
// href created by passing the arguments into fmt.Sprintf
func (lb *LinkBuilder) Linkf(format string, args ...interface{}) Link {
return lb.Link(fmt.Sprintf(format, args...))
}
// expandLink takes an href and resolves it against the LinkBuilders base url,
// if set. NOTE: this method panics if the input href cannot be parsed. It is
// meant to be used by developer author ed links, not with external data.
func (lb *LinkBuilder) expandLink(href string) string {
if lb.Base == nil {
return href
}
u, err := url.Parse(href)
if err != nil {
panic(err)
}
if u.Host == "" {
u.Host = lb.Base.Host
if u.Scheme == "" {
u.Scheme = lb.Base.Scheme
}
}
//HACK: replace the encoded path with the un-encoded path, which preserves
//the uritemplate parameters.
result := strings.Replace(u.String(), u.EscapedPath(), u.Path, -1)
return result
}