class Google::Apis::Core::ResumableUploadCommand
Implementation of the resumable upload protocol
Constants
- BYTES_RECEIVED_HEADER
- QUERY_COMMAND
- RESUMABLE
- START_COMMAND
- STATUS_ACTIVE
- STATUS_CANCELLED
- STATUS_FINAL
- UPLOAD_COMMAND
- UPLOAD_COMMAND_HEADER
- UPLOAD_OFFSET_HEADER
- UPLOAD_STATUS_HEADER
- UPLOAD_URL_HEADER
Public Instance Methods
Source
# File lib/google/apis/core/upload.rb, line 246 def execute_once(client, &block) case @state when :start response = send_start_command(client) result = process_response(response.status_code, response.header, response.body) when :active response = send_query_command(client) result = process_response(response.status_code, response.header, response.body) when :cancelled, :final error(@last_error, rethrow: true, &block) end if @state == :active response = send_upload_command(client) result = process_response(response.status_code, response.header, response.body) end success(result, &block) if @state == :final rescue => e # Some APIs like Youtube generate non-retriable 401 errors and mark # the upload as finalized. Save the error just in case we get # retried. @last_error = e error(e, rethrow: true, &block) end
Execute the upload request once. This will typically perform two HTTP requests – one to initiate or query for the status of the upload, the second to send the (remaining) content.
@private @param [HTTPClient] client
HTTP client
@yield [result, err] Result or error if block supplied @return [Object] @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification @raise [Google::Apis::AuthorizationError] Authorization is required
Source
# File lib/google/apis/core/upload.rb, line 137 def prepare! @state = :start @upload_url = nil @offset = 0 # Prevent the command from populating the body with form encoding, by # asserting that it already has a body. Form encoding is never used # by upload requests. self.body = '' unless self.body super end
Reset upload to initial state.
@return [void] @raise [Google::Apis::ClientError] if upload source is invalid
Google::Apis::Core::BaseUploadCommand#prepare!
Source
# File lib/google/apis/core/upload.rb, line 161 def process_response(status, header, body) @offset = Integer(header[BYTES_RECEIVED_HEADER].first) unless header[BYTES_RECEIVED_HEADER].empty? @upload_url = header[UPLOAD_URL_HEADER].first unless header[UPLOAD_URL_HEADER].empty? upload_status = header[UPLOAD_STATUS_HEADER].first logger.debug { sprintf('Upload status %s', upload_status) } if upload_status == STATUS_ACTIVE @state = :active elsif upload_status == STATUS_FINAL @state = :final elsif upload_status == STATUS_CANCELLED @state = :cancelled fail Google::Apis::ClientError, body end super(status, header, body) end
Check the to see if the upload is complete or needs to be resumed.
@param [Fixnum] status
HTTP status code of response
@param [HTTP::Message::Headers] header
Response headers
@param [String, read] body
Response body
@return [Object]
Response object
@raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification @raise [Google::Apis::AuthorizationError] Authorization is required
Source
# File lib/google/apis/core/upload.rb, line 202 def send_query_command(client) logger.debug { sprintf('Sending upload query command to %s', @upload_url) } request_header = header.dup apply_request_options(request_header) request_header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND client.post(@upload_url, body: '', header: request_header, follow_redirect: true) end
Query for the status of an incomplete upload
@param [HTTPClient] client
HTTP client
@return [HTTP::Message] @raise [Google::Apis::ServerError] Unable to send the request
Source
# File lib/google/apis/core/upload.rb, line 177 def send_start_command(client) logger.debug { sprintf('Sending upload start command to %s', url) } request_header = header.dup apply_request_options(request_header) request_header[UPLOAD_PROTOCOL_HEADER] = RESUMABLE request_header[UPLOAD_COMMAND_HEADER] = START_COMMAND request_header[UPLOAD_CONTENT_LENGTH] = upload_io.size.to_s request_header[UPLOAD_CONTENT_TYPE_HEADER] = upload_content_type client.request(method.to_s.upcase, url.to_s, query: nil, body: body, header: request_header, follow_redirect: true) rescue => e raise Google::Apis::ServerError, e.message end
Source
# File lib/google/apis/core/upload.rb, line 219 def send_upload_command(client) logger.debug { sprintf('Sending upload command to %s', @upload_url) } content = upload_io content.pos = @offset request_header = header.dup apply_request_options(request_header) request_header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND request_header[UPLOAD_COMMAND_HEADER] = UPLOAD_COMMAND request_header[UPLOAD_OFFSET_HEADER] = @offset.to_s request_header[CONTENT_TYPE_HEADER] = upload_content_type client.post(@upload_url, body: content, header: request_header, follow_redirect: true) end
Send the actual content
@param [HTTPClient] client
HTTP client
@return [HTTP::Message] @raise [Google::Apis::ServerError] Unable to send the request