-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_properties.go
175 lines (136 loc) · 4.37 KB
/
get_properties.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package apaleo
import (
"net/http"
"net/url"
"github.com/omniboost/go-apaleo/utils"
)
func (c *Client) NewGetPropertiesRequest() GetPropertiesRequest {
return GetPropertiesRequest{
client: c,
queryParams: c.NewGetPropertiesQueryParams(),
pathParams: c.NewGetPropertiesPathParams(),
method: http.MethodGet,
headers: http.Header{},
requestBody: c.NewGetPropertiesRequestBody(),
}
}
type GetPropertiesRequest struct {
client *Client
queryParams *GetPropertiesQueryParams
pathParams *GetPropertiesPathParams
method string
headers http.Header
requestBody GetPropertiesRequestBody
}
func (c *Client) NewGetPropertiesQueryParams() *GetPropertiesQueryParams {
return &GetPropertiesQueryParams{}
}
type GetPropertiesQueryParams struct {
// Filter result by property status
Status []string `schema:"status,omitempty"`
// Include archived properties in the result. If not set, or set to false, it only returns non-archived properties
IncludeArchived bool `schema:"includeArchived,omitempty"`
// Filter result by country code
CountryCode []string `schema:"countryCode,omitempty"`
// Page number, 1-based. Default value is 1 (if this is not set or not positive). Results in 204 if there are no items on that page.
PageNumber int `schema:"pageNumber,omitempty"`
// Page size. If this is not set or not positive, the pageNumber is ignored and all items are returned.
PageSize int `schema:"pageSize,omitempty"`
// List of all embedded resources that should be expanded in the response. Possible values are: actions. All other values will be silently ignored.
Expand []string `schema:"expand,omitempty"`
}
func (p GetPropertiesQueryParams) ToURLValues() (url.Values, error) {
encoder := utils.NewSchemaEncoder()
encoder.RegisterEncoder(Date{}, utils.EncodeSchemaMarshaler)
encoder.RegisterEncoder(DateTime{}, utils.EncodeSchemaMarshaler)
params := url.Values{}
err := encoder.Encode(p, params)
if err != nil {
return params, err
}
return params, nil
}
func (r *GetPropertiesRequest) QueryParams() *GetPropertiesQueryParams {
return r.queryParams
}
func (c *Client) NewGetPropertiesPathParams() *GetPropertiesPathParams {
return &GetPropertiesPathParams{}
}
type GetPropertiesPathParams struct {
}
func (p *GetPropertiesPathParams) Params() map[string]string {
return map[string]string{}
}
func (r *GetPropertiesRequest) PathParams() *GetPropertiesPathParams {
return r.pathParams
}
func (r *GetPropertiesRequest) PathParamsInterface() PathParams {
return r.pathParams
}
func (r *GetPropertiesRequest) SetMethod(method string) {
r.method = method
}
func (r *GetPropertiesRequest) Method() string {
return r.method
}
func (s *Client) NewGetPropertiesRequestBody() GetPropertiesRequestBody {
return GetPropertiesRequestBody{}
}
type GetPropertiesRequestBody struct {
}
func (r *GetPropertiesRequest) RequestBody() *GetPropertiesRequestBody {
return nil
}
func (r *GetPropertiesRequest) RequestBodyInterface() interface{} {
return nil
}
func (r *GetPropertiesRequest) SetRequestBody(body GetPropertiesRequestBody) {
r.requestBody = body
}
func (r *GetPropertiesRequest) NewResponseBody() *GetPropertiesResponseBody {
return &GetPropertiesResponseBody{}
}
type GetPropertiesResponseBody struct {
Count int `json:"count"`
Properties Properties `json:"properties"`
}
func (r *GetPropertiesRequest) URL() *url.URL {
u := r.client.GetEndpointURL("inventory/v1/properties", r.PathParams())
return &u
}
func (r *GetPropertiesRequest) Do() (GetPropertiesResponseBody, error) {
// Create http request
req, err := r.client.NewRequest(nil, r)
if err != nil {
return *r.NewResponseBody(), err
}
// Process query parameters
err = utils.AddQueryParamsToRequest(r.QueryParams(), req, true)
if err != nil {
return *r.NewResponseBody(), err
}
responseBody := r.NewResponseBody()
_, err = r.client.Do(req, responseBody)
return *responseBody, err
}
func (r *GetPropertiesRequest) All() (Properties, error) {
properties := Properties{}
for {
resp, err := r.Do()
if err != nil {
return properties, err
}
// Break out of loop when no properties are found
if len(resp.Properties) == 0 {
break
}
// Add properties to list
properties = append(properties, resp.Properties...)
if len(properties) == resp.Count {
break
}
// Increment page number
r.QueryParams().PageNumber = r.QueryParams().PageNumber + 1
}
return properties, nil
}