Skip to content

Commit

Permalink
W-6115601 Update all functions to use Authorization header (#40)
Browse files Browse the repository at this point in the history
Update calls to use Authorization header instead of query string
  • Loading branch information
Jared Pearson authored May 7, 2019
1 parent e8cfc06 commit 418d66f
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ruby-pardot (1.2.0)
ruby-pardot (1.3.0)
crack (= 0.4.3)
httparty (= 0.13.1)

Expand All @@ -25,7 +25,7 @@ GEM
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
safe_yaml (1.0.4)
safe_yaml (1.0.5)

PLATFORMS
ruby
Expand Down
13 changes: 10 additions & 3 deletions lib/pardot/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Http
def get object, path, params = {}, num_retries = 0
smooth_params object, params
full_path = fullpath object, path
check_response self.class.get(full_path, :query => params)
headers = create_auth_header object
check_response self.class.get(full_path, :query => params, :headers => headers)

rescue Pardot::ExpiredApiKeyError => e
handle_expired_api_key :get, object, path, params, num_retries, e
Expand All @@ -16,7 +17,8 @@ def get object, path, params = {}, num_retries = 0
def post object, path, params = {}, num_retries = 0, bodyParams = {}
smooth_params object, params
full_path = fullpath object, path
check_response self.class.post(full_path, :query => params, :body => bodyParams)
headers = create_auth_header object
check_response self.class.post(full_path, :query => params, :body => bodyParams, :headers => headers)

rescue Pardot::ExpiredApiKeyError => e
handle_expired_api_key :post, object, path, params, num_retries, e
Expand All @@ -39,7 +41,12 @@ def smooth_params object, params
return if object == "login"

authenticate unless authenticated?
params.merge! :user_key => @user_key, :api_key => @api_key, :format => @format
params.merge! :format => @format
end

def create_auth_header object
return if object == "login"
{ :Authorization => "Pardot api_key=#{@api_key}, user_key=#{@user_key}" }
end

def check_response http_response
Expand Down
2 changes: 1 addition & 1 deletion lib/pardot/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Pardot
VERSION = "1.2.0"
VERSION = "1.3.0"
end
16 changes: 8 additions & 8 deletions spec/pardot/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get object = "foo", path = "/bar", params = {}
end

it "should notice errors and raise them as Pardot::ResponseError" do
fake_get "/api/foo/version/3/bar?api_key=my_api_key&format=simple&user_key=bar",
fake_get "/api/foo/version/3/bar?format=simple",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Login failed</err>\n</rsp>\n)

lambda { get }.should raise_error(Pardot::ResponseError)
Expand All @@ -31,7 +31,7 @@ def get object = "foo", path = "/bar", params = {}
end

it "should call handle_expired_api_key when the api key expires" do
fake_get "/api/foo/version/3/bar?api_key=my_api_key&format=simple&user_key=bar",
fake_get "/api/foo/version/3/bar?format=simple",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Invalid API key or user key</err>\n</rsp>\n)

@client.should_receive(:handle_expired_api_key)
Expand All @@ -47,7 +47,7 @@ def post object = "foo", path = "/bar", params = {}
end

it "should notice errors and raise them as Pardot::ResponseError" do
fake_post "/api/foo/version/3/bar?api_key=my_api_key&format=simple&user_key=bar",
fake_post "/api/foo/version/3/bar?format=simple",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Login failed</err>\n</rsp>\n)

lambda { post }.should raise_error(Pardot::ResponseError)
Expand All @@ -60,7 +60,7 @@ def post object = "foo", path = "/bar", params = {}
end

it "should call handle_expired_api_key when the api key expires" do
fake_post "/api/foo/version/3/bar?api_key=my_api_key&format=simple&user_key=bar",
fake_post "/api/foo/version/3/bar?format=simple",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Invalid API key or user key</err>\n</rsp>\n)

@client.should_receive(:handle_expired_api_key)
Expand All @@ -77,7 +77,7 @@ def get object = "foo", path = "/bar", params = {}
end

it "should notice errors and raise them as Pardot::ResponseError" do
fake_get "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
fake_get "/api/foo/version/4/bar?format=simple",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Login failed</err>\n</rsp>\n)

