Skip to content

Commit

Permalink
feat(Auth): update auth methods to use authenticators
Browse files Browse the repository at this point in the history
  • Loading branch information
mamoonraja committed Aug 30, 2019
1 parent c3d8c94 commit 222eafe
Show file tree
Hide file tree
Showing 13 changed files with 452 additions and 365 deletions.
2 changes: 0 additions & 2 deletions lib/ibm_watson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
module IBMWatson
ApiException = IBMCloudSdkCore::ApiException
DetailedResponse = IBMCloudSdkCore::DetailedResponse
IAMTokenManager = IBMCloudSdkCore::IAMTokenManager
ICP4DTokenManager = IBMCloudSdkCore::ICP4DTokenManager

require_relative("./ibm_watson/personality_insights_v3.rb")
require_relative("./ibm_watson/tone_analyzer_v3.rb")
Expand Down
142 changes: 82 additions & 60 deletions lib/ibm_watson/assistant_v1.rb

Large diffs are not rendered by default.

20 changes: 8 additions & 12 deletions lib/ibm_watson/assistant_v2.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright 2018 IBM All Rights Reserved.
# (C) Copyright IBM Corp. 2019.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,21 +82,14 @@ def initialize(args = {})
defaults = {}
defaults[:version] = nil
defaults[:url] = "https://gateway.watsonplatform.net/assistant/api"
defaults[:username] = nil
defaults[:password] = nil
defaults[:iam_apikey] = nil
defaults[:iam_access_token] = nil
defaults[:iam_url] = nil
defaults[:iam_client_id] = nil
defaults[:iam_client_secret] = nil
defaults[:icp4d_access_token] = nil
defaults[:icp4d_url] = nil
defaults[:authenticator] = nil
defaults[:authentication_type] = nil
args = defaults.merge(args)
args[:vcap_services_name] = "conversation"
@version = args[:version]
raise ArgumentError.new("version must be provided") if @version.nil?

args[:display_name] = "Assistant"
super
@version = args[:version]
end

#########################
Expand Down Expand Up @@ -129,6 +122,7 @@ def create_session(assistant_id:)

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

headers = authenticator.authenticate(headers)
response = request(
method: "POST",
url: method_url,
Expand Down Expand Up @@ -167,6 +161,7 @@ def delete_session(assistant_id:, session_id:)

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

headers = authenticator.authenticate(headers)
request(
method: "DELETE",
url: method_url,
Expand Down Expand Up @@ -219,6 +214,7 @@ def message(assistant_id:, session_id:, input: nil, context: nil)

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

headers = authenticator.authenticate(headers)
response = request(
method: "POST",
url: method_url,
Expand Down
39 changes: 21 additions & 18 deletions lib/ibm_watson/compare_comply_v1.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright 2018 IBM All Rights Reserved.
# (C) Copyright IBM Corp. 2019.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -81,40 +81,32 @@ def initialize(args = {})
defaults = {}
defaults[:version] = nil
defaults[:url] = "https://gateway.watsonplatform.net/compare-comply/api"
defaults[:username] = nil
defaults[:password] = nil
defaults[:iam_apikey] = nil
defaults[:iam_access_token] = nil
defaults[:iam_url] = nil
defaults[:iam_client_id] = nil
defaults[:iam_client_secret] = nil
defaults[:icp4d_access_token] = nil
defaults[:icp4d_url] = nil
defaults[:authenticator] = nil
defaults[:authentication_type] = nil
args = defaults.merge(args)
args[:vcap_services_name] = "compare-comply"
@version = args[:version]
raise ArgumentError.new("version must be provided") if @version.nil?

args[:display_name] = "Compare Comply"
super
@version = args[:version]
end

#########################
# HTML conversion
#########################

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

headers = {
Expand All @@ -132,11 +124,11 @@ def convert_to_html(file:, filename: nil, file_content_type: nil, model: nil)
unless file.instance_of?(StringIO) || file.instance_of?(File)
file = file.respond_to?(:to_json) ? StringIO.new(file.to_json) : StringIO.new(file)
end
filename = file.path if filename.nil? && file.respond_to?(:path)
form_data[:file] = HTTP::FormData::File.new(file, content_type: file_content_type.nil? ? "application/octet-stream" : file_content_type, filename: filename)
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)

method_url = "/v1/html_conversion"

headers = authenticator.authenticate(headers)
response = request(
method: "POST",
url: method_url,
Expand Down Expand Up @@ -184,6 +176,7 @@ def classify_elements(file:, file_content_type: nil, model: nil)

method_url = "/v1/element_classification"

headers = authenticator.authenticate(headers)
response = request(
method: "POST",
url: method_url,
Expand Down Expand Up @@ -231,6 +224,7 @@ def extract_tables(file:, file_content_type: nil, model: nil)

method_url = "/v1/tables"

headers = authenticator.authenticate(headers)
response = request(
method: "POST",
url: method_url,
Expand Down Expand Up @@ -291,6 +285,7 @@ def compare_documents(file_1:, file_2:, file_1_content_type: nil, file_2_content

method_url = "/v1/comparison"

headers = authenticator.authenticate(headers)
response = request(
method: "POST",
url: method_url,
Expand Down Expand Up @@ -337,6 +332,7 @@ def add_feedback(feedback_data:, user_id: nil, comment: nil)

method_url = "/v1/feedback"

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

method_url = "/v1/feedback"

headers = authenticator.authenticate(headers)
response = request(
method: "GET",
url: method_url,
Expand Down Expand Up @@ -457,6 +454,7 @@ def get_feedback(feedback_id:, model: nil)

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

headers = authenticator.authenticate(headers)
response = request(
method: "GET",
url: method_url,
Expand Down Expand Up @@ -492,6 +490,7 @@ def delete_feedback(feedback_id:, model: nil)

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

headers = authenticator.authenticate(headers)
response = request(
method: "DELETE",
url: method_url,
Expand Down Expand Up @@ -583,6 +582,7 @@ def create_batch(function:, input_credentials_file:, input_bucket_location:, inp

method_url = "/v1/batches"

headers = authenticator.authenticate(headers)
response = request(
method: "POST",
url: method_url,
Expand Down Expand Up @@ -611,6 +611,7 @@ def list_batches

method_url = "/v1/batches"

headers = authenticator.authenticate(headers)
response = request(
method: "GET",
url: method_url,
Expand Down Expand Up @@ -641,6 +642,7 @@ def get_batch(batch_id:)

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

headers = authenticator.authenticate(headers)
response = request(
method: "GET",
url: method_url,
Expand Down Expand Up @@ -681,6 +683,7 @@ def update_batch(batch_id:, action:, model: nil)

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

headers = authenticator.authenticate(headers)
response = request(
method: "PUT",
url: method_url,
Expand Down
Loading

0 comments on commit 222eafe

Please sign in to comment.