-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
88 lines (76 loc) · 2.25 KB
/
options.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
package scraperapi
import (
"context"
"net/http"
)
// Option is a function that modifies the request.
type Option func(r *http.Request) *http.Request
// WithContext adds context to the request.
func WithContext(ctx context.Context) Option {
return func(req *http.Request) *http.Request {
return req.WithContext(ctx)
}
}
// WithRenderJS makes the request fetch pages using a headless browser.
func WithRenderJS() Option {
return func(req *http.Request) *http.Request {
return SetQueryParam(req, "render", "true")
}
}
// WithHeader instructs Scraper API to pass a provided header.
// TODO: Refactoring to only accept a single header.
func WithHeader(key, value string) Option {
return func(req *http.Request) *http.Request {
req.Header.Set(key, value)
return SetQueryParam(req, "keep_headers", "true")
}
}
// WithSessionNumber attaches a session number to a request.
func WithSessionNumber(num string) Option {
return func(req *http.Request) *http.Request {
return SetQueryParam(req, "session_number", num)
}
}
// Available country codes.
const (
CountryCodeUS = "us"
CountryCodeCanada = "ca"
CountryCodeUK = "uk"
CountryCodeGermany = "de"
CountryCodeFrance = "fr"
CountryCodeSpain = "es"
CountryCodeBrazil = "br"
CountryCodeMexico = "mx"
CountryCodeIndia = "in"
CountryCodeJapan = "jp"
CountryCodeChina = "cn"
CountryCodeAustralia = "au"
)
// WithCountryCode ensures your requests come from the specified location.
func WithCountryCode(countyCode string) Option {
return func(req *http.Request) *http.Request {
return SetQueryParam(req, "country_code", countyCode)
}
}
// Available device types.
const (
DeviceTypeMobile = "mobile"
)
// WithDeviceType sets device_type to the specified value.
func WithDeviceType(deviceType string) Option {
return func(req *http.Request) *http.Request {
return SetQueryParam(req, "device_type", deviceType)
}
}
// WithAutoParse adds autoparse=true to the request.
func WithAutoParse() Option {
return func(req *http.Request) *http.Request {
return SetQueryParam(req, "autoparse", "true")
}
}
// WithPremium adds premium=true to the request.
func WithPremium() Option {
return func(req *http.Request) *http.Request {
return SetQueryParam(req, "premium", "true")
}
}