Created
February 13, 2016 21:13
-
-
Save dazza-codes/89efe92d43449c07c729 to your computer and use it in GitHub Desktop.
Faraday JSON client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faraday' | |
require 'faraday_middleware' | |
class Client | |
JSON_CONTENT = 'application/json' | |
attr_reader :conn | |
# Initialize a new client | |
def initialize | |
@base_uri = 'https://api.example.org' | |
@api_path = '/v1/xyz' | |
@conn = Faraday.new(url: @base_uri) do |conn| | |
# conn.use FaradayMiddleware::FollowRedirects, limit: 3 | |
# conn.use Faraday::Response::RaiseError # raise exceptions on 40x, 50x | |
# conn.request :logger, @config.logger | |
conn.request :retry, max: 2, | |
interval: 0.5, | |
interval_randomness: 0.5, | |
backoff_factor: 2 | |
# conn.request :url_encoded | |
conn.request :json | |
conn.response :json, :content_type => JSON_CONTENT | |
conn.adapter :httpclient | |
end | |
@conn.options.timeout = 90 | |
@conn.options.open_timeout = 10 | |
#@conn.headers.merge!(json_payloads) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment