sf-username-password-authentication-and-rest-api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/Gemfile +15 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +22 -0
- data/README.md +99 -0
- data/Rakefile +5 -0
- data/lib/salesforce-api.rb +12 -0
- data/lib/salesforce-api/caller.rb +85 -0
- data/lib/salesforce-api/version.rb +3 -0
- data/sf-username-password-authentication-and-rest-api.gemspec +20 -0
- data/test/test_helper.rb +3 -0
- data/test/test_salesforce_api.rb +84 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in sf-username-password-authentication-and-rest-api.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
|
7
|
+
group :development do
|
8
|
+
gem 'guard-minitest'
|
9
|
+
gem 'rb-inotify'
|
10
|
+
end
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'minitest'
|
14
|
+
gem 'mocha'
|
15
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'minitest' do
|
5
|
+
# with Minitest::Unit
|
6
|
+
watch(%r|^test/(.*)\/?test_(.*)\.rb|)
|
7
|
+
watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
8
|
+
watch(%r|^test/test_helper\.rb|) { "test" }
|
9
|
+
|
10
|
+
# with Minitest::Spec
|
11
|
+
# watch(%r|^spec/(.*)_spec\.rb|)
|
12
|
+
# watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
13
|
+
# watch(%r|^spec/spec_helper\.rb|) { "spec" }
|
14
|
+
|
15
|
+
# Rails 3.2
|
16
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/controllers/#{m[1]}_test.rb" }
|
17
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
18
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
19
|
+
|
20
|
+
# Rails
|
21
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/functional/#{m[1]}_test.rb" }
|
22
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
23
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
24
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 David Vallance
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# Purpose
|
2
|
+
|
3
|
+
I wanted a simple easy way to interact with the [Salesforce REST API](http://www.salesforce.com/us/developer/docs/api_rest/index_Left.htm) using their [Username-Password OAuth Authentication Flow](http://www.salesforce.com/us/developer/docs/api_rest/Content/intro_understanding_username_password_oauth_flow.htm).
|
4
|
+
|
5
|
+
# Goal
|
6
|
+
|
7
|
+
1. Handle authorization and re-authorization when the callers session token expires.
|
8
|
+
2. Provide a generic low level method to make API calls, and provide some of the more common calls (so far I have only added a couple which is all I needed).
|
9
|
+
|
10
|
+
# Assumptions
|
11
|
+
|
12
|
+
I try to never have any, I simple expose the REST API calls in a easy manor and return JSON results.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'sf-username-password-authentication-and-rest-api', :git => "git://github.com/dvallance/sf-username-password-authentication-and-rest-api.git", :require => "salesforce-api"
|
19
|
+
|
20
|
+
Require the code if necessary (note: some frameworks like rails are set to auto-require gems for you by default)
|
21
|
+
|
22
|
+
require 'salesforce-api'
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install sf-username-password-authentication-and-rest-api
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
You need to supply your credentials in one of two ways and instantiate a SalesforceAPI::Caller.
|
35
|
+
|
36
|
+
### Version
|
37
|
+
|
38
|
+
You can see a list of available Salesforce Rest API Versions like this:
|
39
|
+
|
40
|
+
SalesforceAPI::versions("na1.salesforce.com") # defaults to this host if not supplied.
|
41
|
+
|
42
|
+
### Credentials from Environmental Values
|
43
|
+
|
44
|
+
# Example of pointing to a sandbox.
|
45
|
+
ENV["SALESFORCE_API_VERSION"] = "26.0"
|
46
|
+
ENV["SALESFORCE_USERNAME"] = "username@mycompany.sandboxname"
|
47
|
+
ENV["SALESFORCE_PASSWORD"] = "password"
|
48
|
+
ENV["SALESFORCE_CLIENT_ID"] = "client_id"
|
49
|
+
ENV["SALESFORCE_CLIENT_SECRET"] = "client_secret"
|
50
|
+
ENV["SALESFORCE_HOST"] = "test.salesforce.com" #pointing to a sandbox
|
51
|
+
|
52
|
+
caller = SalesforceAPI::Caller.new
|
53
|
+
|
54
|
+
### Credentials supplied as Options
|
55
|
+
|
56
|
+
_Note:_ options will be used over ENV variables if both are available.
|
57
|
+
|
58
|
+
caller = SalesforceAPI::Caller.new({
|
59
|
+
api_version: "26.0",
|
60
|
+
username: "username@mycompany.sandboxname",
|
61
|
+
password: "password",
|
62
|
+
client_id: "client_id",
|
63
|
+
client_secret: "client_secret",
|
64
|
+
host: "test.salesforce.com"
|
65
|
+
|
66
|
+
}
|
67
|
+
|
68
|
+
### REST API Examples
|
69
|
+
|
70
|
+
Once you have a SalesforceAPI::Caller instance you can make calls like:
|
71
|
+
|
72
|
+
# get an object by its type and its id.
|
73
|
+
caller.sobject "Communication_Log__c", "a0Ce00000005c9G"
|
74
|
+
caller.sobject "Communication_Log__c", "a0Ce00000005c9G", "Email" # ask for only 1 field instead of all by default
|
75
|
+
|
76
|
+
# get an attachment
|
77
|
+
caller.attachment("Attachment", "00Pe0000000IVTyEAH")
|
78
|
+
|
79
|
+
# running a query to get all attachments that belong to a specific parent
|
80
|
+
caller.query "Select Id, ParentId, OwnerId, Name, Body From Attachment where ParentId = 'a0Ce00000005b9P'"
|
81
|
+
|
82
|
+
# get a specific attachment as a binary/string
|
83
|
+
caller.attachment("Attachment", "00Pe0000000IVTyEAO").class
|
84
|
+
|
85
|
+
Responses are for the most part just JSON which is easily parsed like:
|
86
|
+
|
87
|
+
JSON.parse(response)
|
88
|
+
|
89
|
+
### Want more API Methods?
|
90
|
+
|
91
|
+
This is all I needed for my current project but if there is a demand/need for more let me know I'll add them or better yet contribute!
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
1. Fork it
|
96
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
97
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
98
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
99
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require "active_support/core_ext/object/to_query"
|
4
|
+
require "json"
|
5
|
+
require "active_support/core_ext/hash/indifferent_access"
|
6
|
+
require 'salesforce-api/caller'
|
7
|
+
|
8
|
+
module SalesforceAPI
|
9
|
+
def self.api_versions host = "na1.salesforce.com"
|
10
|
+
JSON.parse(Net::HTTP.get(host, "/services/data"))
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module SalesforceAPI
|
2
|
+
|
3
|
+
class ConnectionException < StandardError; end
|
4
|
+
class ErrorReceivedException < StandardError; end
|
5
|
+
|
6
|
+
class Caller
|
7
|
+
|
8
|
+
attr_reader :api_version, :username, :password, :client_id, :client_secret, :host, :instance_url, :id, :issued_at, :access_token, :signature
|
9
|
+
|
10
|
+
def initialize options = {}
|
11
|
+
@api_version = options[:api_version] ||= ENV["SALESFORCE_API_VERSION"]
|
12
|
+
@username = options[:username] ||= ENV["SALESFORCE_USERNAME"]
|
13
|
+
@password = options[:password] ||= ENV["SALESFORCE_PASSWORD"]
|
14
|
+
@client_id = options[:client_id] ||= ENV["SALESFORCE_CLIENT_ID"]
|
15
|
+
@client_secret = options[:client_secret] ||= ENV["SALESFORCE_CLIENT_SECRET"]
|
16
|
+
@host = options[:host] ||= ENV["SALESFORCE_HOST"]
|
17
|
+
authorize
|
18
|
+
end
|
19
|
+
|
20
|
+
def attachment name, id
|
21
|
+
http_get( URI("#{instance_url}/services/data/v#{api_version}/sobjects/#{name}/#{id}/body")).body
|
22
|
+
end
|
23
|
+
|
24
|
+
def authorize
|
25
|
+
begin
|
26
|
+
parse_authorization_response( Net::HTTP.new(@host, 443).tap { |http|
|
27
|
+
http.use_ssl = true
|
28
|
+
}.post("/services/oauth2/token", token_request_parameters.to_query).body)
|
29
|
+
rescue Errno::ECONNREFUSED
|
30
|
+
raise ConnectionException, "Connection problem, did you supply a host?"
|
31
|
+
end
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def http_get uri, auto_authorize = true
|
36
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
37
|
+
req["Authorization"] = "Bearer #{access_token}"
|
38
|
+
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
|
39
|
+
http.request(req)
|
40
|
+
end
|
41
|
+
|
42
|
+
if response.instance_of?(Net::HTTPUnauthorized) && auto_authorize
|
43
|
+
authorize
|
44
|
+
http_get uri, false
|
45
|
+
else
|
46
|
+
response
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def sobject name, id, fields = ""
|
51
|
+
uri = URI("#{instance_url}/services/data/v#{api_version}/sobjects/#{name}/#{id}")
|
52
|
+
uri.query = {:fields => fields}.to_query unless fields.empty?
|
53
|
+
http_get(uri).body
|
54
|
+
end
|
55
|
+
|
56
|
+
def query query_string
|
57
|
+
uri = URI("#{instance_url}/services/data/v#{api_version}/query")
|
58
|
+
uri.query = "q=#{URI::escape(query_string)}"
|
59
|
+
http_get(uri).body
|
60
|
+
end
|
61
|
+
|
62
|
+
def token_request_parameters
|
63
|
+
@token_request_parameters.nil? ? @token_request_parameters = {
|
64
|
+
:grant_type => "password",
|
65
|
+
:username => @username,
|
66
|
+
:password => @password,
|
67
|
+
:client_id => @client_id,
|
68
|
+
:client_secret => @client_secret
|
69
|
+
}.delete_if{|k,v| v.to_s.empty?} : @token_request_parameters
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def parse_authorization_response json
|
75
|
+
hash = JSON.parse(json)
|
76
|
+
raise ErrorReceivedException, "Authentication error: #{hash["error"]}" if hash["error"]
|
77
|
+
@instance_url = hash["instance_url"]
|
78
|
+
@id = hash["id"]
|
79
|
+
@issued_at = hash["issued_at"]
|
80
|
+
@access_token = hash["access_token"]
|
81
|
+
@signature = hash["signature"]
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'salesforce-api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sf-username-password-authentication-and-rest-api"
|
8
|
+
gem.version = SalesforceAPI::VERSION
|
9
|
+
gem.authors = ["David Vallance"]
|
10
|
+
gem.email = ["dvallance@infotech.com\n"]
|
11
|
+
gem.description = %q{Easy to use Salesforce REST API caller, using the Username-Password OAuth Authentication Flow.}
|
12
|
+
gem.summary = %q{See the homepage for details.}
|
13
|
+
gem.homepage = "https://github.com/dvallance/sf-username-password-authentication-and-rest-api"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency("activesupport")
|
20
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe SalesforceAPI::Caller do
|
4
|
+
|
5
|
+
describe "not supplying any authentication parameters" do
|
6
|
+
|
7
|
+
it "causes a connection exception if Net:Http.post barfs" do
|
8
|
+
Net::HTTP.any_instance.stubs(:post).raises(Errno::ECONNREFUSED)
|
9
|
+
-> {SalesforceAPI::Caller.new()}.must_raise SalesforceAPI::ConnectionException
|
10
|
+
end
|
11
|
+
|
12
|
+
it "causes a error received exception if there is a json error response" do
|
13
|
+
Net::HTTP.any_instance.stubs(:post).returns(stub(:body => '{"error":"unsupported_grant_type"}'))
|
14
|
+
|
15
|
+
-> {SalesforceAPI::Caller.new(:host => "test.salesforce.com" )}.must_raise SalesforceAPI::ErrorReceivedException
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "supplying configuration values" do
|
21
|
+
|
22
|
+
before do
|
23
|
+
SalesforceAPI::Caller.any_instance.stubs(:authorize)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "uses environment variables if no options are supplied" do
|
27
|
+
ENV["SALESFORCE_API_VERSION"] = "version"
|
28
|
+
ENV["SALESFORCE_USERNAME"] = "username"
|
29
|
+
ENV["SALESFORCE_PASSWORD"] = "password"
|
30
|
+
ENV["SALESFORCE_CLIENT_ID"] = "client_id"
|
31
|
+
ENV["SALESFORCE_CLIENT_SECRET"] = "client_secret"
|
32
|
+
ENV["SALESFORCE_HOST"] = "host"
|
33
|
+
|
34
|
+
caller = SalesforceAPI::Caller.new
|
35
|
+
caller.api_version.must_equal "version"
|
36
|
+
caller.username.must_equal "username"
|
37
|
+
caller.password.must_equal "password"
|
38
|
+
caller.client_id.must_equal "client_id"
|
39
|
+
caller.client_secret.must_equal "client_secret"
|
40
|
+
caller.host.must_equal "host"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "uses options over environment variables if they are supplied" do
|
44
|
+
caller = SalesforceAPI::Caller.new({
|
45
|
+
api_version: "opt_version",
|
46
|
+
username: "opt_username",
|
47
|
+
password: "opt_password",
|
48
|
+
client_id: "opt_client_id",
|
49
|
+
client_secret: "opt_client_secret",
|
50
|
+
host: "opt_host"
|
51
|
+
})
|
52
|
+
caller.api_version.must_equal "opt_version"
|
53
|
+
caller.username.must_equal "opt_username"
|
54
|
+
caller.password.must_equal "opt_password"
|
55
|
+
caller.client_id.must_equal "opt_client_id"
|
56
|
+
caller.client_secret.must_equal "opt_client_secret"
|
57
|
+
caller.host.must_equal "opt_host"
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#http_get" do
|
61
|
+
it "should try and authorize if we receive a HTTPUnauthorized response" do
|
62
|
+
caller = SalesforceAPI::Caller.new
|
63
|
+
mock_response = mock()
|
64
|
+
mock_response.expects(:instance_of?).twice.with(Net::HTTPUnauthorized).returns(true,false)
|
65
|
+
Net::HTTP.any_instance.stubs(:start).returns(mock_response)
|
66
|
+
caller.expects(:authorize)
|
67
|
+
uri = URI("https://www.fake.com")
|
68
|
+
response = caller.http_get(uri)
|
69
|
+
response.must_equal mock_response
|
70
|
+
end
|
71
|
+
|
72
|
+
it "won't try and authorize if we explicitly tell it not too" do
|
73
|
+
caller = SalesforceAPI::Caller.new
|
74
|
+
mock_response = stub(:instance_of? => true)
|
75
|
+
Net::HTTP.any_instance.stubs(:start).returns(mock_response)
|
76
|
+
caller.expects(:authorize).never
|
77
|
+
uri = URI("https://www.fake.com")
|
78
|
+
response = caller.http_get(uri,false)
|
79
|
+
response.must_equal mock_response
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sf-username-password-authentication-and-rest-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Vallance
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &19944440 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *19944440
|
25
|
+
description: Easy to use Salesforce REST API caller, using the Username-Password OAuth
|
26
|
+
Authentication Flow.
|
27
|
+
email:
|
28
|
+
- ! 'dvallance@infotech.com
|
29
|
+
|
30
|
+
'
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- Gemfile
|
37
|
+
- Guardfile
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- lib/salesforce-api.rb
|
42
|
+
- lib/salesforce-api/caller.rb
|
43
|
+
- lib/salesforce-api/version.rb
|
44
|
+
- sf-username-password-authentication-and-rest-api.gemspec
|
45
|
+
- test/test_helper.rb
|
46
|
+
- test/test_salesforce_api.rb
|
47
|
+
homepage: https://github.com/dvallance/sf-username-password-authentication-and-rest-api
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.10
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: See the homepage for details.
|
71
|
+
test_files:
|
72
|
+
- test/test_helper.rb
|
73
|
+
- test/test_salesforce_api.rb
|