rest-api-client 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/rest/api/client/config.rb +14 -4
- data/lib/rest/api/client/json_parser.rb +18 -14
- data/lib/rest/api/client/logger.rb +10 -0
- data/lib/rest/api/client/request_handler.rb +48 -13
- data/lib/rest/api/client/version.rb +1 -1
- data/lib/rest/api/client.rb +25 -12
- data/lib/rest/api/exceptions/model_errors_exception.rb +11 -0
- data/lib/rest/api/exceptions/unauthorized_exception.rb +11 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77c312be58c3857739624d8ed5d3db9c2216705d
|
4
|
+
data.tar.gz: 6ff9f71b15e12ddda09139dfb7659c9aae731747
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31ab1acf51e92df58ccac8f6c881f935551bb6fc44d9699903b290b3d1f7bd94f984586a46a50e176971ac27ca418a3e08a696461c881ef272c609540522550f
|
7
|
+
data.tar.gz: b4233d76a0a9130df98e261cb1677fcec91bafc231661b22c6cf4300390ab397b89df969aec071236f38b91c8d750e7114f3ed52be7d9073c8059123eba87b79
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
# RestApiClient
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/rest-api-client.svg)](http://badge.fury.io/rb/rest-api-client)
|
4
|
+
|
2
5
|
[![Build Status](https://travis-ci.org/victor0402/rest-api-client.svg?branch=master)](https://travis-ci.org/victor0402/rest-api-client)
|
3
6
|
|
4
7
|
Common classes and methods to handle rest communication
|
@@ -4,7 +4,6 @@ module RestApiClient
|
|
4
4
|
|
5
5
|
# Configuration defaults
|
6
6
|
@config = {
|
7
|
-
:log_level => 'verbose',
|
8
7
|
:service_key => ''
|
9
8
|
}
|
10
9
|
|
@@ -13,17 +12,28 @@ module RestApiClient
|
|
13
12
|
opts.each { |k, v| @config[k.to_sym] = v }
|
14
13
|
end
|
15
14
|
|
15
|
+
# Configure the authorization_key for one client
|
16
|
+
def self.configure_authorization(client_name, auth_key)
|
17
|
+
@config['authorization'] ||= {}
|
18
|
+
@config['authorization'][client_name] = auth_key
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.get_auth_key(client_name)
|
22
|
+
@config['authorization'] ||= {}
|
23
|
+
@config['authorization'][client_name]
|
24
|
+
end
|
25
|
+
|
16
26
|
# Configure through yaml file
|
17
27
|
def self.configure_with(path_to_yaml_file)
|
18
28
|
begin
|
19
29
|
config = YAML::load(IO.read(path_to_yaml_file))
|
30
|
+
configure(config)
|
20
31
|
rescue Errno::ENOENT
|
21
|
-
|
32
|
+
RestApiClient.logger.warn("YAML configuration file couldn't be found. Using defaults.");
|
22
33
|
rescue Psych::SyntaxError
|
23
|
-
|
34
|
+
RestApiClient.logger.warn('YAML configuration file contains invalid syntax. Using defaults.');
|
24
35
|
end
|
25
36
|
|
26
|
-
configure(config)
|
27
37
|
end
|
28
38
|
|
29
39
|
def self.config
|
@@ -3,24 +3,28 @@ require 'json'
|
|
3
3
|
module RestApiClient
|
4
4
|
|
5
5
|
def self.parse_json(json, opts = {})
|
6
|
-
|
6
|
+
begin
|
7
|
+
json_response = JSON.parse json
|
7
8
|
|
8
|
-
|
9
|
+
data_type = opts[:type]
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
json_data = {}
|
12
|
+
if json_response.kind_of?(Hash) && json_response.has_key?('data')
|
13
|
+
json_data = json_response['data']
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
if json_data.kind_of?(Array) && data_type
|
17
|
+
return json_data.map { |data| data_type.new data }
|
18
|
+
elsif json_data.kind_of?(Hash) && data_type
|
19
|
+
return data_type.new json_data
|
20
|
+
else
|
21
|
+
return json_data unless json_data.empty?
|
22
|
+
end
|
23
|
+
return json_response
|
22
24
|
|
23
|
-
|
25
|
+
rescue Exception => e
|
26
|
+
return (opts[:default_return] || nil)
|
27
|
+
end
|
24
28
|
end
|
25
29
|
|
26
30
|
end
|
@@ -1,38 +1,59 @@
|
|
1
1
|
require 'rest-client'
|
2
2
|
require 'json'
|
3
3
|
require 'redis'
|
4
|
+
require 'addressable/uri'
|
4
5
|
|
5
6
|
module RestApiClient
|
6
7
|
|
7
8
|
class RequestsHandler
|
8
9
|
|
9
|
-
def self.perform_get(service_key, path, args = {:params => {}})
|
10
|
-
|
11
|
-
get_response_callback(args).call(response, request, result, &block)
|
12
|
-
}
|
10
|
+
def self.perform_get(service_key, path, args = {:params => {}}, headers = {})
|
11
|
+
self.do_request_without_payload('get', service_key, path, args, headers)
|
13
12
|
end
|
14
13
|
|
15
|
-
def self.
|
16
|
-
|
17
|
-
get_response_callback(args).call(response, request, result, &block)
|
18
|
-
}
|
14
|
+
def self.perform_delete(service_key, path, args = {:params => {}}, headers = {})
|
15
|
+
self.do_request_without_payload('delete', service_key, path, args, headers)
|
19
16
|
end
|
20
17
|
|
21
|
-
def self.
|
22
|
-
|
18
|
+
def self.perform_post(service_key, path, args = {:params => {}}, headers = {})
|
19
|
+
self.do_request_with_payload('post', service_key, path, args, headers)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.perform_put(service_key, path, args = {:params => {}}, headers = {})
|
23
|
+
self.do_request_with_payload('put', service_key, path, args, headers)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.do_request_with_payload(method, service_key, path, args = {:params => {}}, headers = {})
|
27
|
+
headers = treat_header headers, service_key
|
28
|
+
url = get_service_url(service_key) + path
|
29
|
+
|
30
|
+
res = RestClient::Resource.new(url)
|
31
|
+
res.method(method).call(args[:params], headers) { |response, request, result, &block|
|
23
32
|
get_response_callback(args).call(response, request, result, &block)
|
24
33
|
}
|
25
34
|
end
|
26
35
|
|
27
|
-
def self.
|
28
|
-
|
36
|
+
def self.do_request_without_payload(method, service_key, path, args = {:params => {}}, headers = {})
|
37
|
+
headers = treat_header headers, service_key
|
38
|
+
url = get_service_url(service_key) + path + '?' + params_to_query(args[:params])
|
39
|
+
|
40
|
+
res = RestClient::Resource.new(url)
|
41
|
+
res.method(method).call(headers) { |response, request, result, &block|
|
29
42
|
get_response_callback(args).call(response, request, result, &block)
|
30
43
|
}
|
31
44
|
end
|
32
45
|
|
46
|
+
def self.treat_header(headers, service_key)
|
47
|
+
authorization_key = RestApiClient.get_auth_key service_key
|
48
|
+
if authorization_key
|
49
|
+
headers = headers.merge(:Authorization => authorization_key)
|
50
|
+
end
|
51
|
+
headers
|
52
|
+
end
|
53
|
+
|
33
54
|
def self.get_service_url(service_key)
|
34
55
|
redis = Redis.new
|
35
|
-
path = redis.get service_key
|
56
|
+
path = redis.get "#{service_key}.url"
|
36
57
|
raise RestApiClient::ServiceUrlException.new('You must need to set the service key') unless path
|
37
58
|
path << '/' unless path.end_with?('/')
|
38
59
|
path
|
@@ -46,10 +67,24 @@ module RestApiClient
|
|
46
67
|
elsif [301, 302, 307].include? response.code
|
47
68
|
response.follow_redirection(request, result, &block)
|
48
69
|
|
70
|
+
elsif response.code == 401
|
71
|
+
raise RestApiClient::UnauthorizedException.new RestApiClient.parse_json response
|
72
|
+
|
73
|
+
elsif response.code >= 400 && response.code < 500
|
74
|
+
response = RestApiClient.parse_json response
|
75
|
+
message = response.has_key?('message') ? response['message'] : ''
|
76
|
+
raise RestApiClient::ModelErrorsException.new message
|
77
|
+
|
49
78
|
else
|
50
79
|
response.return!(request, result, &block)
|
51
80
|
end
|
52
81
|
end
|
53
82
|
end
|
83
|
+
|
84
|
+
def self.params_to_query(params)
|
85
|
+
uri = Addressable::URI.new
|
86
|
+
uri.query_values = params
|
87
|
+
uri.query || ''
|
88
|
+
end
|
54
89
|
end
|
55
90
|
end
|
data/lib/rest/api/client.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
require 'virtus'
|
2
2
|
require 'rest/api/client/version'
|
3
|
+
require 'rest/api/client/logger'
|
3
4
|
require 'rest/api/client/json_parser'
|
4
5
|
require 'rest/api/client/config'
|
5
6
|
require 'rest/api/exceptions/service_url_exception'
|
7
|
+
require 'rest/api/exceptions/unauthorized_exception'
|
8
|
+
require 'rest/api/exceptions/model_errors_exception'
|
6
9
|
require 'rest/api/client/request_handler'
|
7
10
|
|
8
11
|
module RestApiClient
|
@@ -30,7 +33,9 @@ module RestApiClient
|
|
30
33
|
end
|
31
34
|
|
32
35
|
def save!
|
33
|
-
|
36
|
+
klazz = self.class
|
37
|
+
response = perform_post path, {:type => klazz, :params => {get_model_name => self.attributes}}
|
38
|
+
self.attributes = response && response.is_a?(klazz) ? response.attributes : {}
|
34
39
|
end
|
35
40
|
|
36
41
|
def delete
|
@@ -38,27 +43,35 @@ module RestApiClient
|
|
38
43
|
end
|
39
44
|
|
40
45
|
def update!
|
41
|
-
perform_put "#{path}/#{id}", {:type => self, :params => self.attributes}
|
46
|
+
perform_put "#{path}/#{id}", {:type => self.class, :params => self.attributes}
|
42
47
|
end
|
43
48
|
|
44
|
-
def perform_get(path, args = {})
|
45
|
-
RequestsHandler.perform_get(service_key, path, args)
|
49
|
+
def perform_get(path, args = {}, headers = {})
|
50
|
+
RequestsHandler.perform_get(service_key, path, args, headers)
|
46
51
|
end
|
47
52
|
|
48
|
-
def perform_post(path, args = {})
|
49
|
-
RequestsHandler.perform_post(service_key, path, args)
|
53
|
+
def perform_post(path, args = {}, headers = {})
|
54
|
+
RequestsHandler.perform_post(service_key, path, args, headers)
|
50
55
|
end
|
51
56
|
|
52
|
-
def perform_put(path, args = {})
|
53
|
-
RequestsHandler.perform_put(service_key, path, args)
|
57
|
+
def perform_put(path, args = {}, headers = {})
|
58
|
+
RequestsHandler.perform_put(service_key, path, args, headers)
|
54
59
|
end
|
55
60
|
|
56
|
-
def perform_delete(path, args = {})
|
57
|
-
RequestsHandler.perform_delete(service_key, path, args)
|
61
|
+
def perform_delete(path, args = {}, headers = {})
|
62
|
+
RequestsHandler.perform_delete(service_key, path, args, headers)
|
58
63
|
end
|
59
64
|
|
60
|
-
def self.perform_get(path, args = {})
|
61
|
-
RequestsHandler.perform_get(service_key, path, args)
|
65
|
+
def self.perform_get(path, args = {}, headers = {})
|
66
|
+
RequestsHandler.perform_get(service_key, path, args, headers)
|
67
|
+
end
|
68
|
+
|
69
|
+
def get_model_name
|
70
|
+
self.class.name.split('::').last.downcase
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.get_model_name
|
74
|
+
self.name.split('::').last.downcase
|
62
75
|
end
|
63
76
|
|
64
77
|
# default service_key to instance methods
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module RestApiClient
|
2
|
+
class UnauthorizedException < StandardError
|
3
|
+
|
4
|
+
attr_accessor :errors
|
5
|
+
|
6
|
+
def initialize(errors)
|
7
|
+
super 'You are not allowed to perform this action. Maybe you forgot to pass the authorization token?'
|
8
|
+
@errors = errors
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest-api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -155,9 +155,12 @@ files:
|
|
155
155
|
- lib/rest/api/client.rb
|
156
156
|
- lib/rest/api/client/config.rb
|
157
157
|
- lib/rest/api/client/json_parser.rb
|
158
|
+
- lib/rest/api/client/logger.rb
|
158
159
|
- lib/rest/api/client/request_handler.rb
|
159
160
|
- lib/rest/api/client/version.rb
|
161
|
+
- lib/rest/api/exceptions/model_errors_exception.rb
|
160
162
|
- lib/rest/api/exceptions/service_url_exception.rb
|
163
|
+
- lib/rest/api/exceptions/unauthorized_exception.rb
|
161
164
|
- rest-api-client.gemspec
|
162
165
|
homepage: https://github.com/victor0402/rest-api-client
|
163
166
|
licenses:
|