This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactions.go
196 lines (159 loc) · 6.95 KB
/
actions.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package zuora
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type actionsService struct {
http Doer
authHeaderProvider AuthHeaderProvider
baseURL string
isPce bool
}
func newActionsService(http Doer, authHeaderProvider AuthHeaderProvider, baseURL string, isPce bool) *actionsService {
return &actionsService{
http: http,
authHeaderProvider: authHeaderProvider,
baseURL: baseURL,
isPce: isPce,
}
}
// Query The query call sends a query expression by specifying the object to query,
// the fields to retrieve from that object, and any filters to determine whether a
// given object should be queried.
// You can use Zuora Object Query Language (ZOQL) to construct those queries,
// passing them through the queryString.
// https://knowledgecenter.zuora.com/DC_Developers/K_Zuora_Object_Query_Language
// Once the call is made, the API executes the query against the specified object and
// returns a query response object to your application. Your application can then iterate
// through rows in the query response to retrieve information.
//
// Limitations
// This call has the following limitations:
//
// * All ZOQL keywords must be in lower case.
//
// * The number of records returned is limited to 2000 records
//
// * The Invoice Settlement feature is not supported. This feature includes Unapplied Payments, Credit and Debit Memo, and Invoice Item Settlement. The Orders feature is also not supported.
//
// *The default WSDL version for Actions is 79.
func (t *actionsService) Query(ctx context.Context, zoqlQuery string) ([]byte, error) {
authHeader, err := t.authHeaderProvider.AuthHeaders(ctx)
if err != nil {
return nil, responseError{isTemporary: false, message: fmt.Sprintf("error while trying to set auth headers: %v", err)}
}
var url string
if t.isPce {
url = fmt.Sprintf("%v:19016/v1/action/query", t.baseURL)
} else {
url = fmt.Sprintf("%v/v1/action/query", t.baseURL)
}
var buffer bytes.Buffer
buffer.WriteString(`{ "queryString" : "`)
buffer.WriteString(strings.TrimSpace(zoqlQuery))
buffer.WriteString(`"}`)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBufferString(buffer.String()))
if err != nil {
return nil, responseError{isTemporary: false, message: fmt.Sprintf("error while trying to create an HTTP request: %v", err)}
}
req.Header.Add("Authorization", authHeader)
req.Header.Add("Content-Type", "application/json")
if ctx.Value(ContextKeyZuoraEntityIds) != nil {
req.Header.Add("Zuora-Entity-Ids", ctx.Value(ContextKeyZuoraEntityIds).(string))
}
if ctx.Value(ContextKeyZuoraTrackID) != nil {
req.Header.Add("Zuora-Track-Id", ctx.Value(ContextKeyZuoraTrackID).(string))
}
res, err := t.http.Do(req.WithContext(ctx))
if err != nil {
return nil, responseError{isTemporary: false, message: fmt.Sprintf("error while trying to make request: %v", err)}
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if res.StatusCode < 200 || res.StatusCode > 299 {
var isTemporary bool
if http.StatusRequestTimeout == res.StatusCode ||
http.StatusTooManyRequests == res.StatusCode ||
http.StatusInternalServerError == res.StatusCode ||
http.StatusServiceUnavailable == res.StatusCode {
isTemporary = true
}
if err != nil {
return nil, responseError{isTemporary: isTemporary, message: fmt.Sprintf("error while trying to read body response into memory. Response Code: %v - Error: %v", res.StatusCode, err)}
}
return nil, responseError{isTemporary: isTemporary, message: fmt.Sprintf("got an invalid http status. Response Code: %v - Body: %v", res.StatusCode, string(body))}
}
return body, nil
}
// Query The create call can be used to create zObjects in bulk.
// API Reference: https://www.zuora.com/developer/api-reference/#operation/Action_POSTcreate
//
// The useSingleTransaction param controls how the objects are created. If set to false (the default API behavior),
// each object is created in its own unit of work. In addition, the response will indicate which objects were created and which ones where not.
// If the parameter is set to true, then all of the objects will be created in the same unit of work. This means that either all of the objects will be created, or none of them will.
//
// An example of when useSingleTransaction is required is when creating InvoiceItemAdjustment objects where one is a Credit and one is a Charge
//
// Limitations
// This call has the following limitations:
//
// * All zObjects must be the same type
//
// * Only 50 zObjects can be created at a time
//
// * ZObjects can not be null
//
// * The default WSDL version for Actions is 79.
//
// * The Invoice Settlement feature is not supported. This feature includes Unapplied Payments, Credit and Debit Memo, and Invoice Item Settlement. The Orders feature is also not supported.
func (t *actionsService) Create(ctx context.Context, actionPayload interface{}, useSingleTransaction bool) ([]byte, error) {
authHeader, err := t.authHeaderProvider.AuthHeaders(ctx)
if err != nil {
return nil, responseError{isTemporary: false, message: fmt.Sprintf("error while trying to set auth headers: %v", err)}
}
url := fmt.Sprintf("%s/v1/action/create", t.baseURL)
if useSingleTransaction {
url += "?useSingleTransaction=true"
}
j, err := json.Marshal(actionPayload)
if err != nil {
return nil, responseError{isTemporary: false, message: fmt.Sprintf("error while trying to convert empty interface: %v", err)}
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(j))
if err != nil {
return nil, responseError{isTemporary: false, message: fmt.Sprintf("error while trying to create an HTTP request: %v", err)}
}
req.Header.Add("Authorization", authHeader)
req.Header.Add("Content-Type", "application/json")
if ctx.Value(ContextKeyZuoraEntityIds) != nil {
req.Header.Add("Zuora-Entity-Ids", ctx.Value(ContextKeyZuoraEntityIds).(string))
}
if ctx.Value(ContextKeyZuoraTrackID) != nil {
req.Header.Add("Zuora-Track-Id", ctx.Value(ContextKeyZuoraTrackID).(string))
}
res, err := t.http.Do(req.WithContext(ctx))
if err != nil {
return nil, responseError{isTemporary: false, message: fmt.Sprintf("error while trying to make request: %v", err)}
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if res.StatusCode < 200 || res.StatusCode > 299 {
var isTemporary bool
if http.StatusRequestTimeout == res.StatusCode ||
http.StatusTooManyRequests == res.StatusCode ||
http.StatusInternalServerError == res.StatusCode ||
http.StatusServiceUnavailable == res.StatusCode {
isTemporary = true
}
if err != nil {
return nil, responseError{isTemporary: isTemporary, message: fmt.Sprintf("error while trying to read body response into memory. Response Code: %v - Error: %v", res.StatusCode, err)}
}
return nil, responseError{isTemporary: isTemporary, message: fmt.Sprintf("got an invalid http status. Response Code: %v - Body: %v", res.StatusCode, string(body))}
}
return body, nil
}