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
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(={}) @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.
37 38 39 |
# File 'lib/google/api_client/request.rb', line 37 def api_method @api_method end |
#authenticated ⇒ TrueClass, FalseClass
Returns 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.
41 42 43 |
# File 'lib/google/api_client/request.rb', line 41 def @authorization end |
#body ⇒ #read, #to_str
Returns Request body.
45 46 47 |
# File 'lib/google/api_client/request.rb', line 45 def body @body end |
#headers ⇒ Hash (readonly)
Returns Additional HTTP headers.
35 36 37 |
# File 'lib/google/api_client/request.rb', line 35 def headers @headers end |
#http_method ⇒ Symbol
Returns 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 |
#media ⇒ Google::APIClient::UploadIO
Returns File to upload.
39 40 41 |
# File 'lib/google/api_client/request.rb', line 39 def media @media end |
#parameters ⇒ Hash (readonly)
Returns Request parameters.
33 34 35 |
# File 'lib/google/api_client/request.rb', line 33 def parameters @parameters end |
#upload_type ⇒ String (readonly)
Returns 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 |
#uri ⇒ Addressable::URI
Returns 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
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
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.
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..respond_to?(:generate_authenticated_request) http_request = self..generate_authenticated_request( :request => http_request, :connection => connection ) end http_request.to_env(connection) end |
#to_hash ⇒ Hash
Hashified verison of the API request
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 = {} 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
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 |