Skip to content

Commit 222eafe

Browse files
committed
feat(Auth): update auth methods to use authenticators
1 parent c3d8c94 commit 222eafe

13 files changed

Lines changed: 452 additions & 365 deletions

lib/ibm_watson.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
module IBMWatson
77
ApiException = IBMCloudSdkCore::ApiException
88
DetailedResponse = IBMCloudSdkCore::DetailedResponse
9-
IAMTokenManager = IBMCloudSdkCore::IAMTokenManager
10-
ICP4DTokenManager = IBMCloudSdkCore::ICP4DTokenManager
119

1210
require_relative("./ibm_watson/personality_insights_v3.rb")
1311
require_relative("./ibm_watson/tone_analyzer_v3.rb")

lib/ibm_watson/assistant_v1.rb

Lines changed: 82 additions & 60 deletions
Large diffs are not rendered by default.

lib/ibm_watson/assistant_v2.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
# Copyright 2018 IBM All Rights Reserved.
3+
# (C) Copyright IBM Corp. 2019.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -82,21 +82,14 @@ def initialize(args = {})
8282
defaults = {}
8383
defaults[:version] = nil
8484
defaults[:url] = "https://gateway.watsonplatform.net/assistant/api"
85-
defaults[:username] = nil
86-
defaults[:password] = nil
87-
defaults[:iam_apikey] = nil
88-
defaults[:iam_access_token] = nil
89-
defaults[:iam_url] = nil
90-
defaults[:iam_client_id] = nil
91-
defaults[:iam_client_secret] = nil
92-
defaults[:icp4d_access_token] = nil
93-
defaults[:icp4d_url] = nil
85+
defaults[:authenticator] = nil
9486
defaults[:authentication_type] = nil
9587
args = defaults.merge(args)
96-
args[:vcap_services_name] = "conversation"
88+
@version = args[:version]
89+
raise ArgumentError.new("version must be provided") if @version.nil?
90+
9791
args[:display_name] = "Assistant"
9892
super
99-
@version = args[:version]
10093
end
10194

10295
#########################
@@ -129,6 +122,7 @@ def create_session(assistant_id:)
129122

130123
method_url = "/v2/assistants/%s/sessions" % [ERB::Util.url_encode(assistant_id)]
131124

