lib/blurb/base_resource.rb in blurb-0.2.3 vs lib/blurb/base_resource.rb in blurb-0.3.0
- old
+ new
@@ -1,35 +1,100 @@
module Blurb
class BaseResource
- def self.profile_request(api_path)
- access_token = Blurb::Token.retrieve()
+ attr_accessor :client_secret, :client_id, :refresh_token, :profile_id, :test_env, :eu_env
+ TOKEN_URL = "https://api.amazon.com"
+ API_URL = "https://advertising-api.amazon.com"
+ TEST_API_URL = "https://advertising-api-test.amazon.com"
+ EU_API_URL = "https://advertising-api-eu.amazon.com"
+
+ def initialize(account=Blurb.default_account)
+ @client_secret = account[:client_secret]
+ @client_id = account[:client_id]
+ @refresh_token = account[:refresh_token]
+ @profile_id = account[:profile_id]
+ @test_env = Blurb.test_env
+ @eu_env = account[:eu_env]
+ end
+
+ def active_api_url
+ if @test_env
+ return TEST_API_URL
+ end
+ if @eu_env
+ return EU_API_URL
+ end
+
+ return API_URL
+ end
+
+ def client
+ return OAuth2::Client.new(
+ "",
+ "",
+ :site => TOKEN_URL
+ )
+ end
+
+ def token_code(auth_code)
+ response = client.request(:post, "/auth/o2/token",
+ {
+ body: {
+ grant_type: "authorization_code",
+ client_id: @client_id,
+ code: auth_code,
+ client_secret: @client_secret
+ }
+ }
+ )
+
+ return JSON.parse(response.body)
+ end
+
+ def retrieve_token(params = {}, opts = {})
+ response = client.request(:post, "/auth/o2/token",
+ {
+ body: {
+ grant_type: "refresh_token",
+ client_id: @client_id,
+ refresh_token: @refresh_token,
+ client_secret: @client_secret
+ }
+ }
+ )
+
+ return JSON.parse(response.body)
+ end
+
+ def profile_request(api_path)
+ access_token = retrieve_token()
+
request_config = {
method: :get,
- url: "#{Blurb.active_api_url}#{api_path}",
+ url: "#{active_api_url}#{api_path}",
headers: {
"Authorization" => "Bearer #{access_token['access_token']}",
"Content-Type" => "application/json",
- "Amazon-Advertising-API-ClientId" => Blurb.client_id
+ "Amazon-Advertising-API-ClientId" => @client_id
}
}
resp = RestClient::Request.execute(request_config)
return JSON.parse(resp)
end
- def self.get_request(api_path, opts = {})
- access_token = Blurb::Token.retrieve()
+ def get_request(api_path, opts = {})
+ access_token = retrieve_token()
- url = "#{Blurb.active_api_url}#{api_path}"
+ url = "#{active_api_url}#{api_path}"
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-ClientId" => Blurb.client_id
+ "Amazon-Advertising-API-Scope" => @profile_id,
+ "Amazon-Advertising-API-ClientId" => @client_id
}
headers_hash["Content-Encoding"] = "gzip" if opts[:gzip]
# headers_hash.delete("Authorization") if opts[:no_token]
@@ -41,49 +106,49 @@
}
return make_request(request_config)
end
- def self.post_request(api_path, payload)
- access_token = Blurb::Token.retrieve()
+ def post_request(api_path, payload)
+ access_token = retrieve_token()
request_config = {
method: :post,
- url: "#{Blurb.active_api_url}#{api_path}",
+ url: "#{active_api_url}#{api_path}",
payload: payload.to_json,
headers: {
"Authorization" => "Bearer #{access_token['access_token']}",
"Content-Type" => "application/json",
- "Amazon-Advertising-API-Scope" => Blurb.profile_id.to_i,
- "Amazon-Advertising-API-ClientId" => Blurb.client_id
+ "Amazon-Advertising-API-Scope" => @profile_id.to_i,
+ "Amazon-Advertising-API-ClientId" => @client_id
}
}
return make_request(request_config)
end
- def self.delete_request(api_path)
- access_token = Blurb::Token.retrieve()
+ def delete_request(api_path)
+ access_token = retrieve_token()
headers_hash = {
"Authorization" => "Bearer #{access_token['access_token']}",
"Content-Type" => "application/json",
- "Amazon-Advertising-API-Scope" => Blurb.profile_id,
- "Amazon-Advertising-API-ClientId" => Blurb.client_id
+ "Amazon-Advertising-API-Scope" => @profile_id,
+ "Amazon-Advertising-API-ClientId" => @client_id
}
request_config = {
method: :delete,
- url: "#{Blurb.active_api_url}#{api_path}",
+ url: "#{active_api_url}#{api_path}",
headers: headers_hash,
}
return make_request(request_config)
end
private
- def self.make_request(request_config)
+ def 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