-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathym.go
More file actions
489 lines (430 loc) · 11.6 KB
/
ym.go
File metadata and controls
489 lines (430 loc) · 11.6 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package ym
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"html"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"text/template"
"time"
)
type Config struct {
ConnectTimeout time.Duration
ReadWriteTimeout time.Duration
}
func TimeoutDialer(config *Config) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, config.ConnectTimeout)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(config.ReadWriteTimeout))
return conn, nil
}
}
func NewTimeoutClient(args ...interface{}) *http.Client {
// Default configuration
config := &Config{
ConnectTimeout: 1 * time.Second,
ReadWriteTimeout: 1 * time.Second,
}
// merge the default with user input if there is one
if len(args) == 1 {
timeout := args[0].(time.Duration)
config.ConnectTimeout = timeout
config.ReadWriteTimeout = timeout
}
if len(args) == 2 {
config.ConnectTimeout = args[0].(time.Duration)
config.ReadWriteTimeout = args[1].(time.Duration)
}
return &http.Client{
Transport: &http.Transport{
Dial: TimeoutDialer(config),
},
}
}
var token string
var Verbose bool = false
var credentials Auth
var templatePath string
var reportRoot string
var version = "1.36"
type Auth struct {
Login, Password, Env, url string
}
type Column []string
type Row struct {
Column Column `xml:"COLUMN"`
}
type ReportData []Row
var layoutXml = `<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
{{template "Body" .}}
</env:Body>
</env:Envelope>`
var layoutTemplate *template.Template
func init() {
var err error
layoutTemplate, err = template.New("Layout").Parse(layoutXml)
if err != nil {
panic(err)
}
templatePath = strings.Split(os.Getenv("GOPATH"), ":")[0] + "/src/github.com/KarateCode/ym/"
}
func Session(cred Auth, work func()) {
Open(cred)
defer Close()
work()
}
func Open(cred Auth) error {
credentials = cred
if credentials.Env == "test" {
credentials.url = "https://api-test.yieldmanager.com/api-" + version + "/"
reportRoot = "https://api-test.yieldmanager.com/reports/"
} else {
credentials.url = "https://api.yieldmanager.com/api-" + version + "/"
reportRoot = "https://api.yieldmanager.com/reports/"
}
tmpl, err := template.ParseFiles(templatePath + "templates/contact/login.xml")
if err != nil {
return err
}
buffer := new(bytes.Buffer)
err = tmpl.Execute(buffer, credentials)
if err != nil {
return err
}
req, err := http.NewRequest("POST", credentials.url+"contact.php", buffer)
if err != nil {
return err
}
res, error := http.DefaultClient.Do(req)
if error != nil {
return error
}
type loginStruct struct {
XMLName xml.Name
Body struct {
XMLName xml.Name
Innerxml string "innerxml"
Fault struct {
XMLName xml.Name `xml:"Fault"`
Faultstring string `xml:"faultstring"`
}
LoginResponse struct {
XMLName xml.Name `xml:"loginResponse"`
Token string `xml:"token"`
}
}
}
loginObj := new(loginStruct)
p, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return readErr
}
error = xml.Unmarshal(p, loginObj)
if error != nil {
return error
}
token = loginObj.Body.LoginResponse.Token
if Verbose {
println("\n** Logged in:", credentials.url, " **")
fmt.Printf("%s\n", p)
}
if len(loginObj.Body.Fault.Faultstring) > 0 {
return errors.New(loginObj.Body.Fault.Faultstring)
}
return nil
}
func AssembleTemplate(bodyXml string, data interface{}) *bytes.Buffer {
tmpl, err := layoutTemplate.Clone()
if err != nil {
panic(err)
}
_, err = tmpl.Parse(bodyXml)
if err != nil {
panic(err)
}
buffer := new(bytes.Buffer)
err = tmpl.Execute(buffer, data)
if err != nil {
panic(err)
}
return buffer
}
func checkConnection() {
if token == "" {
panic(errors.New("No valid token. Did you forget to open a session?"))
}
}
type IoData struct {
Response struct {
XMLName xml.Name `xml:"RESPONSE"`
Data struct {
XMLName xml.Name `xml:"DATA"`
Header Row `xml:"HEADER"`
RData ReportData `xml:"ROW"`
}
}
}
func ComplexReport(requestXml string) (*ReportData, *Row, error) {
checkConnection()
type Data struct {
Token string
XmlString string
}
bodyXml := `{{define "Body"}}<n1:requestViaXML env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:n1="urn:ReportService">
<token xsi:type="xsd:string">{{.Token}}</token>
<xml xsi:type="xsd:string">{{.XmlString}}</xml>
</n1:requestViaXML>{{end}}`
buffer := AssembleTemplate(bodyXml, Data{Token: token, XmlString: html.EscapeString(requestXml)})
// io.Copy(os.Stdout, buffer)
// println(credentials.url + "report.php")
req, err := http.NewRequest("POST", credentials.url+"report.php", buffer)
if err != nil {
panic(err)
}
// client := http.DefaultClient
retries := 0
var res *http.Response
for retries < 20 {
client := NewTimeoutClient(500*time.Millisecond, 10*time.Minute)
res, err = client.Do(req)
if err != nil {
fmt.Printf("err: %+v\n", err)
if retries >= 6 {
panic(err)
}
} else {
break
}
// reportUrl, err = Status(requestViaXml.Body.RequestViaXMLResponse.ReportToken)
// if err != nil {
// panic(err)
// // return nil, readErr
// }
// if reportUrl != "" && reportUrl != "https://api-test.yieldmanager.com/reports/" {
// break
// }
// println("reportUrl:", reportUrl)
// println("sleeping ", retries)
time.Sleep(30 * time.Second)
// println("reattempting ", requestViaXml.Body.RequestViaXMLResponse.ReportToken)
retries += 1
}
// io.Copy(os.Stdout, res.Body)
type RequestViaXml struct {
XMLName xml.Name
Body struct {
XMLName xml.Name
Innerxml string "innerxml"
Fault struct {
XMLName xml.Name `xml:"Fault"`
Faultstring string `xml:"faultstring"`
}
RequestViaXMLResponse struct {
XMLName xml.Name `xml:"requestViaXMLResponse"`
Token string `xml:"token"`
ReportToken string `xml:"report_token"`
}
}
}
requestViaXml := new(RequestViaXml)
p, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
panic(readErr)
// return nil, readErr
}
errUnmarshall := xml.Unmarshal(p, requestViaXml)
// if error != nil { return nil, readErr }
if errUnmarshall != nil {
panic(errUnmarshall)
}
if len(requestViaXml.Body.Fault.Faultstring) > 0 {
println(requestViaXml.Body.Fault.Faultstring)
return nil, nil, errors.New(requestViaXml.Body.Fault.Faultstring)
}
// println(requestViaXml.Body.RequestViaXMLResponse.ReportToken)
retries = 0
var reportUrl string
for retries < 20 {
reportUrl, err = Status(requestViaXml.Body.RequestViaXMLResponse.ReportToken)
if err != nil {
// panic(err)
fmt.Printf("err: %+v\n", err)
// return nil, readErr
if strings.Contains(err.Error(), `While beginning to sink the request, an exception occurred. Message: The number of dynamic conversions (advertiser.convs) has exceeded maximum limit of 10000. Please narrow your request filters.' occurred while report system was fulfilling your request.`) {
return nil, nil, err
}
} else if reportUrl != "" && reportUrl != "https://api-test.yieldmanager.com/reports/" {
break
}
println("reportUrl:", reportUrl)
println("sleeping ", retries)
time.Sleep(30 * time.Second)
println("reattempting status", requestViaXml.Body.RequestViaXMLResponse.ReportToken)
retries += 1
}
downloadReq, downloadErr := http.NewRequest("GET", reportUrl, nil)
if downloadErr != nil {
// return nil, downloadErr
panic(downloadErr)
}
retries = 0
ioData := new(IoData)
for retries < 6 {
client := NewTimeoutClient(500*time.Millisecond, 10*time.Minute)
// client := http.DefaultClient
downloadRes, errGet := client.Do(downloadReq)
if errGet != nil {
if retries > 6 {
panic(errGet)
}
fmt.Printf("errGet: %+v\n", errGet)
time.Sleep(15 * time.Second)
println("reattempting download ", requestViaXml.Body.RequestViaXMLResponse.ReportToken)
retries += 1
} else {
defer downloadRes.Body.Close()
// io.Copy(os.Stdout, downloadRes.Body)
// return nil, nil
p, readErr = ioutil.ReadAll(downloadRes.Body)
if readErr != nil {
// return nil, readErr
panic(readErr)
}
// var xmlBody string = string(p)
// if bytes.IndexRune(p, '\u0005') > -1 {
// println(len(p))
// println("found match")
// println(bytes.IndexRune(p, '\u0005'))
// }
// errUnmarshall = xml.Unmarshal(bytes.Replace(p, '\u0005', []byte("")), ioData)
errUnmarshall = xml.Unmarshal(bytes.Map(mapOutIllChars, p), ioData)
// errUnmarshall = xml.Unmarshal(p, ioData)
if errUnmarshall != nil {
// return nil, errUnmarshall
println("\n", reportUrl, "\n")
println(string(p))
println("sleeping")
println(errUnmarshall.Error())
time.Sleep(15 * time.Second)
println("reattempting")
retries += 1
if retries >= 6 {
// panic(errUnmarshall)
log.Print("Retries exceeded. Skipping ", requestXml)
}
} else {
break
}
}
}
// fmt.Printf("Header: %+v\n", ioData.Response.Data)
return &ioData.Response.Data.RData, &ioData.Response.Data.Header, nil
}
func mapOutIllChars(r rune) rune {
if r == '\u0005' {
return -1
}
return r
}
func Status(reportToken string) (string, error) {
type StatusData struct {
Token string
ReportToken string
}
statusXml := `{{define "Body"}}<n1:status env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:n1="urn:ReportService">
<token xsi:type="xsd:string">{{.Token}}</token>
<report_token xsi:type="xsd:string">{{.ReportToken}}</report_token>
</n1:status>{{end}}`
buffer := AssembleTemplate(statusXml, StatusData{Token: token, ReportToken: reportToken})
// io.Copy(os.Stdout, buffer)
req, err := http.NewRequest("POST", credentials.url+"report.php", buffer)
if err != nil {
println("error creating request")
panic(err)
}
client := NewTimeoutClient(500*time.Millisecond, 10*time.Minute)
// client := http.DefaultClient
res, err := client.Do(req)
if err != nil {
println("error posting adhoc report")
// panic(error)
return "", err
}
// io.Copy(os.Stdout, res.Body)
// return "", nil
type Status struct {
XMLName xml.Name
Body struct {
XMLName xml.Name
Innerxml string "innerxml"
Fault struct {
XMLName xml.Name `xml:"Fault"`
Faultstring string `xml:"faultstring"`
}
StatusResponse struct {
XMLName xml.Name `xml:"statusResponse"`
Token string `xml:"token"`
UrlReport string `xml:"url_report"`
}
}
}
status := new(Status)
p, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return "", readErr
}
err = xml.Unmarshal(p, status)
if err != nil {
return "", err
}
if len(status.Body.Fault.Faultstring) > 0 {
return "", errors.New(status.Body.Fault.Faultstring)
}
return status.Body.StatusResponse.UrlReport, nil
}
func Close() {
ManualClose(token)
}
func ManualClose(manualToken string) {
if credentials.Env == "test" {
credentials.url = "https://api-test.yieldmanager.com/api-" + version + "/"
} else {
credentials.url = "https://api.yieldmanager.com/api-" + version + "/"
}
tmpl, err := template.ParseFiles(templatePath + "templates/contact/logout.xml")
if err != nil {
panic(err)
}
buffer := new(bytes.Buffer)
err = tmpl.Execute(buffer, manualToken)
if err != nil {
panic(err)
}
req, reqErr := http.NewRequest("POST", credentials.url+"contact.php", buffer)
if reqErr != nil {
panic(reqErr)
}
res, resErr := http.DefaultClient.Do(req)
if resErr != nil {
panic(resErr)
}
if Verbose {
println("\n** Logged out **")
io.Copy(os.Stdout, res.Body)
}
token = ""
}