Skip to content

Use Faraday to wrap all http requests. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/tempodb.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rubygems'
require 'httpclient'
require 'faraday'
require 'net/https'
require 'json'
require 'time'
Expand Down
49 changes: 30 additions & 19 deletions lib/tempodb/client.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require 'openssl'

module TempoDB
class Client
attr_accessor :http_adapter

def initialize(key, secret, host=TempoDB::API_HOST, port=TempoDB::API_PORT, secure=true)
@key = key
@secret = secret
@host = host
@port = port
@secure = secure
@http_adapter = :patron
end

def create_series(key=nil)
Expand Down Expand Up @@ -183,33 +188,39 @@ def increment(series_type, series_val, data)
do_post(url, nil, body)
end

def do_http(uri, request) # :nodoc:
if @http_client.nil?
@http_client = HTTPClient.new
@http_client.transparent_gzip_decompression = true
if @secure
@http_client.ssl_config.clear_cert_store
@http_client.ssl_config.set_trust_ca(TempoDB::TRUSTED_CERT_FILE)
end
def build_client
headers = { :accept => 'application/json',
:user_agent => "tempodb-ruby/#{TempoDB::VERSION}",
# TempoDb doesn't seem to return gzipped content on our calls, so let's leave this out for the time being
# To get it to work later, we need a Faraday middle-ware to do the unzipping
# :accept_encoding => "gzip"
}

# Calls still succeed without setting up the cert store. Need to investigate that.
cert_store = OpenSSL::X509::Store.new
cert_store.add_file(TempoDB::TRUSTED_CERT_FILE)
ssl_options = {:cert_store => cert_store}

@http_client = Faraday::Connection.new(:headers => headers, :ssl => ssl_options) do |conn|
conn.adapter @http_adapter
conn.basic_auth(@key, @secret)
end
end

request.basic_auth @key, @secret
request['User-Agent'] = "tempodb-ruby/#{TempoDB::VERSION}"
request['Accept-Encoding'] = "gzip"
def do_http(uri, request) # :nodoc:
@http_client ||= build_client

method = request.method.downcase.intern
http_client_attrs = {
:header => Hash[request.to_hash.map {|k,v| [k, v.first] if v.is_a?(Array) && v.size == 1}],
:body => request.body
}
headers = Hash[request.to_hash.map {|k,v| [k, v.first] if v.is_a?(Array) && v.size == 1}]
body = request.body

begin
response = @http_client.request(method, uri, http_client_attrs)
# response = @http_client.request(method, uri, http_client_attrs)
response = @http_client.run_request(method, uri, body, headers)
rescue OpenSSL::SSL::SSLError => e
raise TempoDBClientError.new("SSL error connecting to TempoDB. " +
"There may be a problem with the set of certificates in \"#{TempoDB::TRUSTED_CERT_FILE}\". " + e)
end

parse_response(response)
end

Expand Down Expand Up @@ -275,7 +286,7 @@ def urlencode(params)
end

def parse_response(response)
if response.ok?
if response.success?
body = response.body

begin
Expand All @@ -288,7 +299,7 @@ def parse_response(response)
return body
end
else
raise TempoDBClientError.new("Error: #{response.status_code} #{response.reason}\n#{response.body}")
raise TempoDBClientError.new("Error: #{response.status}\n#{response.body}")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
describe "get_series" do
context "with no options provided" do
it "lists all series in the database" do
stub_request(:get, "https://api.tempo-db.com/v1/series/?").
stub_request(:get, "https://api.tempo-db.com/v1/series/").
to_return(:status => 200, :body => response_fixture('list_all_series.json'), :headers => {})
client = TempoDB::Client.new("key", "secret")
client.get_series.size.should == 7
Expand Down
2 changes: 1 addition & 1 deletion tempodb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_runtime_dependency "json"
s.add_runtime_dependency "httpclient"
s.add_runtime_dependency "faraday"
s.add_development_dependency "rspec", "~> 2.12.0"
s.add_development_dependency "webmock", "~> 1.8.10"
end