Skip to content

Commit f439002

Browse files
committed
feat(authentication): add support to use basic auth for icp iam key
1 parent c6a2c6b commit f439002

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/ibm_watson/watson_service.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def initialize(vars)
3939
@password = nil
4040
@token_manager = nil
4141
@temp_headers = nil
42+
@icp_prefix = vars[:password]&.start_with?("icp-") ? true : false
4243

4344
user_agent_string = "watson-apis-ruby-sdk-" + IBMWatson::VERSION
4445
user_agent_string += " #{RbConfig::CONFIG["host"]}"
@@ -62,7 +63,7 @@ def initialize(vars)
6263
if !vars[:iam_access_token].nil? || !vars[:iam_apikey].nil?
6364
_token_manager(iam_apikey: vars[:iam_apikey], iam_access_token: vars[:iam_access_token], iam_url: vars[:iam_url])
6465
elsif !vars[:username].nil? && !vars[:password].nil?
65-
if vars[:username] == "apikey"
66+
if vars[:username] == "apikey" && !@icp_prefix
6667
iam_apikey(iam_apikey: vars[:password])
6768
else
6869
@username = vars[:username]
@@ -117,7 +118,7 @@ def request(args)
117118
args.delete_if { |_, v| v.nil? }
118119
args[:headers].delete("Content-Type") if args.key?(:form) || args[:json].nil?
119120

120-
if @username == "apikey"
121+
if @username == "apikey" && !@icp_prefix
121122
iam_apikey(iam_apikey: @password)
122123
@username = nil
123124
end

test/unit/test_assistant_v1.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,37 @@ def test_plain_to_json
4040
assert_equal(response, service_response.result)
4141
end
4242

43+
def test_plain_to_json_icp
44+
response = {
45+
"text" => "I want financial advice today.",
46+
"created" => "2016-07-11T16:39:01.774Z",
47+
"updated" => "2015-12-07T18:53:59.153Z"
48+
}
49+
headers = {
50+
"Content-Type" => "application/json"
51+
}
52+
stub_request(:post, "https://gateway.watsonplatform.net/assistant/api/v1/workspaces/boguswid/counterexamples?version=2018-02-16")
53+
.with(
54+
body: "{\"text\":\"I want financial advice today.\"}",
55+
headers: {
56+
"Accept" => "application/json",
57+
"Authorization" => "Basic YXBpa2V5OmljcC14eXo=",
58+
"Content-Type" => "application/json",
59+
"Host" => "gateway.watsonplatform.net"
60+
}
61+
).to_return(status: 201, body: response.to_json, headers: headers)
62+
service = IBMWatson::AssistantV1.new(
63+
version: "2018-02-16",
64+
username: "apikey",
65+
password: "icp-xyz"
66+
)
67+
service_response = service.create_counterexample(
68+
workspace_id: "boguswid",
69+
text: "I want financial advice today."
70+
)
71+
assert_equal(response, service_response.result)
72+
end
73+
4374
def test_rate_limit_exceeded
4475
error_code = 429
4576
error_msg = "Rate limit exceeded"

0 commit comments

Comments
 (0)