class Gapic::Rest::ClientStub

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

Public Class Methods

new(endpoint:, credentials:, numeric_enums: false) { |connection| ... } click to toggle source

Initializes with an endpoint and credentials @param endpoint [String] an endpoint for the service that this stub will send requests to @param credentials [Google::Auth::Credentials]

Credentials to send with calls in form of a googleauth credentials object.
(see the [googleauth docs](https://googleapis.dev/ruby/googleauth/latest/index.html))

@param numeric_enums [Boolean] Whether to signal the server to JSON-encode enums as ints

@yield [Faraday::Connection]

# File lib/gapic/rest/client_stub.rb, line 39
def initialize endpoint:, credentials:, numeric_enums: false
  @endpoint = endpoint
  @endpoint = "https://#{endpoint}" unless /^https?:/.match? endpoint
  @endpoint = @endpoint.sub %r{/$}, ""

  @credentials = credentials
  @numeric_enums = numeric_enums

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

  yield @connection if block_given?
end

Public Instance Methods

make_delete_request(uri:, params: {}) click to toggle source

Makes a DELETE request

@param uri [String] uri to send this request to @param params [Hash] query string parameters for the request @param options [::Gapic::CallOptions,Hash] gapic options to be applied

to the REST call. Currently only timeout and headers are supported.

@return [Faraday::Response]

# File lib/gapic/rest/client_stub.rb, line 78
def make_delete_request uri:, params: {}, options: {}
  make_http_request :delete, uri: uri, body: nil, params: params, options: options
end
make_get_request(uri:, params: {}) click to toggle source

Makes a GET request

@param uri [String] uri to send this request to @param params [Hash] query string parameters for the request @param options [::Gapic::CallOptions,Hash] gapic options to be applied

to the REST call. Currently only timeout and headers are supported.

@return [Faraday::Response]

# File lib/gapic/rest/client_stub.rb, line 66
def make_get_request uri:, params: {}, options: {}
  make_http_request :get, uri: uri, body: nil, params: params, options: options
end
make_http_request(verb, uri:, body:, params:, options: if @numeric_enums && (!params.key?("$alt") || params["$alt"] == "json")) click to toggle source

@private Sends a http request via Faraday @param verb [Symbol] http verb @param uri [String] uri to send this request to @param body [String, nil] a body to send with the request, nil for requests without a body @param params [Hash] query string parameters for the request @param options [::Gapic::CallOptions,Hash] gapic options to be applied

to the REST call. Currently only timeout and headers are supported.

@return [Faraday::Response]

# File lib/gapic/rest/client_stub.rb, line 131
def make_http_request verb, uri:, body:, params:, options:
  if @numeric_enums && (!params.key?("$alt") || params["$alt"] == "json")
    params = params.merge({ "$alt" => "json;enum-encoding=int" })
  end
make_patch_request(uri:, body:, params: {}) click to toggle source

Makes a PATCH request

@param uri [String] uri to send this request to @param body [String] a body to send with the request, nil for requests without a body @param params [Hash] query string parameters for the request @param options [::Gapic::CallOptions,Hash] gapic options to be applied

to the REST call. Currently only timeout and headers are supported.

@return [Faraday::Response]

# File lib/gapic/rest/client_stub.rb, line 91
def make_patch_request uri:, body:, params: {}, options: {}
  make_http_request :patch, uri: uri, body: body, params: params, options: options
end
make_post_request(uri:, body: nil, params: {}) click to toggle source

Makes a POST request

@param uri [String] uri to send this request to @param body [String] a body to send with the request, nil for requests without a body @param params [Hash] query string parameters for the request @param options [::Gapic::CallOptions,Hash] gapic options to be applied

to the REST call. Currently only timeout and headers are supported.

@return [Faraday::Response]

# File lib/gapic/rest/client_stub.rb, line 104
def make_post_request uri:, body: nil, params: {}, options: {}
  make_http_request :post, uri: uri, body: body, params: params, options: options
end
make_put_request(uri:, body: nil, params: {}) click to toggle source

Makes a PUT request

@param uri [String] uri to send this request to @param body [String] a body to send with the request, nil for requests without a body @param params [Hash] query string parameters for the request @param options [::Gapic::CallOptions,Hash] gapic options to be applied

to the REST call. Currently only timeout and headers are supported.

@return [Faraday::Response]

# File lib/gapic/rest/client_stub.rb, line 117
def make_put_request uri:, body: nil, params: {}, options: {}
  make_http_request :put, uri: uri, body: body, params: params, options: options
end