Class: Google::APIClient::Request
- Inherits:
-
Object
- Object
- Google::APIClient::Request
- Includes:
- Logging
- Defined in:
- lib/google/api_client/request.rb
Overview
Represents an API request.
Direct Known Subclasses
Constant Summary collapse
- MULTIPART_BOUNDARY =
"-----------RubyApiMultipartPost".freeze
Instance Attribute Summary collapse
-
#api_method ⇒ Google::APIClient::Method
API method to invoke.
-
#authenticated ⇒ TrueClass, FalseClass
True if request should include credentials.
-
#authorization ⇒ #generated_authenticated_request
User credentials.
-
#body ⇒ #read, #to_str
Request body.
-
#headers ⇒ Hash
readonly
Additional HTTP headers.
-
#http_method ⇒ Symbol
HTTP method if invoking a URI.
-
#media ⇒ Google::APIClient::UploadIO
File to upload.
-
#parameters ⇒ Hash
readonly
Request parameters.
-
#upload_type ⇒ String
readonly
Protocol used for upload.
-
#uri ⇒ Addressable::URI
URI to send request.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Request
constructor
Build a request.
-
#process_http_response(response) ⇒ Google::APIClient::Result
private
Convert HTTP response to an API Result.
-
#send(connection, is_retry = false) ⇒ Google::APIClient::Result
private
Transmits the request with the given connection.
-
#to_env(connection) ⇒ Hash
private
Prepares the request for execution, building a hash of parts suitable for sending to Faraday::Connection.
-
#to_hash ⇒ Hash
Hashified verison of the API request.
-
#to_http_request ⇒ Array<(Symbol, Addressable::URI, Hash, [#read,#to_str])>
private
Convert to an HTTP request.
Methods included from Logging
Constructor Details
#initialize(options = {}) ⇒ Request
Build a request
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 102 |
# File 'lib/google/api_client/request.rb', line 72 def initialize(={}) @parameters = Faraday::Utils::ParamsHash.new @headers = Faraday::Utils::Headers.new self.parameters.merge!([:parameters]) unless [:parameters].nil? self.headers.merge!([:headers]) unless [:headers].nil? self.api_method = [:api_method] self.authenticated = [:authenticated] self. = [:authorization] # These parameters are handled differently because they're not # parameters to the API method, but rather to the API system. self.parameters['key'] ||= [:key] if [:key] self.parameters['userIp'] ||= [:user_ip] if [:user_ip] if [:media] self.initialize_media_upload() elsif [:body] self.body = [:body] elsif [:body_object] self.headers['Content-Type'] ||= 'application/json' self.body = serialize_body([:body_object]) else self.body = '' end unless self.api_method self.http_method = [:http_method] || 'GET' self.uri = [:uri] end end |
Instance Attribute Details
#api_method ⇒ Google::APIClient::Method
Returns API method to invoke.
38 39 40 |
# File 'lib/google/api_client/request.rb', line 38 def api_method @api_method end |
#authenticated ⇒ TrueClass, FalseClass
Returns True if request should include credentials.
44 45 46 |
# File 'lib/google/api_client/request.rb', line 44 def authenticated @authenticated end |
#authorization ⇒ #generated_authenticated_request
Returns User credentials.
42 43 44 |
# File 'lib/google/api_client/request.rb', line 42 def @authorization end |
#body ⇒ #read, #to_str
Returns Request body.
46 47 48 |
# File 'lib/google/api_client/request.rb', line 46 def body @body end |
#headers ⇒ Hash (readonly)
Returns Additional HTTP headers.
36 37 38 |
# File 'lib/google/api_client/request.rb', line 36 def headers @headers end |
#http_method ⇒ Symbol
Returns HTTP method if invoking a URI.
112 113 114 |
# File 'lib/google/api_client/request.rb', line 112 def http_method return @http_method ||= self.api_method.http_method.to_s.downcase.to_sym end |
#media ⇒ Google::APIClient::UploadIO
Returns File to upload.
40 41 42 |
# File 'lib/google/api_client/request.rb', line 40 def media @media end |
#parameters ⇒ Hash (readonly)
Returns Request parameters.
34 35 36 |
# File 'lib/google/api_client/request.rb', line 34 def parameters @parameters end |
#upload_type ⇒ String (readonly)
Returns protocol used for upload.
106 107 108 |
# File 'lib/google/api_client/request.rb', line 106 def upload_type return self.parameters['uploadType'] || self.parameters['upload_type'] end |
#uri ⇒ Addressable::URI
Returns URI to send request.
138 139 140 |
# File 'lib/google/api_client/request.rb', line 138 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
260 261 262 |
# File 'lib/google/api_client/request.rb', line 260 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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/google/api_client/request.rb', line 159 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.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/google/api_client/request.rb', line 232 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..respond_to?(:generate_authenticated_request) http_request = self..generate_authenticated_request( :request => http_request, :connection => connection ) end request_env = http_request.to_env(connection) end |
#to_hash ⇒ Hash
Hashified verison of the API request
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/google/api_client/request.rb', line 203 def to_hash = {} if self.api_method [:api_method] = self.api_method [:parameters] = self.parameters else [:http_method] = self.http_method [:uri] = self.uri end [:headers] = self.headers [:body] = self.body [:media] = self.media unless self..nil? [:authorization] = self. end return end |
#to_http_request ⇒ Array<(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
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/google/api_client/request.rb', line 186 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 |