125+
headers = authenticator.authenticate(headers)
132126
response = request(
133127
method: "POST",
134128
url: method_url,
@@ -167,6 +161,7 @@ def delete_session(assistant_id:, session_id:)
167161

168162
method_url = "/v2/assistants/%s/sessions/%s" % [ERB::Util.url_encode(assistant_id), ERB::Util.url_encode(session_id)]
169163

164+
headers = authenticator.authenticate(headers)
170165
request(
171166
method: "DELETE",
172167
url: method_url,
@@ -219,6 +214,7 @@ def message(assistant_id:, session_id:, input: nil, context: nil)
219214

220215
method_url = "/v2/assistants/%s/sessions/%s/message" % [ERB::Util.url_encode(assistant_id), ERB::Util.url_encode(session_id)]
221216

217+
headers = authenticator.authenticate(headers)
222218
response = request(
223219
method: "POST",
224220
url: method_url,

lib/ibm_watson/compare_comply_v1.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
# Copyright 2018 IBM All Rights Reserved.
3+
# (C) Copyright IBM Corp. 2019.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -81,40 +81,32 @@ def initialize(args = {})
8181
defaults = {}
8282
defaults[:version] = nil
8383
defaults[:url] = "https://gateway.watsonplatform.net/compare-comply/api"
84-
defaults[:username] = nil
85-
defaults[:password] = nil
86-
defaults[:iam_apikey] = nil
87-
defaults[:iam_access_token] = nil
88-
defaults[:iam_url] = nil
89-
defaults[:iam_client_id] = nil
90-
defaults[:iam_client_secret] = nil
91-
defaults[:icp4d_access_token] = nil
92-
defaults[:icp4d_url] = nil
84+
defaults[:authenticator] = nil
9385
defaults[:authentication_type] = nil
9486
args = defaults.merge(args)
95-
args[:vcap_services_name] = "compare-comply"
87+
@version = args[:version]
88+
raise ArgumentError.new("version must be provided") if @version.nil?
89+
9690
args[:display_name] = "Compare Comply"
9791
super
98-
@version = args[:version]
9992
end
10093

10194
#########################
10295
# HTML conversion
10396
#########################
10497

10598
##
106-
# @!method convert_to_html(file:, filename: nil, file_content_type: nil, model: nil)
99+
# @!method convert_to_html(file:, file_content_type: nil, model: nil)
107100
# Convert document to HTML.
108101
# Converts a document to HTML.
109102
# @param file [File] The document to convert.
110-
# @param filename [String] The filename for file.
111103
# @param file_content_type [String] The content type of file.
112104
# @param model [String] The analysis model to be used by the service. For the **Element classification**
113105
# and **Compare two documents** methods, the default is `contracts`. For the
114106
# **Extract tables** method, the default is `tables`. These defaults apply to the
115107
# standalone methods as well as to the methods' use in batch-processing requests.
116108
# @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
117-
def convert_to_html(file:, filename: nil, file_content_type: nil, model: nil)
109+
def convert_to_html(file:, file_content_type: nil, model: nil)
118110
raise ArgumentError.new("file must be provided") if file.nil?
119111

120112
headers = {
@@ -132,11 +124,11 @@ def convert_to_html(file:, filename: nil, file_content_type: nil, model: nil)
132124
unless file.instance_of?(StringIO) || file.instance_of?(File)
133125
file = file.respond_to?(:to_json) ? StringIO.new(file.to_json) : StringIO.new(file)
134126
end
135-
filename = file.path if filename.nil? && file.respond_to?(:path)
136-
form_data[:file] = HTTP::FormData::File.new(file, content_type: file_content_type.nil? ? "application/octet-stream" : file_content_type, filename: filename)
127+
form_data[:file] = HTTP::FormData::File.new(file, content_type: file_content_type.nil? ? "application/octet-stream" : file_content_type, filename: file.respond_to?(:path) ? file.path : nil)
137128

138129
method_url = "/v1/html_conversion"
139130

131+
headers = authenticator.authenticate(headers)
140132
response = request(
141133
method: "POST",
142134
url: method_url,
@@ -184,6 +176,7 @@ def classify_elements(file:, file_content_type: nil, model: nil)
184176

185177
method_url = "/v1/element_classification"
186178

179+
headers = authenticator.authenticate(headers)
187180
response = request(
188181
method: "POST",
189182
url: method_url,
@@ -231,6 +224,7 @@ def extract_tables(file:, file_content_type: nil, model: nil)
231224

232225
method_url = "/v1/tables"
233226

227+
headers = authenticator.authenticate(headers)
234228
response = request(
235229
method: "POST",
236230
url: method_url,
@@ -291,6 +285,7 @@ def compare_documents(file_1:, file_2:, file_1_content_type: nil, file_2_content
291285

292286
method_url = "/v1/comparison"
293287

288+
headers = authenticator.authenticate(headers)
294289
response = request(
295290
method: "POST",
296291
url: method_url,
@@ -337,6 +332,7 @@ def add_feedback(feedback_data:, user_id: nil, comment: nil)
337332

338333
method_url = "/v1/feedback"
339334

335+
headers = authenticator.authenticate(headers)
340336
response = request(
341337
method: "POST",
342338
url: method_url,
@@ -364,7 +360,7 @@ def add_feedback(feedback_data:, user_id: nil, comment: nil)
364360
# specified `model_id`. The only permitted value is `contracts`.
365361
# @param model_version [String] An optional string that filters the output to include only feedback with the
366362
# specified `model_version`.
367-
# @param category_removed [String] An optional string in the form of a comma-separated list of categories. If this is
363+
# @param category_removed [String] An optional string in the form of a comma-separated list of categories. If it is
368364
# specified, the service filters the output to include only feedback that has at
369365
# least one category from the list removed.
370366
# @param category_added [String] An optional string in the form of a comma-separated list of categories. If this is
@@ -422,6 +418,7 @@ def list_feedback(feedback_type: nil, before: nil, after: nil, document_title: n
422418

423419
method_url = "/v1/feedback"
424420

421+
headers = authenticator.authenticate(headers)
425422
response = request(
426423
method: "GET",
427424
url: method_url,
@@ -457,6 +454,7 @@ def get_feedback(feedback_id:, model: nil)
457454

458455
method_url = "/v1/feedback/%s" % [ERB::Util.url_encode(feedback_id)]
459456

457+
headers = authenticator.authenticate(headers)
460458
response = request(
461459
method: "GET",
462460
url: method_url,
@@ -492,6 +490,7 @@ def delete_feedback(feedback_id:, model: nil)
492490

493491
method_url = "/v1/feedback/%s" % [ERB::Util.url_encode(feedback_id)]
494492

493+
headers = authenticator.authenticate(headers)
495494
response = request(
496495
method: "DELETE",
497496
url: method_url,
@@ -583,6 +582,7 @@ def create_batch(function:, input_credentials_file:, input_bucket_location:, inp
583582

584583
method_url = "/v1/batches"
585584

585+
headers = authenticator.authenticate(headers)
586586
response = request(
587587
method: "POST",
588588
url: method_url,
@@ -611,6 +611,7 @@ def list_batches
611611

612612
method_url = "/v1/batches"
613613

614+
headers = authenticator.authenticate(headers)
614615
response = request(
615616
method: "GET",
616617
url: method_url,
@@ -641,6 +642,7 @@ def get_batch(batch_id:)
641642

642643
method_url = "/v1/batches/%s" % [ERB::Util.url_encode(batch_id)]
643644

645+
headers = authenticator.authenticate(headers)
644646
response = request(
645647
method: "GET",
646648
url: method_url,
@@ -681,6 +683,7 @@ def update_batch(batch_id:, action:, model: nil)
681683

682684
method_url = "/v1/batches/%s" % [ERB::Util.url_encode(batch_id)]
683685

686+
headers = authenticator.authenticate(headers)
684687
response = request(
685688
method: "PUT",
686689
url: method_url,

0 commit comments

Comments
 (0)