ez-captcha-client/requests.go

74 lines
1.8 KiB
Go
Raw Normal View History

2024-10-12 00:32:31 +00:00
package ezcaptcha
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
func (c *Client) getResult(task *CreateTaskResponse) (*SolutionResponse, error) {
requestData := SolutionRequest{
ClientKey: c.ClientKey,
TaskID: task.TaskID,
}
b, _ := json.Marshal(requestData)
resp, err := http.Post(fmt.Sprintf("%s%s", ApiURL, GetTaskResultEndPoint), "application/json", bytes.NewReader(b))
if err != nil {
return nil, err
}
response := &SolutionResponse{}
b, _ = io.ReadAll(resp.Body)
json.Unmarshal(b, response)
defer resp.Body.Close()
return response, nil
}
func (c *Client) createTask(task interface{}) (*CreateTaskResponse, error) {
requestData := &TaskRequest{
Client: *c,
Task: task,
}
b, _ := json.Marshal(requestData)
resp, err := http.Post(fmt.Sprintf("%s%s", ApiURL, CreateTaskEndPoint), "application/json", bytes.NewReader(b))
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, _ = io.ReadAll(resp.Body)
response := &CreateTaskResponse{}
err = json.Unmarshal(b, response)
if err != nil {
return nil, err
}
if response.ErrorID != 0 {
return nil, fmt.Errorf(response.ErrorDescription)
}
return response, nil
}
func (c *Client) solveAkamai(task interface{}) (*AkamaiResponse, error) {
requestData := &TaskRequest{
Client: *c,
Task: task,
}
b, _ := json.Marshal(requestData)
resp, err := http.Post(fmt.Sprintf("%s%s", SyncApiURL, CreateSyncTaskEndpoint), "application/json", bytes.NewReader(b))
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, _ = io.ReadAll(resp.Body)
response := &AkamaiResponse{}
err = json.Unmarshal(b, response)
if err != nil {
return nil, err
}
if response.ErrorID != 0 {
return nil, errors.New(fmt.Sprintf("%s", response.ErrorDescription))
}
return response, nil
}