-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline_item.go
More file actions
176 lines (155 loc) · 4.86 KB
/
line_item.go
File metadata and controls
176 lines (155 loc) · 4.86 KB
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
package ym
import (
"encoding/xml"
"io/ioutil"
"net/http"
// "fmt"
"io"
"os"
"time"
)
type lineItemService struct {
url string
}
var LineItemService = lineItemService{url: `line_item.php`}
type LineItem struct {
Id int `xml:"id"`
Description string `xml:"description"`
Comment string `xml:"comment"`
InsertionOrderId int `xml:"insertion_order_id"`
Active bool `xml:"active"`
TargetProfileId string `xml:"target_profile_id"` // can't be an int, since the api sometimes hands back a nil and the parser panics.
}
type GetByIO struct {
XMLName xml.Name
Body struct {
XMLName xml.Name
Innerxml string "innerxml"
Fault struct {
XMLName xml.Name `xml:"Fault"`
Faultstring string `xml:"faultstring"`
}
GetByInsertionOrderResponse struct {
XMLName xml.Name `xml:"getByInsertionOrderResponse"`
LineItems struct {
XMLName xml.Name `xml:"line_items"`
Items []LineItem `xml:"item"`
// ReportToken string `xml:"report_token"`
}
}
}
}
func (service *lineItemService) GetByInsertionOrder(insertionOrderId, entriesOnPage, pageNum int) ([]LineItem, int) {
type Data struct {
Token string
InsertionOrderId int
EntriesOnPage int
PageNum int
}
bodyXml := `{{define "Body"}}<n1:getByInsertionOrder env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:n1="urn:LineItemService">
<token xsi:type="xsd:string">{{.Token}}</token>
<insertion_order_id xsi:type="xsd:long">{{.InsertionOrderId}}</insertion_order_id>
<entries_on_page xsi:type="xsd:long">{{.EntriesOnPage}}</entries_on_page>
<page_num xsi:type="xsd:long">{{.PageNum}}</page_num>
</n1:getByInsertionOrder>{{end}}`
buffer := AssembleTemplate(bodyXml, Data{Token: token, InsertionOrderId: insertionOrderId, EntriesOnPage: entriesOnPage, PageNum: pageNum})
retries := 0
getByIO := new(GetByIO)
var res *http.Response
var p []byte
var readErr error
for retries < 6 {
req, err := http.NewRequest("POST", credentials.url+service.url, buffer)
if err != nil {
println("error creating request")
panic(err)
}
res, err = http.DefaultClient.Do(req)
if err != nil {
println("error posting adhoc report")
panic(err)
}
// io.Copy(os.Stdout, res.Body)
// return nil, 0
p, readErr = ioutil.ReadAll(res.Body)
if readErr != nil {
if retries > 6 {
panic(readErr)
}
// return nil, readErr
} else {
break
}
println("sleeping ", retries)
time.Sleep(30 * time.Second)
retries += 1
}
errUnmarshall := xml.Unmarshal(p, getByIO)
// if error != nil { return nil, readErr }
if errUnmarshall != nil {
io.Copy(os.Stdout, res.Body)
panic(errUnmarshall)
}
// fmt.Printf("\nResponse: %v", getByIO)
return getByIO.Body.GetByInsertionOrderResponse.LineItems.Items, len(getByIO.Body.GetByInsertionOrderResponse.LineItems.Items)
}
func (service *lineItemService) GetByBuyer(insertionOrderId, entriesOnPage, pageNum int) ([]LineItem, int) {
type Data struct {
Token string
InsertionOrderId int
EntriesOnPage int
PageNum int
}
bodyXml := `{{define "Body"}}<n1:getByBuyer env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:n1="urn:LineItemService">
<token xsi:type="xsd:string">{{.Token}}</token>
<buyer_id xsi:type="xsd:long">{{.InsertionOrderId}}</buyer_id>
<entries_on_page xsi:type="xsd:long">{{.EntriesOnPage}}</entries_on_page>
<page_num xsi:type="xsd:long">{{.PageNum}}</page_num>
</n1:getByBuyer>{{end}}`
buffer := AssembleTemplate(bodyXml, Data{Token: token, InsertionOrderId: insertionOrderId, EntriesOnPage: entriesOnPage, PageNum: pageNum})
req, err := http.NewRequest("POST", credentials.url+service.url, buffer)
if err != nil {
println("error creating request")
panic(err)
}
res, error := http.DefaultClient.Do(req)
if error != nil {
println("error posting adhoc report")
panic(error)
}
// io.Copy(os.Stdout, res.Body)
// return nil, 0
type GetByBuyer struct {
XMLName xml.Name
Body struct {
XMLName xml.Name
Innerxml string "innerxml"
Fault struct {
XMLName xml.Name `xml:"Fault"`
Faultstring string `xml:"faultstring"`
}
GetByInsertionOrderResponse struct {
XMLName xml.Name `xml:"getByBuyerResponse"`
LineItems struct {
XMLName xml.Name `xml:"line_items"`
Items []LineItem `xml:"item"`
// ReportToken string `xml:"report_token"`
}
}
}
}
getByBuyer := new(GetByBuyer)
p, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
panic(readErr)
// return nil, readErr
}
errUnmarshall := xml.Unmarshal(p, getByBuyer)
// if error != nil { return nil, readErr }
if errUnmarshall != nil {
io.Copy(os.Stdout, res.Body)
panic(errUnmarshall)
}
// fmt.Printf("\nResponse: %v", getByIO)
return getByBuyer.Body.GetByInsertionOrderResponse.LineItems.Items, len(getByBuyer.Body.GetByInsertionOrderResponse.LineItems.Items)
}