appoxy_api 0.0.3 → 0.0.5

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.
@@ -15,7 +15,7 @@ module Appoxy
15
15
 
16
16
  def verify_signature
17
17
 
18
- if request.put?
18
+ if request.put? || request.post?
19
19
  # We'll extract params from body instead here
20
20
  # todo: maybe check for json format first in case this is a file or something?
21
21
  body = request.body.read
data/lib/api/client.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  module Appoxy
2
2
  module Api
3
3
 
4
+ require 'rest_client'
5
+
4
6
  # Subclass must define:
5
7
  # host: endpoint url for service
6
8
  class Client
@@ -14,20 +16,70 @@ module Appoxy
14
16
  end
15
17
 
16
18
  def get(method, params={}, options={})
17
- parse_response ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params)
19
+ begin
20
+ # ClientHelper.run_http(host, access_key, secret_key, :get, method, nil, params)
21
+ parse_response RestClient.get(append_params(url(method), add_params(method, params)), headers)
22
+ rescue RestClient::BadRequest => ex
23
+ # puts ex.http_body
24
+ raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s
25
+ end
18
26
  end
19
27
 
20
28
  def post(method, params={}, options={})
21
- parse_response ClientHelper.run_http(host, access_key, secret_key, :post, method, nil, params)
29
+ begin
30
+ parse_response RestClient.post(url(method), add_params(method, params).to_json, headers)
31
+ #ClientHelper.run_http(host, access_key, secret_key, :post, method, nil, params)
32
+ rescue RestClient::BadRequest => ex
33
+ # puts ex.http_body
34
+ raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s
35
+ end
36
+
22
37
  end
23
38
 
24
39
  def put(method, body, options={})
25
- parse_response ClientHelper.run_http(host, access_key, secret_key, :put, method, body, nil)
40
+ begin
41
+ parse_response RestClient.put(url(method), add_params(method, body).to_json, headers)
42
+ #ClientHelper.run_http(host, access_key, secret_key, :put, method, body, nil)
43
+ rescue RestClient::BadRequest => ex
44
+ # puts ex.http_body
45
+ raise "Bad Request: " + ActiveSupport::JSON.decode(ex.http_body)["msg"].to_s
46
+ end
47
+ end
48
+
49
+ def url(command_path)
50
+ url = host + command_path
51
+ url
52
+ end
53
+
54
+ def add_params(command_path, hash)
55
+ ts = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
56
+ # puts 'timestamp = ' + ts
57
+ sig = Appoxy::Api::Signatures.generate_signature(command_path, ts, secret_key)
58
+
59
+ extra_params = {'sigv'=>"0.1", 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
60
+ hash.merge!(extra_params)
61
+
62
+ end
63
+
64
+ def append_params(host, params)
65
+ host += "?"
66
+ i = 0
67
+ params.each_pair do |k, v|
68
+ host += "&" if i > 0
69
+ host += k + "=" + CGI.escape(v)
70
+ i+=1
71
+ end
72
+ return host
73
+ end
74
+
75
+ def headers
76
+ user_agent = "Appoxy API Ruby Client"
77
+ headers = {'User-Agent' => user_agent}
26
78
  end
27
79
 
28
80
  def parse_response(response)
29
81
  begin
30
- return ActiveSupport::JSON.decode(response)
82
+ return ActiveSupport::JSON.decode(response.to_s)
31
83
  rescue => ex
32
84
  puts 'response that caused error = ' + response.to_s
33
85
  raise ex
@@ -1,87 +1,10 @@
1
1
  module Appoxy
2
2
  module Api
3
- module ClientHelper
4
-
5
- # TODO: SWAP OUT ALL THIS CRAP AND USE REST-CLIENT INSTEAD
6
-
7
- # body is a hash
8
- def self.run_http(host, access_key, secret_key, http_method, command_path, body=nil, parameters={}, extra_headers=nil)
9
- ts = Appoxy::Api::Signatures.generate_timestamp(Time.now.gmtime)
10
- # puts 'timestamp = ' + ts
11
- sig = Appoxy::Api::Signatures.generate_signature(command_path, ts, secret_key)
12
- # puts "My signature = " + sig
13
- url = host + command_path
14
- # puts url
15
-
16
- user_agent = "Appoxy API Ruby Client"
17
- headers = {'User-Agent' => user_agent}
18
-
19
- if !extra_headers.nil?
20
- extra_headers.each_pair do |k, v|
21
- headers[k] = v
22
- end
23
- end
24
3
 
25
- extra_params = {'sigv'=>"0.1", 'sig' => sig, 'timestamp' => ts, 'access_key' => access_key}
26
- if http_method == :put
27
- body.update(extra_params)
28
- else
29
- parameters = {} if parameters.nil?
30
- parameters.update(extra_params)
31
- # puts 'params=' + parameters.inspect
4
+ require 'rest_client'
32
5
 
33
- end
34
-
35
-
36
- uri = URI.parse(url)
37
- #puts 'body=' + body.to_s
38
- if (http_method == :put)
39
- req = Net::HTTP::Put.new(uri.path)
40
- body = ActiveSupport::JSON.encode(body)
41
- req.body = body unless body.nil?
42
- elsif (http_method == :post)
43
- req = Net::HTTP::Post.new(uri.path)
44
- if !parameters.nil?
45
- req.set_form_data(parameters)
46
- else
47
- req.body = body unless body.nil?
48
- end
49
- elsif (http_method == :delete)
50
- req = Net::HTTP::Delete.new(uri.path)
51
- if !parameters.nil?
52
- req.set_form_data(parameters)
53
- end
54
- else
55
- req = Net::HTTP::Get.new(uri.path)
56
- if !parameters.nil?
57
- req.set_form_data(parameters)
58
- end
59
- end
60
- headers.each_pair do |k, v|
61
- req[k] = v
62
- end
63
- # req.each_header do |k, v|
64
- # puts 'header ' + k + '=' + v
65
- #end
66
- res = Net::HTTP.start(uri.host, uri.port) do |http|
67
- http.request(req)
68
- end
69
-
70
- ret = ''
71
- case res
72
- when Net::HTTPSuccess
73
- # puts 'response body=' + res.body
74
- ret = res.body
75
- when Net::HTTPClientError
76
- raise ClientError.new(res.class.name, ActiveSupport::JSON.decode(res.body))
77
- else
78
- #res.error
79
- puts 'ERROR BODY=' + res.body
80
- raise ServiceError.new(res.class.name, res.body)
81
- end
82
- return ret
6
+ module ClientHelper
83
7
 
84
- end
85
8
 
86
9
  end
87
10
 
@@ -107,4 +30,3 @@ module Appoxy
107
30
  end
108
31
  end
109
32
  end
110
-
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis Reeder
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-08 00:00:00 -07:00
17
+ date: 2010-04-17 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency