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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 927d2422558ddcdfc7ea1ba02f45a45657533a6c
4
- data.tar.gz: 780462719756a3027f400015bdf6b2d548e605a2
3
+ metadata.gz: 2a5c1dde854ffd27a47f940f734f8019bb66a29f
4
+ data.tar.gz: a2f256f6513573642f3d49fd0671c94ee030d5d5
5
5
  SHA512:
6
- metadata.gz: 0e639771570ab45a086fe5153d8b06b32f7b07ec963298eec85098b7e27d663d2466b48ea8b77ec08847687ca69ce54bdb5604c8fb311ca6bc652b5182010e74
7
- data.tar.gz: 45bef59d37c95316520df8d3a8e74e154daf13ffb4f7683bfdf068060c57ee214972c536cebe0832d63516c232a2def18cd2ea63bab09bab030be45de46311b1
6
+ metadata.gz: ad9625e9cae5c9e261847159e2273af3057926737f94eabda14321788bf08e1b812ffe928004628f720e15df3dcf824c395776ed7624396e8d20ea4de68985d4
7
+ data.tar.gz: b0beeba5ecbc5d99f871149848425183d4a86aa4e748003c19b52a37d1e6b8299f9097b77efc8b01aec118d2294fafbf111ffc65e2781b268b60010d05a82e87
@@ -15,10 +15,19 @@ module Percy
15
15
  include Percy::Client::Resources
16
16
 
17
17
  class Error < Exception; end
18
- class ClientError < Error
19
- attr_accessor :env
20
- def initialize(env, *args)
21
- @env = env
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::ClientError.new(
29
- env, "Got #{env.status} (#{env.method.upcase} #{env.url}):\n#{env.body}")
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
- response = connection.get do |request|
49
- request.url(path)
50
- request.headers['Content-Type'] = 'application/vnd.api+json'
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
- response = connection.post do |request|
57
- request.url(path)
58
- request.headers['Content-Type'] = 'application/vnd.api+json'
59
- request.body = data.to_json
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::ClientError => e
69
- raise e if e.env.status != 409
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
@@ -1,5 +1,5 @@
1
1
  module Percy
2
2
  class Client
3
- VERSION = '0.3.2'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -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::ClientError)
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.3.2
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-08 00:00:00.000000000 Z
11
+ date: 2015-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday