Skip to content

Commit fce586a

Browse files
committed
build faraday-based connection class for making http request
1 parent 46629ec commit fce586a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

lib/fcm/connection.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require 'faraday'
4+
require 'faraday/retry'
5+
require 'faraday/typhoeus'
6+
7+
module Fcm
8+
# underhood http client using faraday with typhoeus adapter
9+
module Connection
10+
DEFAULT_TIMEOUT = 30
11+
12+
def make_request(method, base_uri, end_point, body, headers)
13+
url = "#{base_uri}#{end_point}"
14+
agent.send(method, url, body, headers)
15+
end
16+
17+
private
18+
19+
def agent
20+
@agent ||= Faraday.new(
21+
request: { timeout: DEFAULT_TIMEOUT }
22+
) do |builder|
23+
builder.request :retry, exceptions: [
24+
Faraday::TimeoutError, Faraday::ConnectionFailed
25+
]
26+
builder.request :json
27+
builder.response :json
28+
builder.adapter :typhoeus
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)