-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
94 lines (78 loc) · 1.71 KB
/
generic.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 abiquo_api
import (
"errors"
"fmt"
"github.com/go-resty/resty"
)
// Generic DTO
type DTO struct {
Links []Link `json:"links,omitempty"`
}
func (d *DTO) FollowLink(rel string, c *AbiquoClient) (*resty.Response, error) {
return d.FollowLinkWithParams(rel, "", c)
}
func (d *DTO) FollowLinkWithParams(rel string, extraParams string, c *AbiquoClient) (*resty.Response, error) {
link, err := d.GetLink(rel)
if err != nil {
return &resty.Response{}, err
}
uri := link.Href
if len(extraParams) > 0 {
uri = uri + "?" + extraParams
}
resp, err := c.checkResponse(c.client.NewRequest().
SetHeader("Accept", link.Type).
Get(uri))
return resp, err
}
func (d *DTO) GetLink(rel string) (Link, error) {
link := Link{Href: ""}
for _, l := range d.Links {
if l.Rel == rel {
link = l
}
}
if link.Href == "" {
errorMsg := fmt.Sprintf("Link with rel '%s' not found", rel)
return link, errors.New(errorMsg)
} else {
link.trimPort()
return link, nil
}
}
func (d *DTO) Refresh(c *AbiquoClient) (*resty.Response, error) {
edit_lnk, err := d.GetLink("edit")
if err != nil {
edit_lnk, _ = d.GetLink("self")
}
return c.checkResponse(c.client.R().SetHeader("Accept", edit_lnk.Type).
SetHeader("Content-Type", edit_lnk.Type).
Get(edit_lnk.Href))
}
// Generic Collection
type AbstractCollection struct {
Links []Link
TotalSize int
}
func (c *AbstractCollection) GetNext() Link {
link := Link{Href: ""}
for _, l := range c.Links {
if l.Rel == "next" {
link = l
}
}
if link.Href == "" {
return Link{}
} else {
link.trimPort()
return link
}
}
func (c *AbstractCollection) HasNext() bool {
for _, link := range c.Links {
if link.Rel == "next" {
return true
}
}
return false
}