lambda { get }.should raise_error(Pardot::ResponseError)
Expand All @@ -90,7 +90,7 @@ def get object = "foo", path = "/bar", params = {}
end

it "should call handle_expired_api_key when the api key expires" do
fake_get "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
fake_get "/api/foo/version/4/bar?format=simple",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Invalid API key or user key</err>\n</rsp>\n)

@client.should_receive(:handle_expired_api_key)
Expand All @@ -107,7 +107,7 @@ def post object = "foo", path = "/bar", params = {}
end

it "should notice errors and raise them as Pardot::ResponseError" do
fake_post "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
fake_post "/api/foo/version/4/bar?format=simple",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Login failed</err>\n</rsp>\n)

lambda { post }.should raise_error(Pardot::ResponseError)
Expand All @@ -120,7 +120,7 @@ def post object = "foo", path = "/bar", params = {}
end

it "should call handle_expired_api_key when the api key expires" do
fake_post "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
fake_post "/api/foo/version/4/bar?format=simple",
%(?xml version="1.0" encoding="UTF-8"?>\n<rsp stat="fail" version="1.0">\n <err code="15">Invalid API key or user key</err>\n</rsp>\n)

@client.should_receive(:handle_expired_api_key)
Expand Down
9 changes: 6 additions & 3 deletions spec/pardot/objects/emails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ def sample_response
end

it "should take in the email ID" do
fake_get "/api/email/version/3/do/read/id/12?user_key=bar&api_key=my_api_key&format=simple", sample_response
fake_get "/api/email/version/3/do/read/id/12?format=simple", sample_response
@client.emails.read_by_id(12).should == {"name" => "My Email"}
assert_authorization_header
end

it 'should send to a prospect' do
fake_post '/api/email/version/3/do/send/prospect_id/42?campaign_id=765&email_template_id=86&user_key=bar&api_key=my_api_key&format=simple', sample_response
fake_post '/api/email/version/3/do/send/prospect_id/42?campaign_id=765&email_template_id=86&format=simple', sample_response
@client.emails.send_to_prospect(42, :campaign_id => 765, :email_template_id => 86).should == {"name" => "My Email"}
assert_authorization_header
end

it 'should send to a list' do
fake_post '/api/email/version/3/do/send?email_template_id=200&list_ids[]=235&campaign_id=654&user_key=bar&api_key=my_api_key&format=simple', sample_response
fake_post '/api/email/version/3/do/send?email_template_id=200&list_ids[]=235&campaign_id=654&format=simple', sample_response
@client.emails.send_to_list(:email_template_id => 200, 'list_ids[]' => 235, :campaign_id => 654).should == {"name" => "My Email"}
assert_authorization_header
end

end
3 changes: 2 additions & 1 deletion spec/pardot/objects/lists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ def sample_results
end

it "should take in some arguments" do
fake_get "/api/list/version/3/do/query?api_key=my_api_key&id_greater_than=200&format=simple&user_key=bar", sample_results
fake_get "/api/list/version/3/do/query?id_greater_than=200&format=simple", sample_results

@client.lists.query(:id_greater_than => 200).should == {"total_results" => 2,
"list"=>[
{"name"=>"Asdf List"},
{"name"=>"Qwerty List"}
]}
assert_authorization_header
end

end
Expand Down
8 changes: 5 additions & 3 deletions spec/pardot/objects/opportunities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def sample_results
end

it "should take in some arguments" do
fake_get "/api/opportunity/version/3/do/query?api_key=my_api_key&id_greater_than=200&format=simple&user_key=bar", sample_results
fake_get "/api/opportunity/version/3/do/query?id_greater_than=200&format=simple", sample_results

@client.opportunities.query(:id_greater_than => 200).should == {"total_results" => 2,
"opportunity"=>[
{"type"=>"Great", "name"=>"Jim"},
{"type"=>"Good", "name"=>"Sue"}
]}
assert_authorization_header
end

end
Expand All @@ -53,10 +54,11 @@ def sample_results
end

