-
Notifications
You must be signed in to change notification settings - Fork 21
/
http.go
73 lines (65 loc) · 1.24 KB
/
http.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
package pdd
import (
"github.com/parnurzeal/gorequest"
"sync"
"fmt"
"log"
"encoding/json"
)
const EndPoint = "https://gw-api.pinduoduo.com/api/router"
var clients *sync.Pool
func init() {
clients = &sync.Pool{
New: func() interface{} {
return gorequest.New()
},
}
}
func Post(context *Context, query string) (b []byte, err error) {
b, err = post(query)
if err != nil {
times := 0
for times < context.RetryTimes {
b, err = post(query)
if err != nil {
log.Printf("第 %d 次重试失败:%s", times + 1, err)
} else {
return
}
times++
}
}
return
}
func post(query string) (b []byte, err error) {
client := clients.Get().(*gorequest.SuperAgent)
_, b, errors := client.Post(EndPoint).
Type("json").
Query(query).
EndBytes()
// push back
clients.Put(client)
if IsBadPddRequest(b) {
err = new(Error)
json.Unmarshal(b, err)
return
}
if err = getErrorsError(errors); err != nil {
return
}
return
}
func getErrorsError(errors []error) (err error) {
if len(errors) == 0 {
return nil
}
for _, e := range errors {
if e != nil {
err = fmt.Errorf("%s | %s", err, e)
}
}
return
}
func IsBadPddRequest(body []byte) bool {
return len(body) >= 16 && string(body[2:16]) == "error_response"
}