IBM / ruby-sdk-core

The ruby-sdk-core repository contains core functionality required by Ruby code generated by the IBM OpenAPI SDK Generator.
Apache License 2.0
3 stars 10 forks source link

fix(auth): update headers instead of connector in core library #18

Closed mamoonraja closed 5 years ago

mamoonraja commented 5 years ago

Few things came up during integration tests, this PR fixes issue with IAM auth and updates auth headers instead of the whole connection object. Below is the snippet of Assistant service that I did the integration and unit testing with:

Method:

    def message(workspace_id:, input: nil, intents: nil, entities: nil, alternate_intents: nil, context: nil, output: nil, nodes_visited_details: nil)
      raise ArgumentError.new("workspace_id must be provided") if workspace_id.nil?

      headers = {
      }
      sdk_headers = Common.new.get_sdk_headers("conversation", "V1", "message")
      headers.merge!(sdk_headers)

      params = {
        "version" => @version,
        "nodes_visited_details" => nodes_visited_details
      }

      data = {
        "input" => input,
        "intents" => intents,
        "entities" => entities,
        "alternate_intents" => alternate_intents,
        "context" => context,
        "output" => output
      }

      method_url = "/v1/workspaces/%s/message" % [ERB::Util.url_encode(workspace_id)]

      headers = authenticator.authenticate(headers)
      response = request(
        method: "POST",
        url: method_url,
        headers: headers,
        params: params,
        json: data,
        accept_json: true
      )
      response
    end

Constructor:

    def initialize(args = {})
      @__async_initialized__ = false
      defaults = {}
      defaults[:version] = nil
      defaults[:url] = "https://gateway.watsonplatform.net/assistant/api"
      defaults[:authenticator] = nil
      defaults[:authentication_type] = nil
      args = defaults.merge(args)
      args[:vcap_services_name] = "conversation"
      @version = args[:version]
      super
      raise ArgumentError.new("url must be provided") if @url.nil?
      raise ArgumentError.new("version must be provided") if @version.nil?
    end

Auth:

      authenticator = IBMCloudSdkCore::IamAuthenticator.new(
        apikey: ENV["ASSISTANT_APIKEY"],
        version: "2018-02-16"
      )
      @service = IBMWatson::AssistantV1.new(
        version: "2018-02-16",
        url: ENV["ASSISTANT_URL"],
        authenticator: authenticator
      )
watson-github-bot commented 5 years ago

:tada: This PR is included in version 1.0.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: