This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
/
apicat.go
178 lines (152 loc) · 4.41 KB
/
apicat.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"os"
"time"
"github.com/jakdept/cliTricks"
"golang.org/x/net/publicsuffix"
)
type config struct {
username string
password string
url string
delay time.Duration
locReq []interface{}
locCur []interface{}
locTotal []interface{}
locInc int
}
func runRequest(c http.Client, b []byte, out io.Writer, opts config) (bool, error) {
var respData interface{}
req, err := http.NewRequest("POST", opts.url, bytes.NewReader(b))
if err != nil {
return false, fmt.Errorf("could not build request - %v", err)
}
if opts.username != "" && opts.password != "" {
req.SetBasicAuth(opts.username, opts.password)
}
resp, err := c.Do(req)
if err != nil {
return false, fmt.Errorf("cannot send to [%s] - %v", req.URL.String(), err)
}
// some cheap handling for the request
if resp.StatusCode != 200 {
return false, fmt.Errorf("got a %d response from the api server - %s", resp.StatusCode, resp.Status)
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, fmt.Errorf("cannot read response - %v", err)
}
err = resp.Body.Close()
if err != nil {
return false, fmt.Errorf("failed to close body - %v", err)
}
_, err = out.Write(respBytes)
if err != nil {
return false, fmt.Errorf("cannot output response - %v", err)
}
// at this point we're just looking to see if the page was the last page
if len(opts.locCur) <= 0 || len(opts.locTotal) <= 0 {
return true, nil
}
err = json.Unmarshal(respBytes, &respData)
if err != nil {
return false, fmt.Errorf("response not json - %v", err)
}
curPage, err := cliTricks.GetInt(respData, opts.locCur)
if err != nil {
return false, fmt.Errorf("bad current page - %v", err)
}
totalPage, err := cliTricks.GetInt(respData, opts.locTotal)
if err != nil {
return false, fmt.Errorf("bad total page - %v", err)
}
return curPage >= totalPage, nil
}
func loopRequest(reqData interface{}, out io.Writer, opts config) error {
var done bool
// create client to be used
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return err
}
c := http.Client{Jar: jar}
for !done {
// build then run the request
reqBytes, err := json.Marshal(reqData)
if err != nil {
return fmt.Errorf("could not convert interface to bytes - %v", err)
}
done, err = runRequest(c, reqBytes, out, opts)
if err != nil {
return fmt.Errorf("got bad response from requests - %v", err)
}
// finally increment
if len(opts.locReq) <= 0 {
return nil
}
reqPage, err := cliTricks.GetInt(reqData, opts.locReq)
if err != nil {
return fmt.Errorf("failed to get the current page number before increment - %v", err)
}
reqPage += opts.locInc
cliTricks.SetItem(reqData, opts.locReq, reqPage)
if err != nil {
return fmt.Errorf("failed to set the current page number after increment - %v", err)
}
time.Sleep(opts.delay)
}
return nil
}
func ApiJsonRoundTrip(in io.Reader, out io.Writer, opt config) (err error) {
var requestData interface{}
decoder := json.NewDecoder(in)
for decoder.More() {
err = decoder.Decode(&requestData)
if err != nil {
return err
}
err = loopRequest(requestData, out, opt)
if err != nil {
return err
}
}
if err == io.EOF {
return nil
} else {
return err
}
}
func main() {
locReqString := flag.String("requestedPage", "", "location in the request of the page")
locCurString := flag.String("currentPage", "", "location in the response of the page returned")
locTotalString := flag.String("totalPage", "", "location in the response of the total pages")
username := flag.String("username", "", "username to use for authentication")
password := flag.String("password", "", "username to use for authentication")
url := flag.String("url", "", "url location to direct POSt")
locInc := flag.Int("pageIncrement", 1, "number to increase location request by")
delay := flag.Int("delay", 100, "milliseconds to wait between subsequent requests")
flag.Parse()
opts := config{
username: *username,
password: *password,
url: *url,
locInc: *locInc,
locReq: cliTricks.BreakupArray(*locReqString),
locCur: cliTricks.BreakupArray(*locCurString),
locTotal: cliTricks.BreakupArray(*locTotalString),
delay: time.Duration(*delay) * time.Millisecond,
}
err := ApiJsonRoundTrip(os.Stdin, os.Stdout, opts)
if err != nil {
log.Fatal(err)
}
}