package ezcaptcha import ( "bytes" "encoding/json" "errors" "fmt" "io" "net/http" "time" ) var ( ApiURL string = "https://api.ez-captcha.com" SyncApiURL string = "https://sync.ez-captcha.com" CreateTaskEndPoint string = "/createTask" CreateSyncTaskEndpoint string = "/createSyncTask" GetTaskResultEndPoint string = "/getTaskResult" GetBalanceEndPoint string = "/getBalance" ) func NewClient(clientKey, appID string, attempts int) (*Client, error) { if attempts == 0 { attempts = 45 } else if attempts < 0 { return nil, fmt.Errorf("attempts cannot have a negative value") } return &Client{ ClientKey: clientKey, AppID: appID, Attempts: attempts, }, nil } func (c *Client) GetResult(task *CreateTaskResponse) (*SolutionResponse, error) { response := &SolutionResponse{} var err error for i := 0; i < c.Attempts; i++ { time.Sleep(3 * time.Second) response, err = c.getResult(task) if err != nil { return nil, err } if response.ErrorID != 0 { return nil, errors.New(fmt.Sprintf("%s", response.ErrorDescription)) } if response.Status == "ready" { break } } return response, nil } func (c *Client) GetBalance() (int, error) { requestData := GetBalanceRequest{ ClientKey: c.ClientKey, } b, _ := json.Marshal(requestData) resp, err := http.Post(fmt.Sprintf("%s%s", ApiURL, GetBalanceEndPoint), "application/json", bytes.NewReader(b)) if err != nil { return 0, err } defer resp.Body.Close() response := &GetBalanceResponse{} b, _ = io.ReadAll(resp.Body) json.Unmarshal(b, response) if response.ErrorID != 0 { return 0, errors.New(fmt.Sprintf("%s", response.ErrorDescription)) } return response.Balance, nil }