Skip to content

Commit 0d038d4

Browse files
committed
very basic failover to alternative endpoint if it exists
1 parent c0e9b7d commit 0d038d4

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

lib/intercom.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def self.target_base_url
5858

5959
def self.send_request_to_path(request)
6060
request.execute(target_base_url)
61+
rescue Intercom::ServiceReachableError
62+
if @endpoints && @endpoints.length > 1
63+
@endpoints = @endpoints.rotate
64+
request.execute(target_base_url)
65+
end
6166
end
6267

6368
def self.post(path, payload_hash)
@@ -76,7 +81,7 @@ def self.get(path, params)
7681
send_request_to_path(Intercom::Request.get(path, params))
7782
end
7883

79-
def self.check_required_params(params, path=nil)
84+
def self.check_required_params(params, path=nil) #nodoc
8085
return if path.eql?("users")
8186
raise ArgumentError.new("Expected params Hash, got #{params.class}") unless params.is_a?(Hash)
8287
raise ArgumentError.new("Either email or user_id must be specified") unless params.keys.any? { |key| %W(email user_id).include?(key.to_s) }
@@ -86,23 +91,23 @@ def self.protocol #nodoc
8691
@protocol
8792
end
8893

89-
def self.protocol=(override)
94+
def self.protocol=(override) #nodoc
9095
@protocol = override
9196
end
9297

93-
def self.hostname
98+
def self.hostname #nodoc
9499
@hostname
95100
end
96101

97-
def self.hostname=(override)
102+
def self.hostname=(override) #nodoc
98103
@hostname = override
99104
end
100105

101-
def self.endpoint=(endpoint)
106+
def self.endpoint=(endpoint) #nodoc
102107
self.endpoints = [endpoint]
103108
end
104109

105-
def self.endpoints=(endpoints)
110+
def self.endpoints=(endpoints) #nodoc
106111
@endpoints = endpoints
107112
end
108113

@@ -115,6 +120,10 @@ class AuthenticationError < StandardError;
115120
class ServerError < StandardError;
116121
end
117122

123+
# Raised when we reach socket connect timeout
124+
class ServiceReachableError < StandardError;
125+
end
126+
118127
# Raised when requesting resources on behalf of a user that doesn't exist in your application on Intercom.
119128
class ResourceNotFound < StandardError;
120129
end

lib/intercom/request.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def client(uri)
5151
net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
5252
end
5353
net.read_timeout = 30
54-
net.open_timeout = 10
54+
net.open_timeout = 3
5555
net
5656
end
5757

@@ -64,6 +64,8 @@ def execute(target_base_url=nil)
6464
decoded = decode(response['content-encoding'], response.body)
6565
JSON.parse(decoded)
6666
end
67+
rescue Timeout::Error
68+
raise Intercom::ServiceReachableError
6769
end
6870

6971
def decode(content_encoding, body)

spec/integration/intercom_api_integration_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
before :each do
66
Intercom.app_id = "dummy-app-id"
77
Intercom.api_key = "dummy-secret-key"
8+
Intercom.endpoint = "https://api.intercom.io"
89
end
910

1011
it "should get all user" do
@@ -53,4 +54,11 @@
5354
note.html.must_equal "<p>This is a note</p>"
5455
note.user.email.must_equal "[email protected]"
5556
end
57+
58+
it "should failover to good endpoint when first one is un-reachable" do
59+
Intercom.endpoints = ["http://127.0.0.7", "https://api.intercom.io"]
60+
61+
user = Intercom::User.find(:email => "[email protected]")
62+
user.name.must_equal "Somebody"
63+
end
5664
end

0 commit comments

Comments
 (0)