Skip to content

Commit

Permalink
Use headers only for api and app keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ssc3 committed Sep 24, 2019
1 parent 2c2606b commit 2ca618a
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 26 deletions.
6 changes: 4 additions & 2 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ def request(method, url, extra_params, body, send_json, with_app_key=true)
connect do |conn|
begin
current_url = url + prepare_params(extra_params, with_app_key)
# current_url = url
req = method.new(current_url)
req['DD-API-KEY'] = @api_key if @api_key
req['DD-APPLICATION_KEY'] = @application_key if @application_key

if send_json
req.content_type = 'application/json'
Expand All @@ -133,8 +136,7 @@ def request(method, url, extra_params, body, send_json, with_app_key=true)
end

def prepare_params(extra_params, with_app_key)
params = { api_key: @api_key }
params[:application_key] = @application_key if with_app_key
params = {}
params = extra_params.merge params unless extra_params.nil?
qs_params = params.map { |k, v| CGI.escape(k.to_s) + '=' + CGI.escape(v.to_s) }
qs = '?' + qs_params.join('&')
Expand Down
4 changes: 1 addition & 3 deletions spec/integration/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
stub_request(:put, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(dog.send(:update_comment, COMMENT_ID, options)).to eq ['200', {}]

expect(WebMock).to have_requested(:put, url).with(
query: default_query
)
expect(WebMock).to have_requested(:put, url)
end
end

Expand Down
8 changes: 2 additions & 6 deletions spec/integration/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@
stub_request(:get, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(service.request(Net::HTTP::Get, '/api/v1/awesome', nil, nil, true, true)).to eq(['200', {}])

expect(WebMock).to have_requested(:get, url).with(
query: default_query
)
expect(WebMock).to have_requested(:get, url)
end
end
context 'and it is down' do
it 'only queries one endpoint' do
stub_request(:get, /#{url}/).to_timeout
expect(service.request(Net::HTTP::Get, '/api/v1/awesome', nil, nil, true, true)).to eq([-1, {}])

expect(WebMock).to have_requested(:get, url).with(
query: default_query
)
expect(WebMock).to have_requested(:get, url)
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions spec/integration/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
body = MultiJson.dump(EVENT_OPTIONS)

expect(WebMock).to have_requested(:post, url).with(
query: { 'api_key' => api_key },
body: body
)
end
Expand All @@ -63,7 +62,6 @@

params = STREAM_PARAMS.merge(start: EVENT_START, end: EVENT_END)
params.each { |k, v| params[k] = v.join(',') if v.is_a? Array }
params.merge! default_query

expect(WebMock).to have_requested(:get, url).with(
query: params
Expand Down
3 changes: 0 additions & 3 deletions spec/integration/metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
body = MultiJson.dump(body)

expect(WebMock).to have_requested(:post, url).with(
query: { 'api_key' => api_key },
body: body
)
end
Expand All @@ -64,7 +63,6 @@
body = MultiJson.dump(EMIT_BODY)

expect(WebMock).to have_requested(:post, url).with(
query: { 'api_key' => api_key },
body: body
)
end
Expand All @@ -89,7 +87,6 @@
body = MultiJson.dump(BATCH_BODY)

expect(WebMock).to have_requested(:post, url).with(
query: { 'api_key' => api_key },
body: body
)
end
Expand Down
14 changes: 4 additions & 10 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ module SpecDog
let(:old_api_url) { "#{DATADOG_HOST}/api" }
let(:api_v2_url) { "#{DATADOG_HOST}/api/v2" }

let(:default_query) { { api_key: api_key, application_key: app_key } }

shared_examples 'an api method' do |command, args, request, endpoint, body|
it 'queries the api' do
url = api_url + endpoint
Expand All @@ -37,7 +35,6 @@ module SpecDog
body = MultiJson.dump(body) if body

expect(WebMock).to have_requested(request, /#{url}|#{old_url}/).with(
query: default_query,
body: body
)
end
Expand All @@ -55,7 +52,6 @@ module SpecDog
body = MultiJson.dump(body ? (body.merge options) : options)

expect(WebMock).to have_requested(request, /#{url}|#{old_url}/).with(
query: default_query,
body: body
)
end
Expand All @@ -67,7 +63,7 @@ module SpecDog
stub_request(request, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(dog.send(command, *args, *params.values)).to eq ['200', {}]
params.each { |k, v| params[k] = v.join(',') if v.is_a? Array }
params = params.merge default_query
params = params

expect(WebMock).to have_requested(request, url).with(
query: params
Expand All @@ -83,7 +79,7 @@ module SpecDog
expect(dog.send(command, *args, opt_params)).to eq ['200', {}]

opt_params.each { |k, v| opt_params[k] = v.join(',') if v.is_a? Array }
params = opt_params.merge default_query
params = opt_params

expect(WebMock).to have_requested(request, url).with(
query: params
Expand All @@ -101,7 +97,6 @@ module SpecDog
body = MultiJson.dump(body) if body

expect(WebMock).to have_requested(request, url).with(
query: default_query,
body: body
)
end
Expand All @@ -117,7 +112,6 @@ module SpecDog
body = MultiJson.dump(body ? (body.merge options) : options)

expect(WebMock).to have_requested(request, url).with(
query: default_query,
body: body
)
end
Expand All @@ -129,7 +123,7 @@ module SpecDog
stub_request(request, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(dog2.send(command, *args, *params.values)).to eq ['200', {}]
params.each { |k, v| params[k] = v.join(',') if v.is_a? Array }
params = params.merge default_query
params = params

expect(WebMock).to have_requested(request, url).with(
query: params
Expand All @@ -144,7 +138,7 @@ module SpecDog
stub_request(request, /#{url}/).to_return(body: '{}').then.to_raise(StandardError)
expect(dog2.send(command, *args, opt_params)).to eq ['200', {}]
opt_params.each { |k, v| opt_params[k] = v.join(',') if v.is_a? Array }
params = opt_params.merge default_query
params = opt_params

expect(WebMock).to have_requested(request, url).with(
query: params
Expand Down

0 comments on commit 2ca618a

Please sign in to comment.