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 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().
26 # This class should not be directly instantiated in user code;
27 # instantiation is handled by the stub methods created on Service and
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
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)
40 @parameters = parameters
44 metaclass = (class << self; self; end)
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
52 # request.file = object
55 # request.body(object).execute
57 # request.file(object).execute
58 # Examples of retrieving the body for files.insert in the Drive API:
59 # object = request.body
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
68 metaclass.send(:define_method, :body) do |*args|
72 elsif args.length == 0
76 "wrong number of arguments (#{args.length}; expecting 0 or 1)"
80 metaclass.send(:define_method, :body=) do |body|
84 metaclass.send(:alias_method, body_name, :body)
85 metaclass.send(:alias_method, body_name_equals, :body=)
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
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|
102 elsif args.length == 0
106 "wrong number of arguments (#{args.length}; expecting 0 or 1)"
110 metaclass.send(:define_method, :media=) do |media|
117 # Returns the parent service capable of executing this request.
119 # @return [Google::APIClient::Service] The parent service.
123 # Returns the Method instance that describes the API method invoked by
126 # @return [Google::APIClient::Method] The API method description.
130 # Contains the Hash of parameter names and values to be sent as the
131 # parameters for the API call.
133 # @return [Hash] The request parameters.
134 attr_accessor :parameters
137 # Executes the request.
139 @service.execute(self)