appoxy_api 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/api/client.rb +94 -82
- data/lib/api/client_helper.rb +19 -19
- data/lib/api/signatures.rb +17 -17
- data/lib/appoxy_api.rb +2 -3
- metadata +29 -47
- data/lib/api/api_controller.rb +0 -113
data/lib/api/client.rb
CHANGED
@@ -1,82 +1,90 @@
|
|
1
1
|
module Appoxy
|
2
|
-
|
2
|
+
module Api
|
3
3
|
|
4
|
-
|
4
|
+
require 'rest_client'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
# Subclass must define:
|
7
|
+
# host: endpoint url for service
|
8
|
+
class Client
|
9
9
|
|
10
|
-
|
10
|
+
@@logger = Logger.new(STDOUT)
|
11
|
+
@@logger.level = Logger::INFO
|
11
12
|
|
13
|
+
def self.logger
|
14
|
+
@@logger
|
15
|
+
end
|
12
16
|
|
13
|
-
|
14
|
-
@host = host
|
15
|
-
@access_key = access_key
|
16
|
-
@secret_key = secret_key
|
17
|
-
end
|
17
|
+
attr_accessor :host, :access_key, :secret_key
|
18
18
|
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
def initialize(host, access_key, secret_key, options={})
|
21
|
+
@host = host
|
22
|
+
@access_key = access_key
|
23
|
+
@secret_key = secret_key
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def get(method, params={}, options={})
|
28
|
+
begin
|
22
29
|
# ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params)
|
23
|
-
|
24
|
-
|
30
|
+
parse_response RestClient.get(append_params(url(method), add_params(method, params)), headers), options
|
31
|
+
rescue RestClient::BadRequest => ex
|
25
32
|
# puts ex.http_body
|
26
|
-
|
27
|
-
|
28
|
-
|
33
|
+
raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
29
37
|
|
30
38
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
39
|
+
def post(method, params={}, options={})
|
40
|
+
begin
|
41
|
+
parse_response RestClient.post(url(method), add_params(method, params).to_json, headers), options
|
42
|
+
#ClientHelper.run_http(host, access_key, secret_key, :post, method, nil, params)
|
43
|
+
rescue RestClient::BadRequest => ex
|
36
44
|
# puts ex.http_body
|
37
|
-
|
38
|
-
|
45
|
+
raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s
|
46
|
+
end
|
39
47
|
|
40
|
-
|
48
|
+
end
|
41
49
|
|
42
50
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
51
|
+
def put(method, body, options={})
|
52
|
+
begin
|
53
|
+
parse_response RestClient.put(url(method), add_params(method, body).to_json, headers), options
|
54
|
+
#ClientHelper.run_http(host, access_key, secret_key, :put, method, body, nil)
|
55
|
+
rescue RestClient::BadRequest => ex
|
48
56
|
# puts ex.http_body
|
49
|
-
|
50
|
-
|
51
|
-
|
57
|
+
raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s
|
58
|
+
end
|
59
|
+
end
|
52
60
|
|
53
61
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
62
|
+
def delete(method, params={}, options={})
|
63
|
+
begin
|
64
|
+
parse_response RestClient.delete(append_params(url(method), add_params(method, params))), options
|
65
|
+
rescue RestClient::BadRequest => ex
|
66
|
+
raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s
|
67
|
+
end
|
68
|
+
end
|
61
69
|
|
62
70
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
71
|
+
def url(command_path)
|
72
|
+
url = host + command_path
|
73
|
+
url
|
74
|
+
end
|
67
75
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
76
|
+
# old way
|
77
|
+
def add_params(command_path, hash)
|
78
|
+
ts = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
|
79
|
+
# puts 'timestamp = ' + ts
|
80
|
+
sig = Appoxy::Api::Signatures.generate_signature(command_path, ts, secret_key)
|
73
81
|
|
74
|
-
|
75
|
-
|
82
|
+
extra_params = {'sigv'=>"0.1", 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
|
83
|
+
hash.merge!(extra_params)
|
76
84
|
|
77
|
-
|
85
|
+
end
|
78
86
|
|
79
|
-
|
87
|
+
# new way
|
80
88
|
# def add_params(command_path, hash)
|
81
89
|
# ts = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
|
82
90
|
# #p "hash_to s" + command_path + Appoxy::Api::Signatures.hash_to_s(hash)
|
@@ -87,35 +95,39 @@ module Appoxy
|
|
87
95
|
# end
|
88
96
|
|
89
97
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
98
|
+
def append_params(host, params)
|
99
|
+
host += "?"
|
100
|
+
i = 0
|
101
|
+
params.each_pair do |k, v|
|
102
|
+
host += "&" if i > 0
|
103
|
+
host += k + "=" + CGI.escape(v)
|
104
|
+
i +=1
|
105
|
+
end
|
106
|
+
return host
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
def headers
|
111
|
+
user_agent = "Appoxy API Ruby Client"
|
112
|
+
headers = {'User-Agent' => user_agent}
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
def parse_response(response, options={})
|
117
|
+
unless options[:parse] == false
|
118
|
+
begin
|
119
|
+
return ActiveSupport::JSON.decode(response.to_s)
|
120
|
+
rescue => ex
|
121
|
+
puts 'response that caused error = ' + response.to_s
|
122
|
+
raise ex
|
123
|
+
end
|
124
|
+
else
|
125
|
+
response
|
118
126
|
end
|
127
|
+
end
|
128
|
+
|
119
129
|
|
120
130
|
end
|
131
|
+
|
132
|
+
end
|
121
133
|
end
|
data/lib/api/client_helper.rb
CHANGED
@@ -1,32 +1,32 @@
|
|
1
1
|
module Appoxy
|
2
|
-
|
2
|
+
module Api
|
3
3
|
|
4
|
-
|
4
|
+
require 'rest_client'
|
5
5
|
|
6
|
-
|
6
|
+
module ClientHelper
|
7
7
|
|
8
8
|
|
9
|
-
|
9
|
+
end
|
10
10
|
|
11
|
-
|
11
|
+
class ClientError < StandardError
|
12
12
|
|
13
|
-
|
13
|
+
attr_reader :response_hash
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
def initialize(class_name, response_hash)
|
16
|
+
puts 'response-hash=' + response_hash.inspect
|
17
|
+
super("#{class_name} - #{response_hash["msg"]}")
|
18
|
+
@response_hash = response_hash
|
19
|
+
end
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
class ServiceError < StandardError
|
23
|
+
attr_reader :body
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def initialize(class_name, body)
|
26
|
+
super("#{class_name}")
|
27
|
+
@body = body
|
28
28
|
|
29
|
-
|
30
|
-
end
|
29
|
+
end
|
31
30
|
end
|
31
|
+
end
|
32
32
|
end
|
data/lib/api/signatures.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
module Appoxy
|
2
|
-
|
3
|
-
|
2
|
+
module Api
|
3
|
+
module Signatures
|
4
4
|
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def self.generate_timestamp(gmtime)
|
7
|
+
return gmtime.strftime("%Y-%m-%dT%H:%M:%SZ")
|
8
|
+
end
|
9
9
|
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
def self.generate_signature(operation, timestamp, secret_key)
|
12
|
+
my_sha_hmac = Digest::HMAC.digest(operation + timestamp, secret_key, Digest::SHA1)
|
13
|
+
my_b64_hmac_digest = Base64.encode64(my_sha_hmac).strip
|
14
|
+
return my_b64_hmac_digest
|
15
|
+
end
|
16
16
|
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
18
|
+
def self.hash_to_s(hash)
|
19
|
+
str = ""
|
20
|
+
hash.each_pair { |key, value| str+= "#{key}#{value}" }
|
21
|
+
#removing all characters that could differ after parsing with rails
|
22
|
+
return str.delete "\"\/:{}[]\' T"
|
23
|
+
end
|
25
24
|
end
|
25
|
+
end
|
26
26
|
end
|
data/lib/appoxy_api.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
require 'active_support'
|
1
|
+
require 'active_support/core_ext'
|
2
2
|
require 'digest/hmac'
|
3
3
|
require 'net/http'
|
4
4
|
require 'base64'
|
5
|
-
|
6
|
-
require File.join(File.dirname(__FILE__), "api", "api_controller")
|
5
|
+
|
7
6
|
require File.join(File.dirname(__FILE__), "api", "client_helper")
|
8
7
|
require File.join(File.dirname(__FILE__), "api", "signatures")
|
9
8
|
require File.join(File.dirname(__FILE__), "api", "client")
|
metadata
CHANGED
@@ -1,45 +1,35 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: appoxy_api
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 7
|
9
|
-
version: 0.0.7
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Travis Reeder
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2010-12-29 00:00:00 -08:00
|
12
|
+
date: 2011-02-21 00:00:00.000000000 -08:00
|
18
13
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
21
16
|
name: rest-client
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: &26585748 !ruby/object:Gem::Requirement
|
24
18
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
- 0
|
30
|
-
version: "0"
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
31
23
|
type: :runtime
|
32
|
-
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *26585748
|
33
26
|
description: Appoxy API Helper gem description...
|
34
27
|
email: travis@appoxy.com
|
35
28
|
executables: []
|
36
|
-
|
37
29
|
extensions: []
|
38
|
-
|
39
|
-
extra_rdoc_files:
|
30
|
+
extra_rdoc_files:
|
40
31
|
- README.markdown
|
41
|
-
files:
|
42
|
-
- lib/api/api_controller.rb
|
32
|
+
files:
|
43
33
|
- lib/api/client.rb
|
44
34
|
- lib/api/client_helper.rb
|
45
35
|
- lib/api/signatures.rb
|
@@ -48,34 +38,26 @@ files:
|
|
48
38
|
has_rdoc: true
|
49
39
|
homepage: http://www.appoxy.com
|
50
40
|
licenses: []
|
51
|
-
|
52
41
|
post_install_message:
|
53
42
|
rdoc_options: []
|
54
|
-
|
55
|
-
require_paths:
|
43
|
+
require_paths:
|
56
44
|
- lib
|
57
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
46
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
|
64
|
-
version: "0"
|
65
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
52
|
none: false
|
67
|
-
requirements:
|
68
|
-
- -
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
|
71
|
-
- 0
|
72
|
-
version: "0"
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
73
57
|
requirements: []
|
74
|
-
|
75
58
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.
|
59
|
+
rubygems_version: 1.5.2
|
77
60
|
signing_key:
|
78
61
|
specification_version: 3
|
79
62
|
summary: Appoxy API Helper gem
|
80
63
|
test_files: []
|
81
|
-
|
data/lib/api/api_controller.rb
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
module Appoxy
|
2
|
-
|
3
|
-
module Api
|
4
|
-
|
5
|
-
# The api controllers that use this should set:
|
6
|
-
# protect_from_forgery :only => [] # can add methods to here, eg: :create, :update, :destroy
|
7
|
-
|
8
|
-
# rescue_from SigError, :with => :send_error
|
9
|
-
# rescue_from Api::ApiError, :with => :send_error
|
10
|
-
# before_filter :verify_signature(params)
|
11
|
-
|
12
|
-
# Your Controller must define a secret_key_for_signature method which will return the secret key to use to generate signature.
|
13
|
-
|
14
|
-
module ApiController
|
15
|
-
|
16
|
-
def verify_signature
|
17
|
-
params2 = nil
|
18
|
-
if request.put? || request.post?
|
19
|
-
# We'll extract params from body instead here
|
20
|
-
# todo: maybe check for json format first in case this is a file or something?
|
21
|
-
body = request.body.read
|
22
|
-
puts 'body=' + body.inspect
|
23
|
-
params2 = ActiveSupport::JSON.decode(body)
|
24
|
-
puts 'params2=' + params2.inspect
|
25
|
-
params.merge! params2
|
26
|
-
end
|
27
|
-
|
28
|
-
#operation = "#{controller_name}/#{action_name}"
|
29
|
-
#operation = request.env["PATH_INFO"].gsub(/\/api\//, "")# here we're getting original request url'
|
30
|
-
|
31
|
-
# #getting clean params (without parsed via routes)
|
32
|
-
# params_for_signature = params2||request.query_parameters
|
33
|
-
# #removing mandatory params
|
34
|
-
# params_for_signature = params_for_signature.delete_if {|key, value| ["access_key", "sigv", "sig", "timestamp"].include? key}
|
35
|
-
|
36
|
-
|
37
|
-
#puts "params " +operation+Appoxy::Api::Signatures.hash_to_s(params_for_signature)
|
38
|
-
access_key = params["access_key"]
|
39
|
-
sigv = params["sigv"]
|
40
|
-
timestamp = params["timestamp"]
|
41
|
-
sig = params["sig"]
|
42
|
-
signature = ""
|
43
|
-
case sigv
|
44
|
-
when "0.1"
|
45
|
-
puts "outdated version of client"
|
46
|
-
signature = "#{controller_name}/#{action_name}"
|
47
|
-
when "0.2"
|
48
|
-
puts "new version of client"
|
49
|
-
operation = request.env["PATH_INFO"].gsub(/\/api\//, "")# here we're getting original request url'
|
50
|
-
params_for_signature = params2||request.query_parameters
|
51
|
-
params_for_signature = params_for_signature.delete_if {|key, value| ["access_key", "sigv", "sig", "timestamp"].include? key}
|
52
|
-
signature = operation+Appoxy::Api::Signatures.hash_to_s(params_for_signature)
|
53
|
-
end
|
54
|
-
# puts "signature " + signature
|
55
|
-
raise Appoxy::Api::ApiError, "No access_key" if access_key.nil?
|
56
|
-
raise Appoxy::Api::ApiError, "No sigv" if sigv.nil?
|
57
|
-
raise Appoxy::Api::ApiError, "No timestamp" if timestamp.nil?
|
58
|
-
raise Appoxy::Api::ApiError, "No sig" if sig.nil?
|
59
|
-
timestamp2 = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
|
60
|
-
raise Appoxy::Api::ApiError, "Request timed out!" unless (Time.parse(timestamp2)-Time.parse(timestamp))<60 # deny all requests older than 60 seconds
|
61
|
-
sig2 = Appoxy::Api::Signatures.generate_signature(signature, timestamp, secret_key_for_signature(access_key))
|
62
|
-
raise Appoxy::Api::ApiError, "Invalid signature!" unless sig == sig2
|
63
|
-
|
64
|
-
puts 'Signature OK'
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
def sig_should
|
70
|
-
raise "You didn't define a sig_should method in your controller!"
|
71
|
-
end
|
72
|
-
|
73
|
-
|
74
|
-
def send_ok(msg={})
|
75
|
-
response_as_string = '' # in case we want to add debugging or something
|
76
|
-
# respond_to do |format|
|
77
|
-
# format.json { render :json=>msg }
|
78
|
-
# response_as_string = render_to_string :json => msg
|
79
|
-
render :json => msg
|
80
|
-
# end
|
81
|
-
true
|
82
|
-
end
|
83
|
-
|
84
|
-
|
85
|
-
def send_error(statuscode_or_error, msg=nil)
|
86
|
-
exc = nil
|
87
|
-
if statuscode_or_error.is_a? Exception
|
88
|
-
exc = statuscode_or_error
|
89
|
-
statuscode_or_error = 400
|
90
|
-
msg = exc.message
|
91
|
-
end
|
92
|
-
# deprecate status, should use status_code
|
93
|
-
json_msg = {"status_code"=>statuscode_or_error, "msg"=>msg}
|
94
|
-
render :json=>json_msg, :status=>statuscode_or_error
|
95
|
-
true
|
96
|
-
end
|
97
|
-
|
98
|
-
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
class ApiError < StandardError
|
103
|
-
|
104
|
-
def initialize(msg=nil)
|
105
|
-
super(msg)
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|