-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
183 lines (147 loc) · 3.96 KB
/
client.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
package rucaptcha
import (
"bytes"
"context"
"encoding/json"
"mime/multipart"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"github.com/jfk9w-go/based"
"github.com/pkg/errors"
)
const baseURL = "https://rucaptcha.com"
type Config struct {
Key string `url:"key" validate:"required"`
Pingback string `url:"pingback,omitempty"`
SoftID int `url:"soft_id,omitempty"`
}
type ClientParams struct {
Config Config `validate:"required"`
Clock based.Clock `validate:"required_with=Config.Pingback"`
Transport http.RoundTripper
}
func NewClient(params ClientParams) (*Client, error) {
if err := based.Validate(params); err != nil {
return nil, err
}
options, err := query.Values(params.Config)
if err != nil {
return nil, errors.Wrap(err, "encode options")
}
options.Set("json", "1")
client := &Client{
httpClient: &http.Client{
Transport: params.Transport,
},
options: options,
}
if params.Config.Pingback == "" {
client.answerer = &answerPoller{client}
} else {
client.answerer = newAsyncListener(params.Clock)
}
return client, nil
}
type Client struct {
httpClient *http.Client
answerer answerer
options url.Values
}
func (c *Client) HTTPHandler() http.Handler {
if handler, ok := c.answerer.(http.Handler); ok {
return handler
}
return nil
}
func (c *Client) Solve(ctx context.Context, in SolveIn) (*SolveOut, error) {
if err := based.Validate(in); err != nil {
return nil, err
}
values, err := query.Values(in)
if err != nil {
return nil, errors.Wrap(err, "encode solve values")
}
values.Set("method", in.Method())
id, err := c.execute(ctx, "/in.php", values)
if err != nil {
return nil, errors.Wrap(err, "send solve request")
}
answer, err := c.answerer.answer(ctx, id)
if err != nil {
return nil, errors.Wrap(err, "get answer")
}
return &SolveOut{
ID: id,
Answer: answer,
}, nil
}
func (c *Client) Report(ctx context.Context, id string, ok bool) error {
in := &resReportIn{
ID: id,
ok: ok,
}
_, err := c.res(ctx, in)
return err
}
func (c *Client) res(ctx context.Context, in resIn) (string, error) {
values, err := query.Values(in)
if err != nil {
return "", errors.Wrap(err, "encode solve values")
}
values.Set("action", in.action())
result, err := c.execute(ctx, "/res.php", values)
if err != nil {
return "", errors.Wrap(err, "send res request")
}
return result, nil
}
func (c *Client) execute(ctx context.Context, path string, query url.Values) (string, error) {
var reqBody bytes.Buffer
multipartWriter := multipart.NewWriter(&reqBody)
for _, params := range []url.Values{c.options, query} {
for key, values := range params {
for _, value := range values {
if err := multipartWriter.WriteField(key, value); err != nil {
_ = multipartWriter.Close()
return "", errors.Wrapf(err, "write '%s' to request body", key)
}
}
}
}
if err := multipartWriter.Close(); err != nil {
return "", errors.Wrap(err, "close writer")
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, baseURL+path, &reqBody)
if err != nil {
return "", errors.Wrap(err, "create request")
}
httpReq.Header.Set("Content-Type", multipartWriter.FormDataContentType())
httpResp, err := c.httpClient.Do(httpReq)
if err != nil {
return "", errors.Wrap(err, "execute request")
}
if httpResp.StatusCode != http.StatusOK {
return "", errors.Errorf(httpResp.Status)
}
if httpResp.Body == nil {
return "", errors.New("empty response body")
}
defer httpResp.Body.Close()
var respBody struct {
Status *int `json:"status"`
Request string `json:"request"`
ErrorText string `json:"error_text"`
}
if err := json.NewDecoder(httpResp.Body).Decode(&respBody); err != nil {
return "", errors.Wrap(err, "read response json")
}
switch {
case respBody.Status == nil:
return "", errors.New("empty status")
case *respBody.Status == 0:
return "", &Error{Code: respBody.Request, Text: respBody.ErrorText}
default:
return respBody.Request, nil
}
}