percy-client 0.3.2 → 0.4.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/lib/percy/client.rb +13 -4
- data/lib/percy/client/connection.rb +38 -13
- data/lib/percy/client/resources.rb +2 -2
- data/lib/percy/client/version.rb +1 -1
- data/spec/lib/percy/client/connection_spec.rb +54 -0
- data/spec/lib/percy/client/snapshots_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a5c1dde854ffd27a47f940f734f8019bb66a29f
|
4
|
+
data.tar.gz: a2f256f6513573642f3d49fd0671c94ee030d5d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad9625e9cae5c9e261847159e2273af3057926737f94eabda14321788bf08e1b812ffe928004628f720e15df3dcf824c395776ed7624396e8d20ea4de68985d4
|
7
|
+
data.tar.gz: b0beeba5ecbc5d99f871149848425183d4a86aa4e748003c19b52a37d1e6b8299f9097b77efc8b01aec118d2294fafbf111ffc65e2781b268b60010d05a82e87
|
data/lib/percy/client.rb
CHANGED
@@ -15,10 +15,19 @@ module Percy
|
|
15
15
|
include Percy::Client::Resources
|
16
16
|
|
17
17
|
class Error < Exception; end
|
18
|
-
class
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
class TimeoutError < Error; end
|
19
|
+
class ConnectionFailed < Error; end
|
20
|
+
class HttpError < Error
|
21
|
+
attr_reader :status
|
22
|
+
attr_reader :method
|
23
|
+
attr_reader :url
|
24
|
+
attr_reader :body
|
25
|
+
|
26
|
+
def initialize(status, method, url, body, *args)
|
27
|
+
@status = status
|
28
|
+
@method = method
|
29
|
+
@url = url
|
30
|
+
@body = body
|
22
31
|
super(*args)
|
23
32
|
end
|
24
33
|
end
|
@@ -9,7 +9,6 @@ module Percy
|
|
9
9
|
def client
|
10
10
|
@client ||= ::HTTPClient.new
|
11
11
|
@client.cookie_manager = nil
|
12
|
-
# Turn off SSLv2 and SSLv3 to force TLS because CloudFlare smartly blocks SSLv2 and SSLv3.
|
13
12
|
@client.ssl_config.options |= OpenSSL::SSL::OP_NO_SSLv2
|
14
13
|
@client.ssl_config.options |= OpenSSL::SSL::OP_NO_SSLv3
|
15
14
|
@client
|
@@ -21,12 +20,10 @@ module Percy
|
|
21
20
|
|
22
21
|
def on_complete(env)
|
23
22
|
case env[:status]
|
24
|
-
when 407
|
25
|
-
# Mimic the behavior that we get with proxy requests with HTTPS.
|
26
|
-
raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
|
27
23
|
when CLIENT_ERROR_STATUS_RANGE
|
28
|
-
raise Percy::Client::
|
29
|
-
env
|
24
|
+
raise Percy::Client::HttpError.new(
|
25
|
+
env.status, env.method.upcase, env.url, env.body,
|
26
|
+
"Got #{env.status} (#{env.method.upcase} #{env.url}):\n#{env.body}")
|
30
27
|
end
|
31
28
|
end
|
32
29
|
end
|
@@ -45,18 +42,46 @@ module Percy
|
|
45
42
|
end
|
46
43
|
|
47
44
|
def get(path)
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
retries = 3
|
46
|
+
begin
|
47
|
+
response = connection.get do |request|
|
48
|
+
request.url(path)
|
49
|
+
request.headers['Content-Type'] = 'application/vnd.api+json'
|
50
|
+
end
|
51
|
+
rescue Faraday::TimeoutError
|
52
|
+
raise Percy::Client::TimeoutError
|
53
|
+
rescue Faraday::ConnectionFailed
|
54
|
+
raise Percy::Client::ConnectionFailed
|
55
|
+
rescue Percy::Client::HttpError => e
|
56
|
+
# Retry on 502 errors.
|
57
|
+
if e.status == 502 && (retries -= 1) >= 0
|
58
|
+
sleep(rand(1..3))
|
59
|
+
retry
|
60
|
+
end
|
61
|
+
raise e
|
51
62
|
end
|
52
63
|
JSON.parse(response.body)
|
53
64
|
end
|
54
65
|
|
55
66
|
def post(path, data)
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
67
|
+
retries = 3
|
68
|
+
begin
|
69
|
+
response = connection.post do |request|
|
70
|
+
request.url(path)
|
71
|
+
request.headers['Content-Type'] = 'application/vnd.api+json'
|
72
|
+
request.body = data.to_json
|
73
|
+
end
|
74
|
+
rescue Faraday::TimeoutError
|
75
|
+
raise Percy::Client::TimeoutError
|
76
|
+
rescue Faraday::ConnectionFailed
|
77
|
+
raise Percy::Client::ConnectionFailed
|
78
|
+
rescue Percy::Client::HttpError => e
|
79
|
+
# Retry on 502 errors.
|
80
|
+
if e.status == 502 && (retries -= 1) >= 0
|
81
|
+
sleep(rand(1..3))
|
82
|
+
retry
|
83
|
+
end
|
84
|
+
raise e
|
60
85
|
end
|
61
86
|
JSON.parse(response.body)
|
62
87
|
end
|
@@ -65,8 +65,8 @@ module Percy
|
|
65
65
|
}
|
66
66
|
begin
|
67
67
|
post("#{config.api_url}/builds/#{build_id}/resources/", data)
|
68
|
-
rescue Percy::Client::
|
69
|
-
raise e if e.
|
68
|
+
rescue Percy::Client::HttpError => e
|
69
|
+
raise e if e.status != 409
|
70
70
|
STDERR.puts "[percy] Warning: unnecessary resource reuploaded with SHA-256: #{sha}"
|
71
71
|
end
|
72
72
|
true
|
data/lib/percy/client/version.rb
CHANGED
@@ -10,6 +10,33 @@ RSpec.describe Percy::Client::Connection do
|
|
10
10
|
data = Percy.client.get("#{Percy.config.api_url}/test")
|
11
11
|
expect(data).to eq({'foo' => true})
|
12
12
|
end
|
13
|
+
it 'raises customized timeout errors' do
|
14
|
+
stub_request(:get, "#{Percy.config.api_url}/test").to_raise(Faraday::TimeoutError)
|
15
|
+
expect do
|
16
|
+
Percy.client.get("#{Percy.config.api_url}/test")
|
17
|
+
end.to raise_error(Percy::Client::TimeoutError)
|
18
|
+
end
|
19
|
+
it 'raises customized connection failed errors' do
|
20
|
+
stub_request(:get, "#{Percy.config.api_url}/test").to_raise(Faraday::ConnectionFailed)
|
21
|
+
expect do
|
22
|
+
Percy.client.get("#{Percy.config.api_url}/test")
|
23
|
+
end.to raise_error(Percy::Client::ConnectionFailed)
|
24
|
+
end
|
25
|
+
it 'retries on 502 errors' do
|
26
|
+
stub_request(:get, "#{Percy.config.api_url}/test")
|
27
|
+
.to_return(body: {foo: true}.to_json, status: 502)
|
28
|
+
.then.to_return(body: {foo: true}.to_json, status: 200)
|
29
|
+
|
30
|
+
data = Percy.client.get("#{Percy.config.api_url}/test")
|
31
|
+
expect(data).to eq({'foo' => true})
|
32
|
+
end
|
33
|
+
it 'raises error after 3 retries' do
|
34
|
+
stub_request(:get, "#{Percy.config.api_url}/test")
|
35
|
+
.to_return(body: {foo: true}.to_json, status: 502).times(3)
|
36
|
+
expect do
|
37
|
+
Percy.client.get("#{Percy.config.api_url}/test")
|
38
|
+
end.to raise_error(Percy::Client::HttpError)
|
39
|
+
end
|
13
40
|
end
|
14
41
|
describe '#post' do
|
15
42
|
it 'performs a POST request to the api_url and parses response' do
|
@@ -17,5 +44,32 @@ RSpec.describe Percy::Client::Connection do
|
|
17
44
|
data = Percy.client.post("#{Percy.config.api_url}/test", {})
|
18
45
|
expect(data).to eq({'foo' => true})
|
19
46
|
end
|
47
|
+
it 'raises customized timeout errors' do
|
48
|
+
stub_request(:post, "#{Percy.config.api_url}/test").to_raise(Faraday::TimeoutError)
|
49
|
+
expect do
|
50
|
+
Percy.client.post("#{Percy.config.api_url}/test", {})
|
51
|
+
end.to raise_error(Percy::Client::TimeoutError)
|
52
|
+
end
|
53
|
+
it 'raises customized connection failed errors' do
|
54
|
+
stub_request(:post, "#{Percy.config.api_url}/test").to_raise(Faraday::ConnectionFailed)
|
55
|
+
expect do
|
56
|
+
Percy.client.post("#{Percy.config.api_url}/test", {})
|
57
|
+
end.to raise_error(Percy::Client::ConnectionFailed)
|
58
|
+
end
|
59
|
+
it 'retries on 502 errors' do
|
60
|
+
stub_request(:post, "#{Percy.config.api_url}/test")
|
61
|
+
.to_return(body: {foo: true}.to_json, status: 502)
|
62
|
+
.then.to_return(body: {foo: true}.to_json, status: 200)
|
63
|
+
|
64
|
+
data = Percy.client.post("#{Percy.config.api_url}/test", {})
|
65
|
+
expect(data).to eq({'foo' => true})
|
66
|
+
end
|
67
|
+
it 'raises error after 3 retries' do
|
68
|
+
stub_request(:post, "#{Percy.config.api_url}/test")
|
69
|
+
.to_return(body: {foo: true}.to_json, status: 502).times(3)
|
70
|
+
expect do
|
71
|
+
Percy.client.post("#{Percy.config.api_url}/test", {})
|
72
|
+
end.to raise_error(Percy::Client::HttpError)
|
73
|
+
end
|
20
74
|
end
|
21
75
|
end
|
@@ -20,7 +20,7 @@ RSpec.describe Percy::Client::Snapshots, :vcr do
|
|
20
20
|
build = Percy.create_build('fotinakis/percy-examples')
|
21
21
|
expect do
|
22
22
|
Percy.create_snapshot(build['data']['id'], [])
|
23
|
-
end.to raise_error(Percy::Client::
|
23
|
+
end.to raise_error(Percy::Client::HttpError)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
describe '#finalize_snapshot' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: percy-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Perceptual Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|