Class: Google::APIClient::Request

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/google/api_client/request.rb

Overview

Represents an API request.

Direct Known Subclasses

BatchRequest, Reference, ResumableUpload

Constant Summary collapse

MULTIPART_BOUNDARY =
"-----------RubyApiMultipartPost".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(options = {}) ⇒ Request

Build a request

Parameters:

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

Options Hash (options):

  • :parameters (Hash, Array)

    Request parameters for the API method.

  • :api_method (Google::APIClient::Method)

    API method to invoke. Either :api_method or :uri must be specified

  • :authenticated (TrueClass, FalseClass)

    True if request should include credentials. Implicitly true if unspecified and :authorization present

  • :authorization (#generate_signed_request)

    OAuth credentials

  • :media (Google::APIClient::UploadIO)

    File to upload, if media upload request

  • :body_object (#to_json, #to_hash)

    Main body of the API request. Typically hash or object that can be serialized to JSON

  • :body (#read, #to_str)

    Raw body to send in POST/PUT requests

  • :uri (String, Addressable::URI)

    URI to request. Either :api_method or :uri must be specified

  • :http_method (String, Symbol)

    HTTP method when requesting a URI



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
96
97
98
99
100
101
# File 'lib/google/api_client/request.rb', line 71

def initialize(options={})
  @parameters = Faraday::Utils::ParamsHash.new
  @headers = Faraday::Utils::Headers.new

  self.parameters.merge!(options[:parameters]) unless options[:parameters].nil?
  self.headers.merge!(options[:headers]) unless options[:headers].nil?
  self.api_method = options[:api_method]
  self.authenticated = options[:authenticated]
  self.authorization = options[:authorization]

  # These parameters are handled differently because they're not
  # parameters to the API method, but rather to the API system.
  self.parameters['key'] ||= options[:key] if options[:key]
  self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip]

  if options[:media]
    self.initialize_media_upload(options)
  elsif options[:body]
    self.body = options[:body]
  elsif options[:body_object]
    self.headers['Content-Type'] ||= 'application/json'
    self.body = serialize_body(options[:body_object])
  else
    self.body = ''
  end

  unless self.api_method
    self.http_method = options[:http_method] || 'GET'
    self.uri = options[:uri]
  end
end

Instance Attribute Details

#api_methodGoogle::APIClient::Method

Returns API method to invoke.

Returns:



37
38
39
# File 'lib/google/api_client/request.rb', line 37

def api_method
  @api_method
end

#authenticatedTrueClass, FalseClass

Returns True if request should include credentials.

Returns:

  • (TrueClass, FalseClass)

    True if request should include credentials



43
44
45
# File 'lib/google/api_client/request.rb', line 43

def authenticated
  @authenticated
end

#authorization#generated_authenticated_request

Returns User credentials.

Returns:

  • (#generated_authenticated_request)

    User credentials



41
42
43
# File 'lib/google/api_client/request.rb', line 41

def authorization
  @authorization
end

#body#read, #to_str

Returns Request body.

Returns:

  • (#read, #to_str)

    Request body



45
46
47
# File 'lib/google/api_client/request.rb', line 45

def body
  @body
end

#headersHash (readonly)

Returns Additional HTTP headers.

Returns:

  • (Hash)

    Additional HTTP headers



35
36
37
# File 'lib/google/api_client/request.rb', line 35

def headers
  @headers
end

#http_methodSymbol

Returns HTTP method if invoking a URI.

Returns:

  • (Symbol)

    HTTP method if invoking a URI



111
112
113
# File 'lib/google/api_client/request.rb', line 111

def http_method
  return @http_method ||= self.api_method.http_method.to_s.downcase.to_sym
end

#mediaGoogle::APIClient::UploadIO

Returns File to upload.

Returns:



39
40
41
# File 'lib/google/api_client/request.rb', line 39

def media
  @media
end

#parametersHash (readonly)

Returns Request parameters.

Returns:

  • (Hash)

    Request parameters



33
34
35
# File 'lib/google/api_client/request.rb', line 33

def parameters
  @parameters
end

#upload_typeString (readonly)

Returns protocol used for upload.

Returns:

  • (String)

    protocol used for upload



105
106
107
# File 'lib/google/api_client/request.rb', line 105

def upload_type
  return self.parameters['uploadType'] || self.parameters['upload_type']
end

#uriAddressable::URI

Returns URI to send request.

Returns:

  • (Addressable::URI)

    URI to send request



137
138
139
# File 'lib/google/api_client/request.rb', line 137

def uri
  return @uri ||= self.api_method.generate_uri(self.parameters)
end

Instance Method Details

#process_http_response(response) ⇒ Google::APIClient::Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert HTTP response to an API Result

Parameters:

  • response (Faraday::Response)

    HTTP response

Returns:



259
260
261
# File 'lib/google/api_client/request.rb', line 259

def process_http_response(response)
  Result.new(self, response)
end

#send(connection, is_retry = false) ⇒ Google::APIClient::Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Transmits the request with the given connection

Parameters:

  • connection (Faraday::Connection)

    the connection to transmit with

  • is_retry (TrueValue, FalseValue) (defaults to: false)

    True if request has been previous sent

Returns:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/google/api_client/request.rb', line 158

def send(connection, is_retry = false)
  self.body.rewind if is_retry && self.body.respond_to?(:rewind)
  env = self.to_env(connection)
  logger.debug  { "#{self.class} Sending API request #{env[:method]} #{env[:url].to_s} #{env[:request_headers]}" }
  http_response = connection.app.call(env)
  result = self.process_http_response(http_response)

  logger.debug { "#{self.class} Result: #{result.status} #{result.headers}" }

  # Resumamble slightly different than other upload protocols in that it requires at least
  # 2 requests.
  if result.status == 200 && self.upload_type == 'resumable' && self.media
    upload = result.resumable_upload
    unless upload.complete?
      logger.debug { "#{self.class} Sending upload body" }
      result = upload.send(connection)
    end
  end
  return result
end

#to_env(connection) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares the request for execution, building a hash of parts suitable for sending to Faraday::Connection.

Parameters:

  • connection (Faraday::Connection)

    Connection for building the request

Returns:

  • (Hash)

    Encoded request



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/google/api_client/request.rb', line 231

def to_env(connection)
  method, uri, headers, body = self.to_http_request
  http_request = connection.build_request(method) do |req|
    req.url(uri.to_s)
    req.headers.update(headers)
    req.body = body
  end

  if self.authorization.respond_to?(:generate_authenticated_request)
    http_request = self.authorization.generate_authenticated_request(
      :request => http_request,
      :connection => connection
    )
  end

  http_request.to_env(connection)
end

#to_hashHash

Hashified verison of the API request

Returns:

  • (Hash)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/google/api_client/request.rb', line 202

def to_hash
  options = {}
  if self.api_method
    options[:api_method] = self.api_method
    options[:parameters] = self.parameters
  else
    options[:http_method] = self.http_method
    options[:uri] = self.uri
  end
  options[:headers] = self.headers
  options[:body] = self.body
  options[:media] = self.media
  unless self.authorization.nil?
    options[:authorization] = self.authorization
  end
  return options
end

#to_http_requestArray<(Symbol, Addressable::URI, Hash, [#read,#to_str])>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert to an HTTP request. Returns components in order of method, URI, request headers, and body

Returns:

  • (Array<(Symbol, Addressable::URI, Hash, [#read,#to_str])>)


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/google/api_client/request.rb', line 185

def to_http_request
  request = (
    if self.api_method
      self.api_method.generate_request(self.parameters, self.body, self.headers)
    elsif self.uri
      unless self.parameters.empty?
        self.uri.query = Addressable::URI.form_encode(self.parameters)
      end
      [self.http_method, self.uri.to_s, self.headers, self.body]
    end)
  return request
end