Skip to content

Commit 5167dcf

Browse files
authored
Merge pull request #16 from Matthew17-21/context-support
Context support
2 parents 9db92a8 + d8c26ec commit 5167dcf

8 files changed

+108
-25
lines changed

captchatools-go/anticaptcha.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package captchatoolsgo
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -37,8 +38,13 @@ type anticaptchaResponse struct {
3738
}
3839

3940
func (a Anticaptcha) GetToken(additional ...*AdditionalData) (*CaptchaAnswer, error) {
40-
return a.getCaptchaAnswer(additional...)
41+
return a.getCaptchaAnswer(context.Background(), additional...)
4142
}
43+
44+
func (a Anticaptcha) GetTokenWithContext(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
45+
return a.getCaptchaAnswer(ctx, additional...)
46+
}
47+
4248
func (a Anticaptcha) GetBalance() (float32, error) {
4349
return a.getBalance()
4450
}
@@ -73,7 +79,7 @@ func (a Anticaptcha) getID(data *AdditionalData) (int, error) {
7379
}
7480

7581
// This method gets the captcha token from the Capmonster API
76-
func (a Anticaptcha) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAnswer, error) {
82+
func (a Anticaptcha) getCaptchaAnswer(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
7783
var data *AdditionalData = nil
7884
if len(additional) > 0 {
7985
data = additional[0]
@@ -92,8 +98,13 @@ func (a Anticaptcha) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAn
9298
})
9399
response := &anticaptchaResponse{}
94100
for i := 0; i < 100; i++ {
95-
resp, err := http.Post("https://api.anti-captcha.com/getTaskResult", "application/json", bytes.NewBuffer([]byte(payload)))
101+
reqToMake, _ := http.NewRequestWithContext(ctx, "POST", "https://api.anti-captcha.com/getTaskResult", bytes.NewBufferString(string(payload)))
102+
reqToMake.Header.Add("Content-Type", "application/json")
103+
resp, err := makeRequest(reqToMake)
96104
if err != nil {
105+
if errors.Is(err, context.DeadlineExceeded) {
106+
return nil, fmt.Errorf("getCaptchaAnswer error: %w", err)
107+
}
97108
time.Sleep(3 * time.Second)
98109
continue
99110
}

captchatools-go/anticaptcha_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package captchatoolsgo
22

33
import (
4+
"context"
45
"os"
56
"testing"
67

@@ -135,7 +136,7 @@ func TestGetAnticapV2(t *testing.T) {
135136
for _, c := range configs {
136137
t.Run(c.Name, func(t *testing.T) {
137138
a := &Anticaptcha{c.Config}
138-
_, err := a.getCaptchaAnswer()
139+
_, err := a.getCaptchaAnswer(context.Background())
139140
if err != nil {
140141
if !c.ExpectError {
141142
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
@@ -199,7 +200,7 @@ func TestAnticaptchaGetV2Additional(t *testing.T) {
199200
for _, c := range configs {
200201
t.Run(c.Name, func(t *testing.T) {
201202
a := &Anticaptcha{c.Config}
202-
_, err := a.getCaptchaAnswer(c.AdditionalData)
203+
_, err := a.getCaptchaAnswer(context.Background(), c.AdditionalData)
203204
if err != nil && !c.ExpectError {
204205
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
205206
}
@@ -228,7 +229,7 @@ func TestGetAnticapV3(t *testing.T) {
228229
for _, c := range configs {
229230
t.Run(c.Name, func(t *testing.T) {
230231
a := &Anticaptcha{c.Config}
231-
_, err := a.getCaptchaAnswer()
232+
_, err := a.getCaptchaAnswer(context.Background())
232233
if err != nil && !c.ExpectError {
233234
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
234235
}
@@ -259,7 +260,7 @@ func Test2AntiCaptchaGetImage(t *testing.T) {
259260
for _, c := range configs {
260261
t.Run(c.Name, func(t *testing.T) {
261262
a := &Anticaptcha{c.Config}
262-
answer, err := a.getCaptchaAnswer(&AdditionalData{B64Img: c.Image})
263+
answer, err := a.getCaptchaAnswer(context.Background(), &AdditionalData{B64Img: c.Image})
263264
if err != nil && !c.ExpectError {
264265
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
265266
}

captchatools-go/capmonster.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package captchatoolsgo
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -68,8 +69,13 @@ type capmonsterBalanceResponse struct {
6869
}
6970

7071
func (c Capmonster) GetToken(additional ...*AdditionalData) (*CaptchaAnswer, error) {
71-
return c.getCaptchaAnswer(additional...)
72+
return c.getCaptchaAnswer(context.Background(), additional...)
7273
}
74+
75+
func (c Capmonster) GetTokenWithContext(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
76+
return c.getCaptchaAnswer(ctx, additional...)
77+
}
78+
7379
func (c Capmonster) GetBalance() (float32, error) {
7480
return c.getBalance()
7581
}
@@ -104,7 +110,7 @@ func (c Capmonster) getID(data *AdditionalData) (int, error) {
104110
}
105111

106112
// This method gets the captcha token from the Capmonster API
107-
func (c Capmonster) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAnswer, error) {
113+
func (c Capmonster) getCaptchaAnswer(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
108114
var data *AdditionalData = nil
109115
if len(additional) > 0 {
110116
data = additional[0]
@@ -123,8 +129,13 @@ func (c Capmonster) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAns
123129
})
124130
response := &capmonsterTokenResponse{}
125131
for i := 0; i < 100; i++ {
126-
resp, err := http.Post("https://api.capmonster.cloud/getTaskResult", "application/json", bytes.NewBuffer([]byte(payload)))
132+
req, _ := http.NewRequestWithContext(ctx, "POST", "https://api.capmonster.cloud/getTaskResult", bytes.NewBufferString(string(payload)))
133+
req.Header.Add("Content-Type", "application/json")
134+
resp, err := makeRequest(req)
127135
if err != nil {
136+
if errors.Is(err, context.DeadlineExceeded) {
137+
return nil, fmt.Errorf("getCaptchaAnswer error: %w", err)
138+
}
128139
time.Sleep(3 * time.Second)
129140
continue
130141
}

captchatools-go/capmonster_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package captchatoolsgo
22

33
import (
4+
"context"
45
"os"
56
"testing"
67

@@ -132,7 +133,7 @@ func TestCapmonsterGetV2(t *testing.T) {
132133
for _, c := range configs {
133134
t.Run(c.Name, func(t *testing.T) {
134135
a := &Capmonster{c.Config}
135-
_, err := a.getCaptchaAnswer()
136+
_, err := a.getCaptchaAnswer(context.Background())
136137
if err != nil && !c.ExpectError {
137138
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
138139
}
@@ -193,7 +194,7 @@ func TestCapmonsterGetV2Additional(t *testing.T) {
193194
for _, c := range configs {
194195
t.Run(c.Name, func(t *testing.T) {
195196
a := &Capmonster{c.Config}
196-
_, err := a.getCaptchaAnswer(c.AdditionalData)
197+
_, err := a.getCaptchaAnswer(context.Background(), c.AdditionalData)
197198
if err != nil && !c.ExpectError {
198199
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
199200
}
@@ -221,7 +222,7 @@ func TestCapmonsterGetV3(t *testing.T) {
221222
for _, c := range configs {
222223
t.Run(c.Name, func(t *testing.T) {
223224
a := &Capmonster{c.Config}
224-
_, err := a.getCaptchaAnswer()
225+
_, err := a.getCaptchaAnswer(context.Background())
225226
if err != nil && !c.ExpectError {
226227
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
227228
}
@@ -251,7 +252,7 @@ func Test2CapmonsterGetImage(t *testing.T) {
251252
for _, c := range configs {
252253
t.Run(c.Name, func(t *testing.T) {
253254
a := &Capmonster{c.Config}
254-
answer, err := a.getCaptchaAnswer(&AdditionalData{B64Img: c.Image})
255+
answer, err := a.getCaptchaAnswer(context.Background(), &AdditionalData{B64Img: c.Image})
255256
if err != nil && !c.ExpectError {
256257
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
257258
}

captchatools-go/capsolver.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package captchatoolsgo
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
7+
"errors"
68
"fmt"
79
"io"
810
"net/http"
@@ -51,7 +53,11 @@ func (c Capsolver) getBalance() (float32, error) {
5153
}
5254

5355
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...)
5561
}
5662

5763
// Method to get Queue ID from the API.
@@ -87,7 +93,7 @@ func (c Capsolver) getID(data *AdditionalData) (string, error) {
8793
return "", ErrMaxAttempts
8894
}
8995

90-
func (c Capsolver) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAnswer, error) {
96+
func (c Capsolver) getCaptchaAnswer(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
9197
var data *AdditionalData = nil
9298
if len(additional) > 0 {
9399
data = additional[0]
@@ -110,8 +116,13 @@ func (c Capsolver) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAnsw
110116
})
111117
response := &capmonsterTokenResponse{}
112118
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)
114122
if err != nil {
123+
if errors.Is(err, context.DeadlineExceeded) {
124+
return nil, fmt.Errorf("getCaptchaAnswer error: %w", err)
125+
}
115126
time.Sleep(3 * time.Second)
116127
continue
117128
}

captchatools-go/harvester.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package captchatoolsgo
22

3+
import (
4+
"context"
5+
"net/http"
6+
)
7+
38
/*
49
NewHarvester returns a captcha harvester based on the info given
510
by the caller. An error is returned if there is no proper
@@ -26,7 +31,8 @@ https://github.com/Matthew17-21/Captcha-Tools
2631
// Interface that will allow us to interact with the methods from the
2732
// individual structs
2833
type Harvester interface {
29-
GetToken(additional ...*AdditionalData) (*CaptchaAnswer, error) // Function to get a captcha token
34+
GetToken(additional ...*AdditionalData) (*CaptchaAnswer, error) // Function to get a captcha token
35+
GetTokenWithContext(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) // Function to get a captcha token
3036
GetBalance() (float32, error)
3137
}
3238

@@ -75,3 +81,10 @@ func NewHarvester(solving_site site, config *Config) (Harvester, error) {
7581
}
7682
return h, nil
7783
}
84+
85+
// makeRequest is a wrapper function to httpClient.Do
86+
func makeRequest(req *http.Request) (*http.Response, error) {
87+
c := http.Client{}
88+
defer c.CloseIdleConnections()
89+
return c.Do(req)
90+
}

captchatools-go/twocaptcha.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package captchatoolsgo
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -43,8 +44,13 @@ type twocaptchaResponse struct {
4344
}
4445

4546
func (t Twocaptcha) GetToken(additional ...*AdditionalData) (*CaptchaAnswer, error) {
46-
return t.getCaptchaAnswer(additional...)
47+
return t.getCaptchaAnswer(context.Background(), additional...)
4748
}
49+
50+
func (t Twocaptcha) GetTokenWithContext(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
51+
return t.getCaptchaAnswer(ctx, additional...)
52+
}
53+
4854
func (t Twocaptcha) GetBalance() (float32, error) {
4955
return t.getBalance()
5056
}
@@ -79,7 +85,7 @@ func (t Twocaptcha) getID(data *AdditionalData) (string, error) {
7985
}
8086

8187
// This method gets the captcha token from the Capmonster API
82-
func (t Twocaptcha) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAnswer, error) {
88+
func (t Twocaptcha) getCaptchaAnswer(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
8389
var data *AdditionalData = nil
8490
if len(additional) > 0 {
8591
data = additional[0]
@@ -99,8 +105,12 @@ func (t Twocaptcha) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAns
99105
queueID,
100106
)
101107
for i := 0; i < 100; i++ {
102-
resp, err := http.Get(urlToAnswer)
108+
req, _ := http.NewRequestWithContext(ctx, "GET", urlToAnswer, nil)
109+
resp, err := makeRequest(req)
103110
if err != nil {
111+
if errors.Is(err, context.DeadlineExceeded) {
112+
return nil, fmt.Errorf("getCaptchaAnswer error: %w", err)
113+
}
104114
time.Sleep(3 * time.Second)
105115
continue
106116
}

captchatools-go/twocaptcha_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package captchatoolsgo
22

33
import (
4+
"context"
5+
"errors"
46
"fmt"
7+
"net/http"
58
"os"
69
"testing"
10+
"time"
711

812
"github.com/joho/godotenv"
913
)
@@ -132,7 +136,7 @@ func Test2CaptchaGetV2(t *testing.T) {
132136
for _, c := range configs {
133137
t.Run(c.Name, func(t *testing.T) {
134138
a := &Twocaptcha{c.Config}
135-
_, err := a.getCaptchaAnswer()
139+
_, err := a.getCaptchaAnswer(context.Background())
136140
if err != nil && !c.ExpectError {
137141
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
138142
}
@@ -187,7 +191,7 @@ func Test2CaptchaGetV2Additional(t *testing.T) {
187191
for _, c := range configs {
188192
t.Run(c.Name, func(t *testing.T) {
189193
a := &Twocaptcha{c.Config}
190-
_, err := a.getCaptchaAnswer(c.AdditionalData)
194+
_, err := a.getCaptchaAnswer(context.Background(), c.AdditionalData)
191195
if err != nil && !c.ExpectError {
192196
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
193197
}
@@ -215,7 +219,7 @@ func Test2CaptchaGetV3(t *testing.T) {
215219
for _, c := range configs {
216220
t.Run(c.Name, func(t *testing.T) {
217221
a := &Twocaptcha{c.Config}
218-
_, err := a.getCaptchaAnswer()
222+
_, err := a.getCaptchaAnswer(context.Background())
219223
if err != nil && !c.ExpectError {
220224
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
221225
}
@@ -244,7 +248,7 @@ func Test2CaptchaGetImage(t *testing.T) {
244248
for _, c := range configs {
245249
t.Run(c.Name, func(t *testing.T) {
246250
a := &Twocaptcha{c.Config}
247-
answer, err := a.getCaptchaAnswer(&AdditionalData{B64Img: c.Image})
251+
answer, err := a.getCaptchaAnswer(context.Background(), &AdditionalData{B64Img: c.Image})
248252
if err != nil && !c.ExpectError {
249253
t.Fatalf(`getID() Error: %v , wanted: %v`, err, nil)
250254
}
@@ -255,3 +259,24 @@ func Test2CaptchaGetImage(t *testing.T) {
255259
})
256260
}
257261
}
262+
263+
// go test -v -run ^TestSometing$ github.com/Matthew17-21/Captcha-Tools/captchatools-go
264+
func TestSometing(t *testing.T) {
265+
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
266+
defer cancel()
267+
268+
reqToMake, _ := http.NewRequestWithContext(ctx, "GET", "https://httpbin.org/delay/10", nil)
269+
c := http.Client{
270+
Timeout: 5 * time.Second,
271+
}
272+
273+
_, err := c.Do(reqToMake)
274+
if err != nil {
275+
fmt.Println(errors.Is(err, context.Canceled))
276+
fmt.Println(errors.Is(err, context.DeadlineExceeded))
277+
fmt.Printf("%v || %T\n", err, err)
278+
return
279+
}
280+
t.Fatal("Not supposed to be any error")
281+
282+
}

0 commit comments

Comments
 (0)