-
Notifications
You must be signed in to change notification settings - Fork 179
/
link.go
94 lines (78 loc) · 2.75 KB
/
link.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package models
import (
"github.com/gorilla/mux"
"github.com/onflow/flow-go/model/flow"
)
func (l *Links) Build(link string, err error) error {
if err != nil {
return err
}
l.Self = link
return nil
}
// LinkGenerator generates the expandable value for the known endpoints
// e.g. "/v1/blocks/c5e935bc75163db82e4a6cf9dc3b54656709d3e21c87385138300abd479c33b7"
type LinkGenerator interface {
BlockLink(id flow.Identifier) (string, error)
TransactionLink(id flow.Identifier) (string, error)
TransactionResultLink(id flow.Identifier) (string, error)
PayloadLink(id flow.Identifier) (string, error)
ExecutionResultLink(id flow.Identifier) (string, error)
AccountLink(address string) (string, error)
CollectionLink(id flow.Identifier) (string, error)
}
type LinkFunc func(id flow.Identifier) (string, error)
type LinkGeneratorImpl struct {
router *mux.Router
}
func NewLinkGeneratorImpl(router *mux.Router) *LinkGeneratorImpl {
return &LinkGeneratorImpl{
router: router,
}
}
func (generator *LinkGeneratorImpl) BlockLink(id flow.Identifier) (string, error) {
return generator.linkForID("getBlocksByIDs", id)
}
func (generator *LinkGeneratorImpl) PayloadLink(id flow.Identifier) (string, error) {
return generator.linkForID("getBlockPayloadByID", id)
}
func (generator *LinkGeneratorImpl) ExecutionResultLink(id flow.Identifier) (string, error) {
return generator.linkForID("getExecutionResultByID", id)
}
func (generator *LinkGeneratorImpl) TransactionLink(id flow.Identifier) (string, error) {
return generator.linkForID("getTransactionByID", id)
}
func (generator *LinkGeneratorImpl) TransactionResultLink(id flow.Identifier) (string, error) {
return generator.linkForID("getTransactionResultByID", id)
}
func (generator *LinkGeneratorImpl) CollectionLink(id flow.Identifier) (string, error) {
return generator.linkForID("getCollectionByID", id)
}
func (generator *LinkGeneratorImpl) AccountLink(address string) (string, error) {
return generator.link("getAccount", "address", address)
}
// SelfLink generates the _link key value pair for the response
// e.g.
// "_links": { "_self": "/v1/blocks/c5e935bc75163db82e4a6cf9dc3b54656709d3e21c87385138300abd479c33b7" sx}
func SelfLink(id flow.Identifier, linkFun LinkFunc) (*Links, error) {
url, err := linkFun(id)
if err != nil {
return nil, err
}
var link Links
err = link.Build(url, nil)
if err != nil {
return nil, err
}
return &link, nil
}
func (generator *LinkGeneratorImpl) linkForID(route string, id flow.Identifier) (string, error) {
return generator.link(route, "id", id.String())
}
func (generator *LinkGeneratorImpl) link(route string, key string, value string) (string, error) {
url, err := generator.router.Get(route).URLPath(key, value)
if err != nil {
return "", err
}
return url.String(), nil
}