Skip to content

Commit 418d66f

Browse files
author
Jared Pearson
authored
W-6115601 Update all functions to use Authorization header (pardot#40)
Update calls to use Authorization header instead of query string
1 parent e8cfc06 commit 418d66f

14 files changed

+64
-40
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ruby-pardot (1.2.0)
4+
ruby-pardot (1.3.0)
55
crack (= 0.4.3)
66
httparty (= 0.13.1)
77

@@ -25,7 +25,7 @@ GEM
2525
rspec-expectations (2.5.0)
2626
diff-lcs (~> 1.1.2)
2727
rspec-mocks (2.5.0)
28-
safe_yaml (1.0.4)
28+
safe_yaml (1.0.5)
2929

3030
PLATFORMS
3131
ruby

lib/pardot/http.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module Http
44
def get object, path, params = {}, num_retries = 0
55
smooth_params object, params
66
full_path = fullpath object, path
7-
check_response self.class.get(full_path, :query => params)
7+
headers = create_auth_header object
8+
check_response self.class.get(full_path, :query => params, :headers => headers)
89

910
rescue Pardot::ExpiredApiKeyError => e
1011
handle_expired_api_key :get, object, path, params, num_retries, e
@@ -16,7 +17,8 @@ def get object, path, params = {}, num_retries = 0
1617
def post object, path, params = {}, num_retries = 0, bodyParams = {}
1718
smooth_params object, params
1819
full_path = fullpath object, path
19-
check_response self.class.post(full_path, :query => params, :body => bodyParams)
20+
headers = create_auth_header object
21+
check_response self.class.post(full_path, :query => params, :body => bodyParams, :headers => headers)
2022

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

4143
authenticate unless authenticated?
42-
params.merge! :user_key => @user_key, :api_key => @api_key, :format => @format
44+
params.merge! :format => @format
45+
end
46+
47+
def create_auth_header object
48+
return if object == "login"
49+
{ :Authorization => "Pardot api_key=#{@api_key}, user_key=#{@user_key}" }
4350
end
4451

4552
def check_response http_response

lib/pardot/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Pardot
2-
VERSION = "1.2.0"
2+
VERSION = "1.3.0"
33
end

spec/pardot/http_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get object = "foo", path = "/bar", params = {}
1818
end
1919

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

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

3333
it "should call handle_expired_api_key when the api key expires" do
34-
fake_get "/api/foo/version/3/bar?api_key=my_api_key&format=simple&user_key=bar",
34+
fake_get "/api/foo/version/3/bar?format=simple",
3535
%(?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)
3636

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

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

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

6262
it "should call handle_expired_api_key when the api key expires" do
63-
fake_post "/api/foo/version/3/bar?api_key=my_api_key&format=simple&user_key=bar",
63+
fake_post "/api/foo/version/3/bar?format=simple",
6464
%(?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)
6565

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

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

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

9292
it "should call handle_expired_api_key when the api key expires" do
93-
fake_get "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
93+
fake_get "/api/foo/version/4/bar?format=simple",
9494
%(?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)
9595

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

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

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

122122
it "should call handle_expired_api_key when the api key expires" do
123-
fake_post "/api/foo/version/4/bar?api_key=my_api_key&format=simple&user_key=bar",
123+
fake_post "/api/foo/version/4/bar?format=simple",
124124
%(?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)
125125

126126
@client.should_receive(:handle_expired_api_key)

spec/pardot/objects/emails_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ def sample_response
1919
end
2020

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

2627
it 'should send to a prospect' do
27-
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
28+
fake_post '/api/email/version/3/do/send/prospect_id/42?campaign_id=765&email_template_id=86&format=simple', sample_response
2829
@client.emails.send_to_prospect(42, :campaign_id => 765, :email_template_id => 86).should == {"name" => "My Email"}
30+
assert_authorization_header
2931
end
3032

3133
it 'should send to a list' do
32-
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
34+
fake_post '/api/email/version/3/do/send?email_template_id=200&list_ids[]=235&campaign_id=654&format=simple', sample_response
3335
@client.emails.send_to_list(:email_template_id => 200, 'list_ids[]' => 235, :campaign_id => 654).should == {"name" => "My Email"}
36+
assert_authorization_header
3437
end
3538

3639
end

spec/pardot/objects/lists_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ def sample_results
2727
end
2828

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

3232
@client.lists.query(:id_greater_than => 200).should == {"total_results" => 2,
3333
"list"=>[
3434
{"name"=>"Asdf List"},
3535
{"name"=>"Qwerty List"}
3636
]}
37+
assert_authorization_header
3738
end
3839

3940
end

spec/pardot/objects/opportunities_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ def sample_results
2929
end
3030

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

3434
@client.opportunities.query(:id_greater_than => 200).should == {"total_results" => 2,
3535
"opportunity"=>[
3636
{"type"=>"Great", "name"=>"Jim"},
3737
{"type"=>"Good", "name"=>"Sue"}
3838
]}
39+
assert_authorization_header
3940
end
4041

4142
end
@@ -53,10 +54,11 @@ def sample_results
5354
end
5455

5556
it "should return the prospect" do
56-
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
57+
fake_post "/api/opportunity/version/3/do/create/prospect_email/[email protected]?type=Good&format=simple&name=Jim", sample_results
5758

5859
@client.opportunities.create_by_email("[email protected]", :name => "Jim", :type => "Good").should == {"name"=>"Jim", "type"=>"Good"}
59-
60+
61+
assert_authorization_header
6062
end
6163

6264
end

spec/pardot/objects/prospect_accounts_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ def sample_results
2424
end
2525

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

2929
@client.prospect_accounts.query(:assigned => true).should == {'total_results' => 2,
3030
'prospectAccount'=>[
3131
{'name'=>'Spaceships R Us'},
3232
{'name'=>'Monsters Inc'}
3333
]}
34+
assert_authorization_header
3435
end
3536

3637
end
@@ -47,9 +48,10 @@ def sample_results
4748
end
4849

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

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

5557
end
@@ -67,10 +69,10 @@ def sample_results
6769
end
6870

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

7274
@client.prospect_accounts.create(:name => 'SuperPanda').should == {"name"=>"SuperPanda"}
73-
75+
assert_authorization_header
7476
end
7577

7678
end

spec/pardot/objects/prospects_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ def sample_results
2626
end
2727

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

3131
@client.prospects.query(:assigned => true).should == {"total_results" => 2,
3232
"prospect"=>[
3333
{"last_name"=>"Smith", "first_name"=>"Jim"},
3434
{"last_name"=>"Green", "first_name"=>"Sue"}
3535
]}
36+
assert_authorization_header
3637
end
3738

3839
end
@@ -50,10 +51,10 @@ def sample_results
5051
end
5152

5253
it "should return the prospect" do
53-
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
54+
fake_post "/api/prospect/version/3/do/create/email/[email protected]?format=simple&first_name=Jim", sample_results
5455

5556
@client.prospects.create("[email protected]", :first_name => "Jim").should == {"last_name"=>"Smith", "first_name"=>"Jim"}
56-
57+
assert_authorization_header
5758
end
5859

5960
end

spec/pardot/objects/users_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ def sample_results
2929
end
3030

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

3434
@client.users.query(:id_greater_than => 200).should == {"total_results" => 2,
3535
"user"=>[
3636
{"email"=>"[email protected]", "first_name"=>"Jim"},
3737
{"email"=>"[email protected]", "first_name"=>"Sue"}
3838
]}
39+
assert_authorization_header
3940
end
4041

4142
end
@@ -53,10 +54,10 @@ def sample_results
5354
end
5455

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

5859
@client.users.read_by_email("[email protected]").should == {"email"=>"[email protected]", "first_name"=>"Sue"}
59-
60+
assert_authorization_header
6061
end
6162

6263
end

0 commit comments

Comments
 (0)