Class: Gapic::Rest::ClientStub

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, credentials:, numeric_enums: false, raise_faraday_errors: true) {|Faraday::Connection| ... } ⇒ ClientStub

Initializes with an endpoint and credentials

Parameters:

  • endpoint (String)

    an endpoint for the service that this stub will send requests to

  • 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.

Yields:

  • (Faraday::Connection)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gapic/rest/client_stub.rb', line 45

def initialize endpoint:, credentials:, numeric_enums: false, raise_faraday_errors: true
  @endpoint = endpoint
  @endpoint = "https://#{endpoint}" unless /^https?:/.match? endpoint
  @endpoint = @endpoint.sub %r{/$}, ""

  @credentials = credentials
  @numeric_enums = numeric_enums

  @raise_faraday_errors = raise_faraday_errors

  @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

Instance Method Details

#make_delete_request(uri:, params: {}, options: {}) ⇒ 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)


86
87
88
# File 'lib/gapic/rest/client_stub.rb', line 86

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: {}, options: {}) ⇒ 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)


74
75
76
# File 'lib/gapic/rest/client_stub.rb', line 74

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

#make_patch_request(uri:, body:, params: {}, options: {}) ⇒ 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)


99
100
101
# File 'lib/gapic/rest/client_stub.rb', line 99

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: {}, options: {}) ⇒ 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)


112
113
114
# File 'lib/gapic/rest/client_stub.rb', line 112

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: {}, options: {}) ⇒ 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)


125
126
127
# File 'lib/gapic/rest/client_stub.rb', line 125

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