lib/blurb/base_resource.rb in blurb-0.1.2 vs lib/blurb/base_resource.rb in blurb-0.2.0

- old
+ new

@@ -5,12 +5,13 @@ request_config = { method: :get, url: "#{Blurb.active_api_url}#{api_path}", headers: { - :Authorization => "Bearer #{access_token['access_token']}", - "Content-Type" => "application/json" + "Authorization" => "Bearer #{access_token['access_token']}", + "Content-Type" => "application/json", + "Amazon-Advertising-API-ClientId" => Blurb.client_id } } resp = RestClient::Request.execute(request_config) return JSON.parse(resp) @@ -23,11 +24,12 @@ url = api_path if opts[:full_path] headers_hash = { "Authorization" => "Bearer #{access_token['access_token']}", "Content-Type" => "application/json", - "Amazon-Advertising-API-Scope" => Blurb.profile_id + "Amazon-Advertising-API-Scope" => Blurb.profile_id, + "Amazon-Advertising-API-ClientId" => Blurb.client_id } headers_hash["Content-Encoding"] = "gzip" if opts[:gzip] # headers_hash.delete("Authorization") if opts[:no_token] @@ -36,37 +38,45 @@ url: url, headers: headers_hash, max_redirects: 0 } - begin - resp = RestClient::Request.execute(request_config) - rescue RestClient::ExceptionWithResponse => err - # If this happens, then we are downloading a report from the api, so we can simply download the location - if err.response.code == 307 - return RestClient.get(err.response.headers[:location]) - end - end - - response = JSON.parse(resp) if resp - return response + return make_request(request_config) end def self.post_request(api_path, payload) access_token = Blurb::Token.retrieve() request_config = { method: :post, url: "#{Blurb::API_URL}#{api_path}", payload: payload.to_json, headers: { - :Authorization => "Bearer #{access_token['access_token']}", + "Authorization" => "Bearer #{access_token['access_token']}", "Content-Type" => "application/json", - "Amazon-Advertising-API-Scope" => Blurb.profile_id.to_i + "Amazon-Advertising-API-Scope" => Blurb.profile_id.to_i, + "Amazon-Advertising-API-ClientId" => Blurb.client_id } } - resp = RestClient::Request.execute(request_config) - return JSON.parse(resp) + return make_request(request_config) + end + + private + + def self.make_request(request_config) + begin + resp = RestClient::Request.execute(request_config) + rescue RestClient::ExceptionWithResponse => err + # If this happens, then we are downloading a report from the api, so we can simply download the location + if err.response.code == 307 + return RestClient.get(err.response.headers[:location]) + else + return err.response.body + end + end + + response = JSON.parse(resp) if resp + return response end end end