google-api-client 0.7.0.rc2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,103 @@
1
+ # Copyright 2013 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+ require 'google/api_client/service/result'
16
+ require 'google/api_client/batch'
17
+
18
+ module Google
19
+ class APIClient
20
+ class Service
21
+
22
+ ##
23
+ # Helper class to contain the result of an individual batched call.
24
+ #
25
+ class BatchedCallResult < Result
26
+ # @return [Fixnum] Index of the call
27
+ def call_index
28
+ return @base_result.response.call_id.to_i - 1
29
+ end
30
+ end
31
+
32
+ ##
33
+ #
34
+ #
35
+ class BatchRequest
36
+ ##
37
+ # Creates a new batch request.
38
+ # This class shouldn't be instantiated directly, but rather through
39
+ # Service.batch.
40
+ #
41
+ # @param [Array] calls
42
+ # List of Google::APIClient::Service::Request to be made.
43
+ # @param [Proc] block
44
+ # Callback for every call's response. Won't be called if a call
45
+ # defined a callback of its own.
46
+ #
47
+ # @yield [Google::APIClient::Service::Result]
48
+ # block to be called when result ready
49
+ def initialize(service, calls, &block)
50
+ @service = service
51
+ @base_batch = Google::APIClient::BatchRequest.new
52
+ @global_callback = block if block_given?
53
+
54
+ if calls && calls.length > 0
55
+ calls.each do |call|
56
+ add(call)
57
+ end
58
+ end
59
+ end
60
+
61
+ ##
62
+ # Add a new call to the batch request.
63
+ #
64
+ # @param [Google::APIClient::Service::Request] call
65
+ # the call to be added.
66
+ # @param [Proc] block
67
+ # callback for this call's response.
68
+ #
69
+ # @return [Google::APIClient::Service::BatchRequest]
70
+ # the BatchRequest, for chaining
71
+ #
72
+ # @yield [Google::APIClient::Service::Result]
73
+ # block to be called when result ready
74
+ def add(call, &block)
75
+ if !block_given? && @global_callback.nil?
76
+ raise BatchError, 'Request needs a block'
77
+ end
78
+ callback = block || @global_callback
79
+ base_call = {
80
+ :api_method => call.method,
81
+ :parameters => call.parameters
82
+ }
83
+ @base_batch.add(base_call) do |base_result|
84
+ result = Google::APIClient::Service::BatchedCallResult.new(
85
+ call, base_result)
86
+ callback.call(result)
87
+ end
88
+ return self
89
+ end
90
+
91
+ ##
92
+ # Executes the batch request.
93
+ def execute
94
+ @service.execute(self)
95
+ end
96
+
97
+ attr_reader :base_batch
98
+
99
+ end
100
+
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,144 @@
1
+ # Copyright 2013 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+ module Google
16
+ class APIClient
17
+ class Service
18
+ ##
19
+ # Handles an API request.
20
+ # This contains a full definition of the request to be made (including
21
+ # method name, parameters, body and media). The remote API call can be
22
+ # invoked with execute().
23
+ class Request
24
+ ##
25
+ # Build a request.
26
+ # This class should not be directly instantiated in user code;
27
+ # instantiation is handled by the stub methods created on Service and
28
+ # Resource objects.
29
+ #
30
+ # @param [Google::APIClient::Service] service
31
+ # The parent Service instance that will execute the request.
32
+ # @param [Google::APIClient::Method] method
33
+ # The Method instance that describes the API method invoked by the
34
+ # request.
35
+ # @param [Hash] parameters
36
+ # A Hash of parameter names and values to be sent in the API call.
37
+ def initialize(service, method, parameters)
38
+ @service = service
39
+ @method = method
40
+ @parameters = parameters
41
+ @body = nil
42
+ @media = nil
43
+
44
+ metaclass = (class << self; self; end)
45
+
46
+ # If applicable, add "body", "body=" and resource-named methods for
47
+ # retrieving and setting the HTTP body for this request.
48
+ # Examples of setting the body for files.insert in the Drive API:
49
+ # request.body = object
50
+ # request.execute
51
+ # OR
52
+ # request.file = object
53
+ # request.execute
54
+ # OR
55
+ # request.body(object).execute
56
+ # OR
57
+ # request.file(object).execute
58
+ # Examples of retrieving the body for files.insert in the Drive API:
59
+ # object = request.body
60
+ # OR
61
+ # object = request.file
62
+ if method.request_schema
63
+ body_name = method.request_schema.data['id'].dup
64
+ body_name[0] = body_name[0].chr.downcase
65
+ body_name_equals = (body_name + '=').to_sym
66
+ body_name = body_name.to_sym
67
+
68
+ metaclass.send(:define_method, :body) do |*args|
69
+ if args.length == 1
70
+ @body = args.first
71
+ return self
72
+ elsif args.length == 0
73
+ return @body
74
+ else
75
+ raise ArgumentError,
76
+ "wrong number of arguments (#{args.length}; expecting 0 or 1)"
77
+ end
78
+ end
79
+
80
+ metaclass.send(:define_method, :body=) do |body|
81
+ @body = body
82
+ end
83
+
84
+ metaclass.send(:alias_method, body_name, :body)
85
+ metaclass.send(:alias_method, body_name_equals, :body=)
86
+ end
87
+
88
+ # If applicable, add "media" and "media=" for retrieving and setting
89
+ # the media object for this request.
90
+ # Examples of setting the media object:
91
+ # request.media = object
92
+ # request.execute
93
+ # OR
94
+ # request.media(object).execute
95
+ # Example of retrieving the media object:
96
+ # object = request.media
97
+ if method.media_upload
98
+ metaclass.send(:define_method, :media) do |*args|
99
+ if args.length == 1
100
+ @media = args.first
101
+ return self
102
+ elsif args.length == 0
103
+ return @media
104
+ else
105
+ raise ArgumentError,
106
+ "wrong number of arguments (#{args.length}; expecting 0 or 1)"
107
+ end
108
+ end
109
+
110
+ metaclass.send(:define_method, :media=) do |media|
111
+ @media = media
112
+ end
113
+ end
114
+ end
115
+
116
+ ##
117
+ # Returns the parent service capable of executing this request.
118
+ #
119
+ # @return [Google::APIClient::Service] The parent service.
120
+ attr_reader :service
121
+
122
+ ##
123
+ # Returns the Method instance that describes the API method invoked by
124
+ # the request.
125
+ #
126
+ # @return [Google::APIClient::Method] The API method description.
127
+ attr_reader :method
128
+
129
+ ##
130
+ # Contains the Hash of parameter names and values to be sent as the
131
+ # parameters for the API call.
132
+ #
133
+ # @return [Hash] The request parameters.
134
+ attr_accessor :parameters
135
+
136
+ ##
137
+ # Executes the request.
138
+ def execute
139
+ @service.execute(self)
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,40 @@
1
+ # Copyright 2013 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+ module Google
16
+ class APIClient
17
+ class Service
18
+ ##
19
+ # Handles an API resource.
20
+ # Simple class that contains API methods and/or child resources.
21
+ class Resource
22
+ include Google::APIClient::Service::StubGenerator
23
+
24
+ ##
25
+ # Build a resource.
26
+ # This class should not be directly instantiated in user code; resources
27
+ # are instantiated by the stub generation mechanism on Service creation.
28
+ #
29
+ # @param [Google::APIClient::Service] service
30
+ # The Service instance this resource belongs to.
31
+ # @param [Google::APIClient::API, Google::APIClient::Resource] root
32
+ # The node corresponding to this resource.
33
+ def initialize(service, root)
34
+ @service = service
35
+ generate_call_stubs(service, root)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,162 @@
1
+ # Copyright 2013 Google Inc.
2
+ #
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
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
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.
14
+
15
+ module Google
16
+ class APIClient
17
+ class Service
18
+ ##
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.
23
+ class Result
24
+ extend Forwardable
25
+
26
+ ##
27
+ # Init the result.
28
+ #
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)
34
+ @request = request
35
+ @base_result = base_result
36
+ end
37
+
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
45
+
46
+ # @return [Google::APIClient::Service::Request] Original request object
47
+ attr_reader :request
48
+
49
+ ##
50
+ # Get the content type of the response
51
+ # @!attribute [r] media_type
52
+ # @return [String]
53
+ # Value of content-type header
54
+ def_delegators :@base_result, :media_type
55
+
56
+ ##
57
+ # Check if request failed
58
+ #
59
+ # @!attribute [r] error?
60
+ # @return [TrueClass, FalseClass]
61
+ # true if result of operation is an error
62
+ def_delegators :@base_result, :error?
63
+
64
+ ##
65
+ # Check if request was successful
66
+ #
67
+ # @!attribute [r] success?
68
+ # @return [TrueClass, FalseClass]
69
+ # true if result of operation was successful
70
+ def_delegators :@base_result, :success?
71
+
72
+ ##
73
+ # Extracts error messages from the response body
74
+ #
75
+ # @!attribute [r] error_message
76
+ # @return [String]
77
+ # error message, if available
78
+ def_delegators :@base_result, :error_message
79
+
80
+ ##
81
+ # Check for parsable data in response
82
+ #
83
+ # @!attribute [r] data?
84
+ # @return [TrueClass, FalseClass]
85
+ # true if body can be parsed
86
+ def_delegators :@base_result, :data?
87
+
88
+ ##
89
+ # Return parsed version of the response body.
90
+ #
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
95
+
96
+ ##
97
+ # Pagination scheme used by this request/response
98
+ #
99
+ # @!attribute [r] pagination_type
100
+ # @return [Symbol]
101
+ # currently always :token
102
+ def_delegators :@base_result, :pagination_type
103
+
104
+ ##
105
+ # Name of the field that contains the pagination token
106
+ #
107
+ # @!attribute [r] page_token_param
108
+ # @return [String]
109
+ # currently always 'pageToken'
110
+ def_delegators :@base_result, :page_token_param
111
+
112
+ ##
113
+ # Get the token used for requesting the next page of data
114
+ #
115
+ # @!attribute [r] next_page_token
116
+ # @return [String]
117
+ # next page tokenx =
118
+ def_delegators :@base_result, :next_page_token
119
+
120
+ ##
121
+ # Get the token used for requesting the previous page of data
122
+ #
123
+ # @!attribute [r] prev_page_token
124
+ # @return [String]
125
+ # previous page token
126
+ def_delegators :@base_result, :prev_page_token
127
+
128
+ # @!attribute [r] resumable_upload
129
+ def resumable_upload
130
+ # TODO(sgomes): implement resumable_upload for Service::Result
131
+ raise NotImplementedError
132
+ end
133
+
134
+ ##
135
+ # Build a request for fetching the next page of data
136
+ #
137
+ # @return [Google::APIClient::Service::Request]
138
+ # API request for retrieving next page
139
+ def 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
144
+ return request
145
+ end
146
+
147
+ ##
148
+ # Build a request for fetching the previous page of data
149
+ #
150
+ # @return [Google::APIClient::Service::Request]
151
+ # API request for retrieving previous page
152
+ def prev_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
157
+ return request
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end