authlete 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e7654558117fbc2aa82e0b85086b663f709d162b
4
- data.tar.gz: 756edef67e1137db22548eafa97d4a5226faa822
3
+ metadata.gz: 77582cfeb5b4bb0276c0e3040a61cdf7c5515fa6
4
+ data.tar.gz: 6229901177d39f4ff62348231699d99a0bc534b5
5
5
  SHA512:
6
- metadata.gz: a782c1b18f97515e1e3afab5688dd55b78a10f2524144b60fa249f0d8a425352abea6207d6e83c177e57a372fa945bb2d64455368c4bcea067d428ce67a349ea
7
- data.tar.gz: a2b5b0dc9ab9477d95011c29d39eaab24fcc178315123c710b643ab19d3e2f9f6a2ade526814afc35c77731409f2990dbbe2295b4eb56c96a115071781e8cde8
6
+ metadata.gz: 20dd499cd219ce97c08c48c5c186deb39a04b9b23e036abf3117f14bc5779008851593764e8e5a0c3abeba39065d5d8a8ba51013d6b8e5d9139d85b81e875b5e
7
+ data.tar.gz: e57907b65b09a2e213a6bc8fff05b9f59d208e8649aeded4640f3b9b23c9ac7a744a4dd7005da47b83fabc590329e672a2f4289c9a4027112df762c00cd11ba4
data/lib/authlete.rb CHANGED
@@ -25,6 +25,7 @@ require 'authlete/version'
25
25
  module Authlete
26
26
  autoload :AuthenticationServer, 'authlete/authentication-server'
27
27
  autoload :Api, 'authlete/api'
28
+ autoload :Exception, 'authlete/exception'
28
29
  autoload :Host, 'authlete/host'
29
30
  autoload :Utility, 'authlete/utility'
30
31
 
data/lib/authlete/api.rb CHANGED
@@ -47,6 +47,8 @@ module Authlete
47
47
  # Extra HTTP headers
48
48
  attr_accessor :extra_headers
49
49
 
50
+ private
51
+
50
52
  # The constructor which takes a hash containing configuration
51
53
  # parameters. Valid configuration parameter names are as follows.
52
54
  #
@@ -64,8 +66,6 @@ module Authlete
64
66
  @service_api_secret = extract_value(config, :service_api_secret)
65
67
  end
66
68
 
67
- private
68
-
69
69
  def call_api(method, path, content_type, payload, user, password)
70
70
  headers = {}
71
71
 
@@ -77,14 +77,14 @@ module Authlete
77
77
  headers.merge!(@extra_headers)
78
78
  end
79
79
 
80
- response = RestClient::Request.new(
80
+ response = execute(
81
81
  :method => method,
82
82
  :url => @host + path,
83
83
  :headers => headers,
84
84
  :payload => payload,
85
85
  :user => user,
86
86
  :password => password
87
- ).execute
87
+ )
88
88
 
89
89
  body = body_as_string(response)
90
90
 
@@ -95,6 +95,61 @@ module Authlete
95
95
  JSON.parse(response.body.to_s, :symbolize_names => true)
96
96
  end
97
97
 
98
+ def execute(parameters)
99
+ begin
100
+ return RestClient::Request.new(parameters).execute
101
+ rescue => e
102
+ raise_api_exception(e)
103
+ end
104
+ end
105
+
106
+ def raise_api_exception(exception)
107
+ message = exception.message
108
+ response = exception.response
109
+
110
+ if response.nil?
111
+ # Raise an error without HTTP response information.
112
+ raise Authlete::Exception.new(:message => message)
113
+ end
114
+
115
+ # Raise an error with HTTP response information.
116
+ raise_api_exception_with_http_response_info(message, response.code, response.body)
117
+ end
118
+
119
+ def raise_api_exception_with_http_response_info(message, status_code, response_body)
120
+ # Parse the response body as a json.
121
+ json = parse_response_body(response_body, message, status_code)
122
+
123
+ # If the json has the HTTP response information from an Authlete API.
124
+ if has_authlete_api_response_info(json)
125
+ # Raise an error with it.
126
+ hash = json.merge!(:statusCode => status_code)
127
+ raise Authlete::Exception.new(hash)
128
+ end
129
+
130
+ # Raise an error with 'status_code' and the original error message.
131
+ raise Authlete::Exception.new(
132
+ :message => message,
133
+ :status_code => status_code
134
+ )
135
+ end
136
+
137
+ def parse_response_body(response_body, message, status_code)
138
+ begin
139
+ return JSON.parse(response_body.to_s, :symbolize_names => true)
140
+ rescue
141
+ # Failed to parse the response body as a json.
142
+ raise Authlete::Exception.new(
143
+ :message => message,
144
+ :status_code => status_code
145
+ )
146
+ end
147
+ end
148
+
149
+ def has_authlete_api_response_info(json)
150
+ json && json.key?(:resultCode) && json.key?(:resultMessage)
151
+ end
152
+
98
153
  def body_as_string(response)
99
154
  if response.body.nil?
100
155
  return nil
@@ -340,7 +395,7 @@ module Authlete
340
395
  # throws <tt>RestClient::Exception</tt>.
341
396
  def introspection(token, scopes = nil, subject = nil)
342
397
  hash = call_api_json_service('/api/auth/introspection',
343
- :token => token, :scopes => scopes, :subject => subject)
398
+ :token => token, :scopes => scopes, :subject => subject)
344
399
 
345
400
  Authlete::Response::IntrospectionResponse.new(hash)
346
401
  end
@@ -0,0 +1,48 @@
1
+ # :nodoc:
2
+ #
3
+ # Copyright (C) 2015 Authlete, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ module Authlete
19
+ class Exception < StandardError
20
+ # The HTTP status code of the error.
21
+ attr_reader :status_code
22
+
23
+ # The code of the result of an Authlete API call.
24
+ attr_reader :result_code
25
+
26
+ # The messasge of the result of an Authlete API call.
27
+ attr_reader :result_message
28
+
29
+ private
30
+
31
+ def initialize(hash = {})
32
+ @message = hash[:message]
33
+ @status_code = hash[:statusCode]
34
+ @result_code = hash[:resultCode]
35
+ @result_message = hash[:resultMessage]
36
+ end
37
+
38
+ public
39
+
40
+ def message
41
+ @result_message || @message || self.class.default_message
42
+ end
43
+
44
+ def self.default_message
45
+ self.name
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module Authlete
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authlete
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiko Kawasaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-25 00:00:00.000000000 Z
11
+ date: 2015-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -69,6 +69,7 @@ files:
69
69
  - lib/authlete.rb
70
70
  - lib/authlete/api.rb
71
71
  - lib/authlete/authentication-server.rb
72
+ - lib/authlete/exception.rb
72
73
  - lib/authlete/host.rb
73
74
  - lib/authlete/model/client-list.rb
74
75
  - lib/authlete/model/client.rb