This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
147 lines (123 loc) · 3.05 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package scrapeninja
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"sort"
"time"
)
var (
BaseUrl = "https://scrapeninja.p.rapidapi.com"
ContentType = "application/json"
RapidAPIHost = "scrapeninja.p.rapidapi.com"
Timeout time.Duration = 30
)
const (
ProxyDefault string = "default"
ProxyUS string = "us"
ProxyEU string = "eu"
ProxyEU4G string = "4g-eu"
ProxyDE string = "de"
ProxyFR string = "fr"
ProxyBR string = "br"
)
type ScrapeRequest struct {
Url string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers,omitempty"`
Data string `json:"body,omitempty"`
Proxy string `json:"geo,omitempty"`
}
func (r *ScrapeRequest) MarshalJSON() ([]byte, error) {
keys := make([]string, 0)
for header := range r.Headers {
keys = append(keys, header)
}
sort.Strings(keys)
headers := make([]string, 0)
for _, header := range keys {
headers = append(headers, fmt.Sprintf("%s: %s", header, r.Headers[header]))
}
type ScrapeRequestAlias ScrapeRequest
return json.Marshal(&struct {
*ScrapeRequestAlias
Headers []string `json:"headers"`
}{
ScrapeRequestAlias: (*ScrapeRequestAlias)(r),
Headers: headers,
})
}
type ScrapeResponse struct {
Info struct {
Version string `json:"version"`
StatusCode int `json:"statusCode"`
StatusMessage string `json:"statusMessage"`
Headers map[string]string `json:"headers"`
} `json:"info"`
Body string `json:"body"`
}
type Client struct {
httpClient *http.Client
apiKey string
}
func (c *Client) newRequest(method string, path string, body io.Reader) (*http.Request, error) {
u, _ := url.Parse(BaseUrl)
u.Path = path
request, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", ContentType)
request.Header.Set("X-RapidAPI-Host", RapidAPIHost)
request.Header.Set("X-RapidAPI-Key", c.apiKey)
return request, err
}
func (c *Client) Scrape(request *ScrapeRequest) (*ScrapeResponse, error) {
jsonRequest, err := json.Marshal(request)
if err != nil {
return nil, err
}
scrapeRequest, err := c.newRequest(
http.MethodPost,
"/scrape",
bytes.NewBuffer(jsonRequest),
)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(scrapeRequest)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Print(err)
}
}(resp.Body)
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, errors.New(string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var scrapeResponse ScrapeResponse
err = json.Unmarshal(body, &scrapeResponse)
if err != nil {
return nil, err
}
return &scrapeResponse, nil
}
func New(apiKey string) Client {
return Client{
apiKey: apiKey,
httpClient: &http.Client{Timeout: time.Second * Timeout},
}
}