Class: Gapic::Rest::ClientStub

Inherits:
Object
  • Object
show all
Includes:
LoggingConcerns, UniverseDomainConcerns
Defined in:
lib/gapic/rest/client_stub.rb

Overview

A class for making REST calls through Faraday ClientStub's responsibilities:

  • wrap Faraday methods with a bounded explicit interface
  • store service endpoint and create full url for the request
  • store credentials and add auth information to the request

Constant Summary

Constants included from UniverseDomainConcerns

UniverseDomainConcerns::ENDPOINT_SUBSTITUTION

Instance Attribute Summary

Attributes included from LoggingConcerns

#logger

Attributes included from UniverseDomainConcerns

#credentials, #endpoint, #universe_domain

Instance Method Summary collapse

Constructor Details

#initialize(credentials:, endpoint: nil, endpoint_template: nil, universe_domain: nil, numeric_enums: false, raise_faraday_errors: true, logging_system: nil, service_name: nil, logger: :default) {|Faraday::Connection| ... } ⇒ ClientStub

Initializes with an endpoint and credentials

Parameters:

  • endpoint (String) (defaults to: nil)

    The endpoint of the API. Overrides any endpoint_template.

  • endpoint_template (String) (defaults to: nil)

    The endpoint of the API, where the universe domain component of the hostname is marked by the string in the constant UniverseDomainConcerns::ENDPOINT_SUBSTITUTION.

  • universe_domain (String) (defaults to: nil)

    The universe domain in which calls should be made. Defaults to googleapis.com.

  • credentials (Google::Auth::Credentials)

    Credentials to send with calls in form of a googleauth credentials object. (see the googleauth docs)

  • numeric_enums (Boolean) (defaults to: false)

    Whether to signal the server to JSON-encode enums as ints

  • raise_faraday_errors (Boolean) (defaults to: true)

    Whether to raise Faraday errors instead of wrapping them in Gapic::Rest::Error Added for backwards compatibility. Default is true. All REST clients (except for old versions of google-cloud-compute-v1) should explicitly set this parameter to false.

  • logger (Logger, :default, nil) (defaults to: :default)

    An explicit logger to use, or one of the values :default (the default) to construct a default logger, or nil to disable logging explicitly.

Yields:

  • (Faraday::Connection)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gapic/rest/client_stub.rb', line 58

def initialize credentials:,
               endpoint: nil,
               endpoint_template: nil,
               universe_domain: nil,
               numeric_enums: false,
               raise_faraday_errors: true,
               logging_system: nil,
               service_name: nil,
               logger: :default
  setup_universe_domain universe_domain: universe_domain,
                        endpoint: endpoint,
                        endpoint_template: endpoint_template,
                        credentials: credentials

  endpoint_url = self.endpoint
  endpoint_url = "https://#{endpoint_url}" unless /^https?:/.match? endpoint_url
  endpoint_url = endpoint_url.sub %r{/$}, ""

  setup_logging logger: logger,
                system_name: logging_system,
                service: service_name,
                endpoint: endpoint_url,
                client_id: object_id

  @numeric_enums = numeric_enums

  @raise_faraday_errors = raise_faraday_errors

  @connection = Faraday.new url: endpoint_url do |conn|
    conn.headers = { "Content-Type" => "application/json" }
    conn.request :google_authorization, self.credentials unless self.credentials.is_a? ::Symbol
    conn.request :retry
    conn.response :raise_error
    conn.adapter :net_http
  end

  yield @connection if block_given?
end

Instance Method Details

#make_delete_request(uri:, params: {}, options: {}, method_name: nil) ⇒ Faraday::Response

Makes a DELETE request

Parameters:

  • uri (String)

    uri to send this request to

  • params (Hash) (defaults to: {})

    query string parameters for the request

  • options (::Gapic::CallOptions, Hash) (defaults to: {})

    gapic options to be applied to the REST call. Currently only timeout and headers are supported.

Returns:

  • (Faraday::Response)


117
118
119
# File 'lib/gapic/rest/client_stub.rb', line 117

def make_delete_request uri:, params: {}, options: {}, method_name: nil
  make_http_request :delete, uri: uri, body: nil, params: params, options: options, method_name: method_name
end

#make_get_request(uri:, params: {}, options: {}, method_name: nil) ⇒ Faraday::Response

Makes a GET request

Parameters:

  • uri (String)

    uri to send this request to

  • params (Hash) (defaults to: {})

    query string parameters for the request

  • options (::Gapic::CallOptions, Hash) (defaults to: {})

    gapic options to be applied to the REST call. Currently only timeout and headers are supported.

Returns:

  • (Faraday::Response)


105
106
107
# File 'lib/gapic/rest/client_stub.rb', line 105

def make_get_request uri:, params: {}, options: {}, method_name: nil
  make_http_request :get, uri: uri, body: nil, params: params, options: options, method_name: method_name
end

#make_patch_request(uri:, body:, params: {}, options: {}, method_name: nil) ⇒ Faraday::Response

Makes a PATCH request

Parameters:

  • uri (String)

    uri to send this request to

  • body (String)

    a body to send with the request, nil for requests without a body

  • params (Hash) (defaults to: {})

    query string parameters for the request

  • options (::Gapic::CallOptions, Hash) (defaults to: {})

    gapic options to be applied to the REST call. Currently only timeout and headers are supported.

Returns:

  • (Faraday::Response)


130
131
132
# File 'lib/gapic/rest/client_stub.rb', line 130

def make_patch_request uri:, body:, params: {}, options: {}, method_name: nil
  make_http_request :patch, uri: uri, body: body, params: params, options: options, method_name: method_name
end

#make_post_request(uri:, body: nil, params: {}, options: {}, method_name: nil) ⇒ Faraday::Response

Makes a POST request

Parameters:

  • uri (String)

    uri to send this request to

  • body (String) (defaults to: nil)

    a body to send with the request, nil for requests without a body

  • params (Hash) (defaults to: {})

    query string parameters for the request

  • options (::Gapic::CallOptions, Hash) (defaults to: {})

    gapic options to be applied to the REST call. Currently only timeout and headers are supported.

Returns:

  • (Faraday::Response)


143
144
145
# File 'lib/gapic/rest/client_stub.rb', line 143

def make_post_request uri:, body: nil, params: {}, options: {}, method_name: nil
  make_http_request :post, uri: uri, body: body, params: params, options: options, method_name: method_name
end

#make_put_request(uri:, body: nil, params: {}, options: {}, method_name: nil) ⇒ Faraday::Response

Makes a PUT request

Parameters:

  • uri (String)

    uri to send this request to

  • body (String) (defaults to: nil)

    a body to send with the request, nil for requests without a body

  • params (Hash) (defaults to: {})

    query string parameters for the request

  • options (::Gapic::CallOptions, Hash) (defaults to: {})

    gapic options to be applied to the REST call. Currently only timeout and headers are supported.

Returns:

  • (Faraday::Response)


156
157
158
# File 'lib/gapic/rest/client_stub.rb', line 156

def make_put_request uri:, body: nil, params: {}, options: {}, method_name: nil
  make_http_request :put, uri: uri, body: body, params: params, options: options, method_name: method_name
end