screenshotscout 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +14 -0
- data/lib/screenshotscout/client.rb +15 -8
- data/lib/screenshotscout/internal/credentials.rb +34 -1
- data/lib/screenshotscout/version.rb +1 -1
- data/sig/screenshotscout.rbs +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 453a6daf83880b4a4944a622edeef17dc85fc6fb9ead7f88d7cdd5f70bcd12be
|
|
4
|
+
data.tar.gz: ba96a2e79656528cae4fb5de5ab316a95b9866e87683fcae92e1235e1554b101
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a668350fff10e050c92041e25b4dd6cc7c00744d913ed182c15d8f836697a68a13a46a3e39d53ce486f7fd1e72a6c1e1e99790d8b1be22f4841a60e49dbca6fb
|
|
7
|
+
data.tar.gz: 49cc93d3c90d719a4b9f5fcb15db3e09562c9b14eee94c6a3af0c2eebe7ef88c6a978c970e34fde34256defe6605c351f6c82faa3cdc4388e1b21c7bfbbf5881
|
data/README.md
CHANGED
|
@@ -144,6 +144,20 @@ If you pass either key, the client reads neither environment variable. If you pa
|
|
|
144
144
|
example because the variable holding it is `nil`, the client reads both environment variables
|
|
145
145
|
instead.
|
|
146
146
|
|
|
147
|
+
## Send requests to another server
|
|
148
|
+
|
|
149
|
+
To send requests to another server, such as your own proxy or a mock server, pass its base URL, or
|
|
150
|
+
set it as the `SCREENSHOTSCOUT_BASE_URL` environment variable:
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
require "screenshotscout"
|
|
154
|
+
|
|
155
|
+
client = ScreenshotScout::Client.new(base_url: "https://proxy.example.com") # Reads SCREENSHOTSCOUT_ACCESS_KEY
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The client sends captures to the base URL plus `/v1/capture`, and `build_capture_url` builds URLs on
|
|
159
|
+
it. A base URL you pass wins over the variable.
|
|
160
|
+
|
|
147
161
|
## Capture options
|
|
148
162
|
|
|
149
163
|
The target URL is the first argument to `capture`. Use snake_case keywords to customize the
|
|
@@ -5,20 +5,25 @@ require "openssl"
|
|
|
5
5
|
module ScreenshotScout
|
|
6
6
|
# Reusable blocking client for the inline Screenshot Scout capture operation.
|
|
7
7
|
class Client
|
|
8
|
-
CAPTURE_ENDPOINT = "https://api.screenshotscout.com/v1/capture"
|
|
9
8
|
ACCESS_KEY_PATTERN = %r{\A[A-Za-z0-9\-._~+/]+=*\z}
|
|
10
9
|
|
|
11
10
|
# Creates a client. When no key is passed, the client reads SCREENSHOTSCOUT_ACCESS_KEY and, if it's set,
|
|
12
|
-
# SCREENSHOTSCOUT_SECRET_KEY. Passing either key means neither variable is read.
|
|
13
|
-
|
|
11
|
+
# SCREENSHOTSCOUT_SECRET_KEY. Passing either key means neither variable is read. Pass base_url to send
|
|
12
|
+
# requests to another server, such as your own proxy or a mock server: captures go to that URL plus
|
|
13
|
+
# /v1/capture, and build_capture_url builds URLs on it. When no base URL is passed, the client reads
|
|
14
|
+
# SCREENSHOTSCOUT_BASE_URL, and without it uses the Screenshot Scout API.
|
|
15
|
+
def initialize(access_key: nil, secret_key: nil, base_url: nil, transport: nil)
|
|
14
16
|
credentials = Internal::Credentials.resolve(access_key, secret_key, environment: ENV)
|
|
15
17
|
validate_access_key(credentials.access_key)
|
|
16
18
|
validate_secret_key(credentials.secret_key)
|
|
17
19
|
validate_transport(transport)
|
|
20
|
+
endpoint = Internal::Credentials.resolve_base_url(base_url, environment: ENV)
|
|
18
21
|
|
|
19
22
|
@access_key = credentials.access_key.dup.freeze
|
|
20
23
|
@secret_key = credentials.secret_key&.dup&.freeze
|
|
21
24
|
@secret_key_from_environment = credentials.from_environment && !@secret_key.nil?
|
|
25
|
+
@capture_endpoint = endpoint.capture_endpoint
|
|
26
|
+
@base_url_from_environment = endpoint.from_environment
|
|
22
27
|
@transport = transport || NetHttpTransport.new
|
|
23
28
|
end
|
|
24
29
|
|
|
@@ -46,7 +51,7 @@ module ScreenshotScout
|
|
|
46
51
|
signature = create_signature(serialized.pairs)
|
|
47
52
|
pairs = [Internal::WirePair.new(name: "access_key", value: @access_key), *serialized.pairs]
|
|
48
53
|
pairs << Internal::WirePair.new(name: "signature", value: signature) unless signature.nil?
|
|
49
|
-
"#{
|
|
54
|
+
"#{@capture_endpoint}?#{Internal::Serializer.encode_query(pairs)}"
|
|
50
55
|
end
|
|
51
56
|
|
|
52
57
|
# Leaves out the keys, so that printing or logging a client doesn't reveal them.
|
|
@@ -112,13 +117,13 @@ module ScreenshotScout
|
|
|
112
117
|
if method == CaptureHttpMethod::GET
|
|
113
118
|
pairs = serialized.pairs.dup
|
|
114
119
|
pairs << Internal::WirePair.new(name: "signature", value: signature) unless signature.nil?
|
|
115
|
-
url = "#{
|
|
120
|
+
url = "#{@capture_endpoint}?#{Internal::Serializer.encode_query(pairs)}"
|
|
116
121
|
TransportRequest.new(method: method, url: url, headers: headers)
|
|
117
122
|
else
|
|
118
123
|
headers["Content-Type"] = "application/json"
|
|
119
124
|
TransportRequest.new(
|
|
120
125
|
method: method,
|
|
121
|
-
url:
|
|
126
|
+
url: @capture_endpoint,
|
|
122
127
|
headers: headers,
|
|
123
128
|
body: Internal::JsonCodec.encode_wire_object(serialized.body)
|
|
124
129
|
)
|
|
@@ -137,7 +142,9 @@ module ScreenshotScout
|
|
|
137
142
|
rescue Error
|
|
138
143
|
raise
|
|
139
144
|
rescue StandardError => e
|
|
140
|
-
|
|
145
|
+
source = @base_url_from_environment ? " The base URL comes from #{Internal::Credentials::BASE_URL_VARIABLE}." : ""
|
|
146
|
+
raise TransportError,
|
|
147
|
+
"Screenshot Scout request failed before a complete HTTP response body was received.#{source}",
|
|
141
148
|
cause: e
|
|
142
149
|
end
|
|
143
150
|
|
|
@@ -243,6 +250,6 @@ module ScreenshotScout
|
|
|
243
250
|
media_type == "application/json" || media_type.end_with?("+json")
|
|
244
251
|
end
|
|
245
252
|
|
|
246
|
-
private_constant :
|
|
253
|
+
private_constant :ACCESS_KEY_PATTERN
|
|
247
254
|
end
|
|
248
255
|
end
|
|
@@ -2,14 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
module ScreenshotScout
|
|
4
4
|
module Internal
|
|
5
|
-
# Picks the keys a client uses: the
|
|
5
|
+
# Picks the keys and the base URL a client uses: the values passed to it, or else the environment variables.
|
|
6
6
|
module Credentials
|
|
7
7
|
ACCESS_KEY_VARIABLE = "SCREENSHOTSCOUT_ACCESS_KEY"
|
|
8
8
|
SECRET_KEY_VARIABLE = "SCREENSHOTSCOUT_SECRET_KEY"
|
|
9
|
+
BASE_URL_VARIABLE = "SCREENSHOTSCOUT_BASE_URL"
|
|
10
|
+
DEFAULT_BASE_URL = "https://api.screenshotscout.com"
|
|
11
|
+
CAPTURE_PATH = "/v1/capture"
|
|
9
12
|
|
|
10
13
|
# The keys to validate, and whether they were read from the environment.
|
|
11
14
|
Resolved = Data.define(:access_key, :secret_key, :from_environment)
|
|
12
15
|
|
|
16
|
+
# The URL captures are sent to and capture URLs are built on, and whether the base URL was read from the
|
|
17
|
+
# environment.
|
|
18
|
+
ResolvedBaseUrl = Data.define(:capture_endpoint, :from_environment)
|
|
19
|
+
|
|
13
20
|
module_function
|
|
14
21
|
|
|
15
22
|
# Keys passed to the client always win: if either one is passed, neither variable is read. Only when
|
|
@@ -29,6 +36,32 @@ module ScreenshotScout
|
|
|
29
36
|
Resolved.new(access_key: environment_access_key, secret_key: environment_secret_key, from_environment: true)
|
|
30
37
|
end
|
|
31
38
|
|
|
39
|
+
# A base URL passed to the client wins. Without one, the variable is read, whether or not keys were passed,
|
|
40
|
+
# and without that the production API is used. The base URL is used as given: only trailing slashes are
|
|
41
|
+
# removed before the capture path is appended.
|
|
42
|
+
def resolve_base_url(base_url, environment:)
|
|
43
|
+
unless base_url.nil? || base_url.is_a?(String)
|
|
44
|
+
raise ConfigurationError, "The Screenshot Scout base URL must be a string when provided."
|
|
45
|
+
end
|
|
46
|
+
unless base_url.nil? || base_url.empty?
|
|
47
|
+
return ResolvedBaseUrl.new(capture_endpoint: append_capture_path(base_url), from_environment: false)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
environment_base_url = read_variable(environment, BASE_URL_VARIABLE)
|
|
51
|
+
if environment_base_url.nil?
|
|
52
|
+
ResolvedBaseUrl.new(capture_endpoint: append_capture_path(DEFAULT_BASE_URL), from_environment: false)
|
|
53
|
+
else
|
|
54
|
+
ResolvedBaseUrl.new(capture_endpoint: append_capture_path(environment_base_url), from_environment: true)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Uses no regular expression, which would raise ArgumentError for text that isn't valid UTF-8.
|
|
59
|
+
def append_capture_path(base_url)
|
|
60
|
+
base_url = base_url.delete_suffix("/") while base_url.end_with?("/")
|
|
61
|
+
base_url + CAPTURE_PATH
|
|
62
|
+
end
|
|
63
|
+
private_class_method :append_capture_path
|
|
64
|
+
|
|
32
65
|
# Returns nil for a variable that is unset or empty.
|
|
33
66
|
def read_variable(environment, name)
|
|
34
67
|
value = environment[name]
|
data/sig/screenshotscout.rbs
CHANGED