Skip to content

Commit 5b62385

Browse files
authored
Merge pull request #27 from watson-developer-cloud/inherit-base-class
Inherit base class for Watson services
2 parents 0dd91d5 + 7f7b8f6 commit 5b62385

21 files changed

+522
-975
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ discovery = IBMWatson::DiscoveryV1.new(
100100
```ruby
101101
# after instantiation, letting the SDK manage the IAM token
102102
discovery = IBMWatson::DiscoveryV1.new(version: "2017-10-16")
103-
discovery._iam_apikey(iam_apikey: "<iam_apikey>")
103+
discovery.iam_apikey(iam_apikey: "<iam_apikey>")
104104
```
105105

106106
#### Supplying the access token
@@ -115,7 +115,7 @@ discovery = IBMWatson::DiscoveryV1.new(
115115
```ruby
116116
# after instantiation, assuming control of managing IAM token
117117
discovery = IBMWatson::DiscoveryV1.new(version: "2017-10-16")
118-
discovery._iam_access_token(iam_access_token: "<access_token>")
118+
discovery.iam_access_token(iam_access_token: "<access_token>")
119119
```
120120

121121
### Username and password

examples/assistant_v1.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require("json")
55

66
# If using IAM
7-
assistant = AssistantV1(
7+
assistant = IBMWatson::AssistantV1.new(
88
iam_apikey: "IAM API KEY",
99
version: "2018-02-16"
1010
)

examples/tone_analyzer_v3.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require("json")
55

66
# If using IAM
7-
tone_analyzer = ToneAnalyzerV3(
7+
tone_analyzer = IBMWatson::ToneAnalyzerV3.new(
88
iam_apikey: "IAM API KEY",
99
version: "2017-09-21"
1010
)

lib/ibm_watson/assistant_v1.rb

Lines changed: 109 additions & 101 deletions
Large diffs are not rendered by default.

lib/ibm_watson/discovery_v1.rb

Lines changed: 102 additions & 77 deletions
Large diffs are not rendered by default.

lib/ibm_watson/iam_token_manager.rb

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def request(method:, url:, headers: nil, params: nil, data: nil)
4545
)
4646
end
4747
return JSON.parse(response.body.to_s) if (200..299).cover?(response.code)
48+
4849
require_relative("./watson_api_exception.rb")
4950
raise WatsonApiException.new(response: response)
5051
end
@@ -54,17 +55,18 @@ def request(method:, url:, headers: nil, params: nil, data: nil)
5455
# 2. If this class is managing tokens and does not yet have one, make a request for one
5556
# 3. If this class is managing tokens and the token has expired refresh it. In case the refresh token is expired, get a new one
5657
# If this class is managing tokens and has a valid token stored, send it
57-
def _token
58+
def token
5859
return @user_access_token unless @user_access_token.nil? || (@user_access_token.respond_to?(:empty?) && @user_access_token.empty?)
60+
5961
if @token_info.all? { |_k, v| v.nil? }
60-
token_info = _request_token
61-
_save_token_info(
62+
token_info = request_token
63+
save_token_info(
6264
token_info: token_info
6365
)
6466
return @token_info["access_token"]
65-
elsif _is_token_expired?
66-
token_info = _is_refresh_token_expired? ? _request_token : _refresh_token
67-
_save_token_info(
67+
elsif token_expired?
68+
token_info = refresh_token_expired? ? request_token : refresh_token
69+
save_token_info(
6870
token_info: token_info
6971
)
7072
return @token_info["access_token"]
@@ -73,8 +75,10 @@ def _token
7375
end
7476
end
7577

78+
private
79+
7680
# Request an IAM token using an API key
77-
def _request_token
81+
def request_token
7882
headers = {
7983
"Content-Type" => CONTENT_TYPE,
8084
"Authorization" => DEFAULT_AUTHORIZATION,
@@ -95,7 +99,7 @@ def _request_token
9599
end
96100

97101
# Refresh an IAM token using a refresh token
98-
def _refresh_token
102+
def refresh_token
99103
headers = {
100104
"Content-Type" => CONTENT_TYPE,
101105
"Authorization" => DEFAULT_AUTHORIZATION,
@@ -114,23 +118,13 @@ def _refresh_token
114118
response
115119
end
116120

117-
# Set a self-managed IAM access token.
118-
# The access token should be valid and not yet expired.
119-
def _access_token(iam_access_token:)
120-
@user_access_token = iam_access_token
121-
end
122-
123-
# Set the IAM api key
124-
def _iam_apikey(iam_apikey:)
125-
@iam_apikey = iam_apikey
126-
end
127-
128121
# Check if currently stored token is expired.
129122
# Using a buffer to prevent the edge case of the
130123
# token expiring before the request could be made.
131124
# The buffer will be a fraction of the total TTL. Using 80%.
132-
def _is_token_expired?
125+
def token_expired?
133126
return true if @token_info["expiration"].nil? || @token_info["expires_in"].nil?
127+
134128
fraction_of_ttl = 0.8
135129
time_to_live = @token_info["expires_in"].nil? ? 0 : @token_info["expires_in"]
136130
expire_time = @token_info["expiration"].nil? ? 0 : @token_info["expiration"]
@@ -142,16 +136,17 @@ def _is_token_expired?
142136
# Used as a fail-safe to prevent the condition of a refresh token expiring,
143137
# which could happen after around 30 days. This function will return true
144138
# if it has been at least 7 days and 1 hour since the last token was set
145-
def _is_refresh_token_expired?
139+
def refresh_token_expired?
146140
return true if @token_info["expiration"].nil?
141+
147142
seven_days = 7 * 24 * 3600
148143
current_time = Time.now.to_i
149144
new_token_time = @token_info["expiration"] + seven_days
150145
new_token_time < current_time
151146
end
152147

153148
# Save the response from the IAM service request to the object's state
154-
def _save_token_info(token_info:)
149+
def save_token_info(token_info:)
155150
@token_info = token_info
156151
end
157152
end

lib/ibm_watson/language_translator_v3.rb

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
module IBMWatson
3232
##
3333
# The Language Translator V3 service.
34-
class LanguageTranslatorV3
34+
class LanguageTranslatorV3 < WatsonService
3535
include Concurrent::Async
3636
##
3737
# @!method initialize(args)
@@ -72,7 +72,6 @@ class LanguageTranslatorV3
7272
# 'https://iam.ng.bluemix.net/identity/token'.
7373
def initialize(args = {})
7474
@__async_initialized__ = false
75-
super()
7675
defaults = {}
7776
defaults[:version] = nil
7877
defaults[:url] = "https://gateway.watsonplatform.net/language-translator/api"
@@ -82,84 +81,11 @@ def initialize(args = {})
8281
defaults[:iam_access_token] = nil
8382
defaults[:iam_url] = nil
8483
args = defaults.merge(args)
85-
@watson_service = WatsonService.new(
86-
vcap_services_name: "language_translator",
87-
url: args[:url],
88-
username: args[:username],
89-
password: args[:password],
90-
iam_apikey: args[:iam_apikey],
91-
iam_access_token: args[:iam_access_token],
92-
iam_url: args[:iam_url],
93-
use_vcap_services: true
94-
)
84+
args[:vcap_services_name] = "language_translator"
85+
super
9586
@version = args[:version]
9687
end
9788

98-
# :nocov:
99-
def add_default_headers(headers: {})
100-
@watson_service.add_default_headers(headers: headers)
101-
end
102-
103-
def _iam_access_token(iam_access_token:)
104-
@watson_service._iam_access_token(iam_access_token: iam_access_token)
105-
end
106-
107-
def _iam_apikey(iam_apikey:)
108-
@watson_service._iam_apikey(iam_apikey: iam_apikey)
109-
end
110-
111-
# @return [DetailedResponse]
112-
def request(args)
113-
@watson_service.request(args)
114-
end
115-
116-
# @note Chainable
117-
# @param headers [Hash] Custom headers to be sent with the request
118-
# @return [self]
119-
def headers(headers)
120-
@watson_service.headers(headers)
121-
self
122-
end
123-
124-
def password=(password)
125-
@watson_service.password = password
126-
end
127-
128-
def password
129-
@watson_service.password
130-
end
131-
132-
def username=(username)
133-
@watson_service.username = username
134-
end
135-
136-
def username
137-
@watson_service.username
138-
end
139-
140-
def url=(url)
141-
@watson_service.url = url
142-
end
143-
144-
def url
145-
@watson_service.url
146-
end
147-
148-
# @!method configure_http_client(proxy: {}, timeout: {})
149-
# Sets the http client config, currently works with timeout and proxies
150-
# @param proxy [Hash] The hash of proxy configurations
151-
# @option proxy address [String] The address of the proxy
152-
# @option proxy port [Integer] The port of the proxy
153-
# @option proxy username [String] The username of the proxy, if authentication is needed
154-
# @option proxy password [String] The password of the proxy, if authentication is needed
155-
# @option proxy headers [Hash] The headers to be used with the proxy
156-
# @param timeout [Hash] The hash for configuring timeouts. `per_operation` has priority over `global`
157-
# @option timeout per_operation [Hash] Timeouts per operation. Requires `read`, `write`, `connect`
158-
# @option timeout global [Integer] Upper bound on total request time
159-
def configure_http_client(proxy: {}, timeout: {})
160-
@watson_service.configure_http_client(proxy: proxy, timeout: timeout)
161-
end
162-
# :nocov:
16389
#########################
16490
# Translation
16591
#########################
@@ -182,6 +108,7 @@ def configure_http_client(proxy: {}, timeout: {})
182108
# @return [DetailedResponse] A `DetailedResponse` object representing the response.
183109
def translate(text:, model_id: nil, source: nil, target: nil)
184110
raise ArgumentError("text must be provided") if text.nil?
111+
185112
headers = {
186113
}
187114
params = {
@@ -239,6 +166,7 @@ def list_identifiable_languages
239166
# @return [DetailedResponse] A `DetailedResponse` object representing the response.
240167
def identify(text:)
241168
raise ArgumentError("text must be provided") if text.nil?
169+
242170
headers = {
243171
}
244172
params = {
@@ -330,6 +258,7 @@ def list_models(source: nil, target: nil, default_models: nil)
330258
# @return [DetailedResponse] A `DetailedResponse` object representing the response.
331259
def create_model(base_model_id:, name: nil, forced_glossary: nil, parallel_corpus: nil, forced_glossary_filename: nil, parallel_corpus_filename: nil)
332260
raise ArgumentError("base_model_id must be provided") if base_model_id.nil?
261+
333262
headers = {
334263
}
335264
params = {
@@ -382,6 +311,7 @@ def create_model(base_model_id:, name: nil, forced_glossary: nil, parallel_corpu
382311
# @return [DetailedResponse] A `DetailedResponse` object representing the response.
383312
def delete_model(model_id:)
384313
raise ArgumentError("model_id must be provided") if model_id.nil?
314+
385315
headers = {
386316
}
387317
params = {
@@ -408,6 +338,7 @@ def delete_model(model_id:)
408338
# @return [DetailedResponse] A `DetailedResponse` object representing the response.
409339
def get_model(model_id:)
410340
raise ArgumentError("model_id must be provided") if model_id.nil?
341+
411342
headers = {
412343
}
413344
params = {

0 commit comments

Comments
 (0)