1 # Copyright 2013 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 # Handles an API result.
20 # Wraps around the Google::APIClient::Result class, making it easier to
21 # handle the result (e.g. pagination) and keeping it in line with the rest
22 # of the Service programming interface.
29 # @param [Google::APIClient::Service::Request] request
30 # The original request
31 # @param [Google::APIClient::Result] base_result
32 # The base result to be wrapped
33 def initialize(request, base_result)
35 @base_result = base_result
38 # @!attribute [r] status
39 # @return [Fixnum] HTTP status code
40 # @!attribute [r] headers
41 # @return [Hash] HTTP response headers
42 # @!attribute [r] body
43 # @return [String] HTTP response body
44 def_delegators :@base_result, :status, :headers, :body
46 # @return [Google::APIClient::Service::Request] Original request object
50 # Get the content type of the response
51 # @!attribute [r] media_type
53 # Value of content-type header
54 def_delegators :@base_result, :media_type
57 # Check if request failed
59 # @!attribute [r] error?
60 # @return [TrueClass, FalseClass]
61 # true if result of operation is an error
62 def_delegators :@base_result, :error?
65 # Check if request was successful
67 # @!attribute [r] success?
68 # @return [TrueClass, FalseClass]
69 # true if result of operation was successful
70 def_delegators :@base_result, :success?
73 # Extracts error messages from the response body
75 # @!attribute [r] error_message
77 # error message, if available
78 def_delegators :@base_result, :error_message
81 # Check for parsable data in response
83 # @!attribute [r] data?
84 # @return [TrueClass, FalseClass]
85 # true if body can be parsed
86 def_delegators :@base_result, :data?
89 # Return parsed version of the response body.
91 # @!attribute [r] data
92 # @return [Object, Hash, String]
93 # Object if body parsable from API schema, Hash if JSON, raw body if unable to parse
94 def_delegators :@base_result, :data
97 # Pagination scheme used by this request/response
99 # @!attribute [r] pagination_type
101 # currently always :token
102 def_delegators :@base_result, :pagination_type
105 # Name of the field that contains the pagination token
107 # @!attribute [r] page_token_param
109 # currently always 'pageToken'
110 def_delegators :@base_result, :page_token_param
113 # Get the token used for requesting the next page of data
115 # @!attribute [r] next_page_token
118 def_delegators :@base_result, :next_page_token
121 # Get the token used for requesting the previous page of data
123 # @!attribute [r] prev_page_token
125 # previous page token
126 def_delegators :@base_result, :prev_page_token
128 # @!attribute [r] resumable_upload
130 # TODO(sgomes): implement resumable_upload for Service::Result
131 raise NotImplementedError
135 # Build a request for fetching the next page of data
137 # @return [Google::APIClient::Service::Request]
138 # API request for retrieving next page
140 request = @request.clone
141 # Make a deep copy of the parameters.
142 request.parameters = Marshal.load(Marshal.dump(request.parameters))
143 request.parameters[page_token_param] = self.next_page_token
148 # Build a request for fetching the previous page of data
150 # @return [Google::APIClient::Service::Request]
151 # API request for retrieving previous page
153 request = @request.clone
154 # Make a deep copy of the parameters.
155 request.parameters = Marshal.load(Marshal.dump(request.parameters))
156 request.parameters[page_token_param] = self.prev_page_token