it "should return the prospect" do
fake_post "/api/opportunity/version/3/do/create/prospect_email/[email protected]?type=Good&api_key=my_api_key&user_key=bar&format=simple&name=Jim", sample_results
fake_post "/api/opportunity/version/3/do/create/prospect_email/[email protected]?type=Good&format=simple&name=Jim", sample_results

@client.opportunities.create_by_email("[email protected]", :name => "Jim", :type => "Good").should == {"name"=>"Jim", "type"=>"Good"}


assert_authorization_header
end

end
Expand Down
10 changes: 6 additions & 4 deletions spec/pardot/objects/prospect_accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ def sample_results
end

it "should take in some arguments and respond with valid items" do
fake_get "/api/prospectAccount/version/3/do/query?assigned=true&format=simple&user_key=bar&api_key=my_api_key", sample_results
fake_get "/api/prospectAccount/version/3/do/query?assigned=true&format=simple", sample_results

@client.prospect_accounts.query(:assigned => true).should == {'total_results' => 2,
'prospectAccount'=>[
{'name'=>'Spaceships R Us'},
{'name'=>'Monsters Inc'}
]}
assert_authorization_header
end

end
Expand All @@ -47,9 +48,10 @@ def sample_results
end

it 'should return a valid account' do
fake_post '/api/prospectAccount/version/3/do/read/id/1234?assigned=true&format=simple&user_key=bar&api_key=my_api_key', sample_results
fake_post '/api/prospectAccount/version/3/do/read/id/1234?assigned=true&format=simple', sample_results

@client.prospect_accounts.read('1234', :assigned => true).should == {'id' => '1234', 'name' => 'SupaDupaPanda' }
assert_authorization_header
end

end
Expand All @@ -67,10 +69,10 @@ def sample_results
end

it 'should return the prospect account' do
fake_post '/api/prospectAccount/version/3/do/create?api_key=my_api_key&user_key=bar&format=simple&name=SuperPanda', sample_results
fake_post '/api/prospectAccount/version/3/do/create?format=simple&name=SuperPanda', sample_results

@client.prospect_accounts.create(:name => 'SuperPanda').should == {"name"=>"SuperPanda"}

assert_authorization_header
end

end
Expand Down
7 changes: 4 additions & 3 deletions spec/pardot/objects/prospects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ def sample_results
end

it "should take in some arguments" do
fake_get "/api/prospect/version/3/do/query?assigned=true&format=simple&user_key=bar&api_key=my_api_key", sample_results
fake_get "/api/prospect/version/3/do/query?assigned=true&format=simple", sample_results

@client.prospects.query(:assigned => true).should == {"total_results" => 2,
"prospect"=>[
{"last_name"=>"Smith", "first_name"=>"Jim"},
{"last_name"=>"Green", "first_name"=>"Sue"}
]}
assert_authorization_header
end

end
Expand All @@ -50,10 +51,10 @@ def sample_results
end

it "should return the prospect" do
fake_post "/api/prospect/version/3/do/create/email/[email protected]?api_key=my_api_key&user_key=bar&format=simple&first_name=Jim", sample_results
fake_post "/api/prospect/version/3/do/create/email/[email protected]?format=simple&first_name=Jim", sample_results

@client.prospects.create("[email protected]", :first_name => "Jim").should == {"last_name"=>"Smith", "first_name"=>"Jim"}

assert_authorization_header
end

end
Expand Down
7 changes: 4 additions & 3 deletions spec/pardot/objects/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def sample_results
end

it "should take in some arguments" do
fake_get "/api/user/version/3/do/query?api_key=my_api_key&user_key=bar&id_greater_than=200&format=simple", sample_results
fake_get "/api/user/version/3/do/query?id_greater_than=200&format=simple", sample_results

@client.users.query(:id_greater_than => 200).should == {"total_results" => 2,
"user"=>[
{"email"=>"[email protected]", "first_name"=>"Jim"},
{"email"=>"[email protected]", "first_name"=>"Sue"}
]}
assert_authorization_header
end

end
Expand All @@ -53,10 +54,10 @@ def sample_results
end

