-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscraperapi.go
106 lines (86 loc) · 2.64 KB
/
scraperapi.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
package scraperapi
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// BaseURL is a default base URL used to request the API.
const BaseURL = "http://api.scraperapi.com"
// Client is a main SDK struct which facilitates access to the API.
type Client struct {
HTTPClient *http.Client
BaseURL string
apiKey string
}
// New creates a new Scraper API client.
func New(apiKey string) *Client {
return &Client{
HTTPClient: &http.Client{
Timeout: time.Minute,
},
BaseURL: BaseURL,
apiKey: apiKey,
}
}
// Get performs a GET HTTP request to Scraper API.
func (c *Client) Get(url string, options ...Option) (*http.Response, error) {
return c.makeAPICall("GET", url, nil, options)
}
// Post performs a POST HTTP request to Scraper API.
func (c *Client) Post(url string, body io.Reader, options ...Option) (*http.Response, error) {
return c.makeAPICall("POST", url, body, options)
}
// Put performs a PUT HTTP request to Scraper API.
func (c *Client) Put(url string, body io.Reader, options ...Option) (*http.Response, error) {
return c.makeAPICall("PUT", url, body, options)
}
func (c *Client) makeAPICall(httpMethod, url string, body io.Reader, options []Option) (*http.Response, error) {
req, err := http.NewRequest(httpMethod, c.BaseURL, body)
if err != nil {
return nil, fmt.Errorf("can't create an HTTP request: %s", err)
}
req = SetQueryParam(req, "url", url)
for k := range options {
req = options[k](req)
}
return c.sendRequest(req)
}
// AccountResponse is a response from the account API call.
type AccountResponse struct {
ConcurrentRequests int `json:"concurrentRequests"`
RequestCount int `json:"requestCount"`
FailedRequestCount int `json:"failedRequestCount"`
RequestLimit int `json:"requestLimit"`
ConcurrencyLimit int `json:"concurrencyLimit"`
}
// Account retrieves account usage information.
func (c *Client) Account() (accountResp AccountResponse, err error) {
req, err := http.NewRequest("GET", c.BaseURL+"/account", nil)
if err != nil {
return
}
res, err := c.sendRequest(req)
if err != nil {
return
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&accountResp)
return
}
func (c *Client) sendRequest(req *http.Request) (*http.Response, error) {
req = SetQueryParam(req, "api_key", c.apiKey)
req = SetQueryParam(req, "scraper_sdk", "go")
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode == 429 {
return res, fmt.Errorf("your plan concurrent connection limit is exceeded, slow down your request rate")
}
if res.StatusCode == 403 {
return res, fmt.Errorf("you exceeded your maximum number of monthly requests")
}
return res, nil
}