Class: MsRest2::ServiceClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ms_rest2/service_client.rb

Overview

Class which represents a point of access to the REST API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = nil, options = nil) ⇒ ServiceClient

Creates and initialize new instance of the ServiceClient class.

HTTP requests made by the service client.

Parameters:

  • credentials (MsRest2::ServiceClientCredentials) (defaults to: nil)

    credentials to authorize

  • options (defaults to: nil)

    additional parameters for the HTTP request (not implemented yet).



30
31
32
33
34
35
36
# File 'lib/ms_rest2/service_client.rb', line 30

def initialize(credentials = nil, options = nil)
  @credentials = credentials
  @request_headers = {}
  @middlewares = {middlewares: [[MsRest2::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]]}
  @user_agent_extended = []
  @user_agent_extended.push("ms_rest2/#{MsRest2::VERSION}")
end

Instance Attribute Details

#credentialsMsRest2::ServiceClientCredentials

Returns the credentials object.

Returns:



12
13
14
# File 'lib/ms_rest2/service_client.rb', line 12

def credentials
  @credentials
end

#middlewaresHash{String=>String}

Returns default middlewares configuration for requests.

Returns:

  • (Hash{String=>String})

    default middlewares configuration for requests.



15
16
17
# File 'lib/ms_rest2/service_client.rb', line 15

def middlewares
  @middlewares
end

#request_headersHash{String=>String}

Returns default request headers for requests.

Returns:

  • (Hash{String=>String})

    default request headers for requests.



18
19
20
# File 'lib/ms_rest2/service_client.rb', line 18

def request_headers
  @request_headers
end

#user_agent_extendedArray

Returns strings to be appended to the user agent in the request.

Returns:

  • (Array)

    strings to be appended to the user agent in the request



21
22
23
# File 'lib/ms_rest2/service_client.rb', line 21

def user_agent_extended
  @user_agent_extended
end

Instance Method Details

#add_user_agent_information(additional_user_agent_information) ⇒ Object

Add additional information into User-Agent header. Example:

recommended format is Product/[version]
please refer https://github.com/Azure/azure-sdk-for-ruby/issues/517 for more information.

add_user_agent_information('fog-azure-rm/0.2.0')

Parameters:

  • additional_user_agent_information (String)

    additional product information for user agent string.



71
72
73
# File 'lib/ms_rest2/service_client.rb', line 71

def add_user_agent_information(additional_user_agent_information)
  @user_agent_extended.push(additional_user_agent_information)
end

#make_request_async(base_url, method, path, options = {}) ⇒ Concurrent::Promise

Returns Promise object which holds the HTTP response.

Parameters:

  • base_url (String)

    the base url for the request.

  • method (Symbol)

    with any of the following values :get, :put, :post, :patch, :delete.

  • path (String)

    the path, relative to base_url.

  • options (Hash{String=>String}) (defaults to: {})

    specifying any request options like :credentials, :body, etc.

Returns:

  • (Concurrent::Promise)

    Promise object which holds the HTTP response.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ms_rest2/service_client.rb', line 45

def make_request_async(base_url, method, path, options = {})
  options = @middlewares.merge(options)
  options[:credentials] = options[:credentials] || @credentials
  options[:user_agent_extended] = @user_agent_extended
  request  = MsRest2::HttpOperationRequest.new(base_url, path, method, options)
  promise = request.run_promise do |req|
    options[:credentials].sign_request(req) unless options[:credentials].nil?
  end
  promise = promise.then do |http_response|
    response_content = http_response.body.to_s.empty? ? nil : http_response.body
    # Create response
    create_response(request, http_response, response_content)
  end
  promise.execute
end