it "should return the prospect" do
fake_post "/api/user/version/3/do/read/email/[email protected]?api_key=my_api_key&user_key=bar&format=simple", sample_results
fake_post "/api/user/version/3/do/read/email/[email protected]?format=simple", sample_results

@client.users.read_by_email("[email protected]").should == {"email"=>"[email protected]", "first_name"=>"Sue"}

assert_authorization_header
end

end
Expand Down
7 changes: 4 additions & 3 deletions spec/pardot/objects/visitor_activities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def sample_results
end

it "should take in some arguments" do
fake_get "/api/visitorActivity/version/3/do/query?user_key=bar&api_key=my_api_key&id_greater_than=200&format=simple", sample_results
fake_get "/api/visitorActivity/version/3/do/query?id_greater_than=200&format=simple", sample_results

@client.visitor_activities.query(:id_greater_than => 200).should == {"total_results" => 2,
"visitorActivity"=>[
{"type_name"=>"Read", "details"=>"Some details"},
{"type_name"=>"Write", "details"=>"More details"}
]}
assert_authorization_header
end

end
Expand All @@ -53,10 +54,10 @@ def sample_results
end

it "should return the prospect" do
fake_post "/api/visitorActivity/version/3/do/read/id/10?user_key=bar&api_key=my_api_key&format=simple", sample_results
fake_post "/api/visitorActivity/version/3/do/read/id/10?format=simple", sample_results

@client.visitor_activities.read(10).should == {"details"=>"More details", "type_name"=>"Write"}

assert_authorization_header
end

end
Expand Down
7 changes: 4 additions & 3 deletions spec/pardot/objects/visitors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def sample_results
end

it "should take in some arguments" do
fake_get "/api/visitor/version/3/do/query?api_key=my_api_key&user_key=bar&id_greater_than=200&format=simple", sample_results
fake_get "/api/visitor/version/3/do/query?id_greater_than=200&format=simple", sample_results

@client.visitors.query(:id_greater_than => 200).should == {"total_results" => 2,
"visitor"=>[
{"browser"=>"Firefox", "language"=>"en"},
{"browser"=>"Chrome", "language"=>"es"}
]}
assert_authorization_header
end

end
Expand All @@ -53,10 +54,10 @@ def sample_results
end

it "should return the prospect" do
fake_post "/api/visitor/version/3/do/assign/id/10?type=Good&api_key=my_api_key&user_key=bar&format=simple&name=Jim", sample_results
fake_post "/api/visitor/version/3/do/assign/id/10?type=Good&format=simple&name=Jim", sample_results

@client.visitors.assign(10, :name => "Jim", :type => "Good").should == {"browser"=>"Chrome", "language"=>"es"}

assert_authorization_header
end

end
Expand Down
7 changes: 4 additions & 3 deletions spec/pardot/objects/visits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def sample_results
end

it "should take in some arguments" do
fake_get "/api/visit/version/3/do/query?api_key=my_api_key&user_key=bar&id_greater_than=200&format=simple", sample_results
fake_get "/api/visit/version/3/do/query?id_greater_than=200&format=simple", sample_results

@client.visits.query(:id_greater_than => 200).should == {"total_results" => 2,
"visit"=>[
{"duration_in_seconds"=>"50", "visitor_page_view_count"=>"3"},
{"duration_in_seconds"=>"10", "visitor_page_view_count"=>"1"}
]}
assert_authorization_header
end

end
Expand All @@ -53,10 +54,10 @@ def sample_results
end

it "should return the prospect" do
fake_post "/api/visit/version/3/do/read/id/10?user_key=bar&api_key=my_api_key&format=simple", sample_results
fake_post "/api/visit/version/3/do/read/id/10?format=simple", sample_results

@client.visits.read(10).should == {"visitor_page_view_count"=>"1", "duration_in_seconds"=>"10"}

assert_authorization_header
end

end
Expand Down
4 changes: 4 additions & 0 deletions spec/support/fakeweb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ def fake_get path, response
def fake_authenticate client, api_key
client.api_key = api_key
end

def assert_authorization_header
expect(FakeWeb.last_request[:authorization]).to eq('Pardot api_key=my_api_key, user_key=bar')
end

0 comments on commit 418d66f

Please sign in to comment.