-
Notifications
You must be signed in to change notification settings - Fork 6
/
capsolver.go
50 lines (45 loc) · 1.06 KB
/
capsolver.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
package capsolver_go
import (
"errors"
"time"
)
func (c CapSolver) Solve(task map[string]any) (*capSolverResponse, error) {
//
capRes, err := request(CREATE_TASK_URI, &capSolverRequest{Task: &task, ClientKey: c.getApiKey()})
if err != nil {
return nil, err
}
if capRes.ErrorId == 1 {
return nil, errors.New(capRes.ErrorDescription)
}
if capRes.Status == STATUS_READY {
return capRes, nil
}
for i := 0; i < TASK_TIMEOUT; i++ {
capRes, err = request(GET_TASK_URI, &capSolverRequest{ClientKey: c.getApiKey(), TaskId: capRes.TaskId})
time.Sleep(time.Second * 1)
if err != nil {
return nil, err
}
if capRes.ErrorId == 1 {
return nil, errors.New(capRes.ErrorDescription)
}
if capRes.Status == STATUS_READY {
break
}
}
return capRes, err
}
func (c CapSolver) Balance() (*capSolverResponse, error) {
capRes, err := request(BALANCE_URI, &capSolverRequest{ClientKey: c.getApiKey()})
if err != nil {
return nil, err
}
return capRes, nil
}
func (c *CapSolver) getApiKey() string {
if c.ApiKey != "" {
return c.ApiKey
}
return ApiKey
}