-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.go
113 lines (95 loc) · 2.71 KB
/
methods.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
package captcha
import (
"encoding/json"
"net/http"
"net/url"
"errors"
"bytes"
"fmt"
)
type CaptchaServiceClient struct {
ApiKey string
ApiAdress *url.URL
HttpClient *http.Client
}
// Preparation client to use package
func Client(secure bool, provider string, apikey string) (*CaptchaServiceClient, error){
var(
parseURL *url.URL
err error
)
switch secure{
case false:
parseURL, err = url.Parse(fmt.Sprintf("http://%s/", provider))
default:
parseURL, err = url.Parse(fmt.Sprintf("https://%s/", provider))
}
if err != nil{
return nil, err
}
return &CaptchaServiceClient{
ApiKey: apikey,
ApiAdress: parseURL,
HttpClient: http.DefaultClient,
}, nil
}
// A method create job and return the ID
func (c *CaptchaServiceClient) CreatTask(data map[string]interface{}) (int, error){
payload, err := json.Marshal(data)
if err != nil {
return 0, err
}
// Formalize the POST request
req, err := http.NewRequest("POST", c.ApiAdress.ResolveReference(&url.URL{Path: "/createTask"}).String(), bytes.NewBuffer(payload))
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
resp, err := c.HttpClient.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
// Decode the response
var responseBody CreateTaskResponse
json.NewDecoder(resp.Body).Decode(&responseBody)
if responseBody.ErrorID != 0{
return 0, errors.New(fmt.Sprint("Error solving captcha: ", responseBody.ErrorCode))
}
return responseBody.TaskID, nil
}
// A method to check answer for job
func (c *CaptchaServiceClient) CheckResult(taskID int) (*Solution, bool, error){
// Preparation form data
data := map[string]interface{}{
"clientKey": c.ApiKey,
"taskId": taskID,
}
payload, err := json.Marshal(data)
if err != nil {
return nil, true, errors.New(fmt.Sprint("Error solving captcha: ", err))
}
req, err := http.NewRequest("POST", c.ApiAdress.ResolveReference(&url.URL{Path: "/getTaskResult"}).String(), bytes.NewBuffer(payload))
if err != nil {
return nil, true, errors.New(fmt.Sprint("Error solving captcha: ", err))
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, true, errors.New(fmt.Sprint("Error solving captcha: ", err))
}
defer resp.Body.Close()
// Decode the response
var responseBody GetTaskResponse
json.NewDecoder(resp.Body).Decode(&responseBody)
// Interpret the answer
switch responseBody.Status{
case "processing":
return nil, false, nil
default:
if responseBody.ErrorID != 0{
return nil, true, errors.New(fmt.Sprint("Error solving captcha: ", responseBody.ErrorCode))
}
}
return &responseBody.Solution, true, nil
}