@@ -2,7 +2,9 @@ package captchatoolsgo
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"encoding/json"
7
+ "errors"
6
8
"fmt"
7
9
"io"
8
10
"net/http"
@@ -51,7 +53,11 @@ func (c Capsolver) getBalance() (float32, error) {
51
53
}
52
54
53
55
func (c Capsolver ) GetToken (additional ... * AdditionalData ) (* CaptchaAnswer , error ) {
54
- return c .getCaptchaAnswer (additional ... )
56
+ return c .getCaptchaAnswer (context .Background (), additional ... )
57
+ }
58
+
59
+ func (c Capsolver ) GetTokenWithContext (ctx context.Context , additional ... * AdditionalData ) (* CaptchaAnswer , error ) {
60
+ return c .getCaptchaAnswer (ctx , additional ... )
55
61
}
56
62
57
63
// Method to get Queue ID from the API.
@@ -87,7 +93,7 @@ func (c Capsolver) getID(data *AdditionalData) (string, error) {
87
93
return "" , ErrMaxAttempts
88
94
}
89
95
90
- func (c Capsolver ) getCaptchaAnswer (additional ... * AdditionalData ) (* CaptchaAnswer , error ) {
96
+ func (c Capsolver ) getCaptchaAnswer (ctx context. Context , additional ... * AdditionalData ) (* CaptchaAnswer , error ) {
91
97
var data * AdditionalData = nil
92
98
if len (additional ) > 0 {
93
99
data = additional [0 ]
@@ -110,8 +116,13 @@ func (c Capsolver) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAnsw
110
116
})
111
117
response := & capmonsterTokenResponse {}
112
118
for i := 0 ; i < 50 ; i ++ {
113
- resp , err := http .Post ("https://api.capsolver.com/getTaskResult" , "application/json" , bytes .NewBuffer ([]byte (payload )))
119
+ reqToMake , _ := http .NewRequestWithContext (ctx , "POST" , "https://api.capsolver.com/getTaskResult" , bytes .NewBuffer (payload ))
120
+ reqToMake .Header .Add ("Content-Type" , "application/json" )
121
+ resp , err := makeRequest (reqToMake )
114
122
if err != nil {
123
+ if errors .Is (err , context .DeadlineExceeded ) {
124
+ return nil , fmt .Errorf ("getCaptchaAnswer error: %w" , err )
125
+ }
115
126
time .Sleep (3 * time .Second )
116
127
continue
117
128
}
0 commit comments