File: cognitive_services_credentials.rb

package info (click to toggle)
ruby-ms-rest-azure 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 200 kB
  • sloc: ruby: 906; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# encoding: utf-8
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.

module MsRestAzure
  #
  # Class that provides access to authentication token.
  #
  class CognitiveServicesCredentials < MsRest::ServiceClientCredentials

    private

    # @return [String] the subscription key
    attr_reader :subscription_key

    public

    #
    # Creates and initialize new instance of the CognitiveServicesCredentials class.
    # @param subscription_key [String] subscription key
    def initialize(subscription_key)
      fail ArgumentError, 'Subscription key cannot be nil' if subscription_key.nil?
      fail ArgumentError, 'Subscription key must be of type string' if subscription_key.class.to_s != 'String'
      @subscription_key = subscription_key
    end

    def sign_request(request)
      super(request)

      if (request.respond_to?(:request_headers))
        request.request_headers['Ocp-Apim-Subscription-Key'] = @subscription_key
        request.request_headers['X-BingApis-SDK-Client'] = 'ruby-SDK'
      elsif request.respond_to?(:headers)
        request.headers['Ocp-Apim-Subscription-Key'] = @subscription_key
        request.headers['X-BingApis-SDK-Client'] = 'ruby-SDK'
      else
        fail ArgumentError, 'Incorrect request object was provided'
      end
    end
  end

end