Skip to content

Commit 662ddd6

Browse files
committed
Context support for 2Captcha
1 parent 383fc2e commit 662ddd6

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

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)