Skip to content

Commit 383fc2e

Browse files
committed
Context support for Capmonster
1 parent f70c8e8 commit 383fc2e

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

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
}

0 commit comments

Comments
 (0)