Merge branch 'master' of https://code.google.com/p/google-api-ruby-client
[arvados.git] / lib / google / api_client / reference.rb
1 # Copyright 2010 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 'stringio'
16 require 'json'
17 require 'addressable/uri'
18 require 'google/api_client/discovery'
19
20 module Google
21   class APIClient
22     class Reference
23       def initialize(options={})
24         # We only need this to do lookups on method ID String values
25         # It's optional, but method ID lookups will fail if the client is
26         # omitted.
27         @client = options[:client]
28         @version = options[:version] || 'v1'
29
30         self.api_method = options[:api_method]
31         self.parameters = options[:parameters] || {}
32         # These parameters are handled differently because they're not
33         # parameters to the API method, but rather to the API system.
34         self.parameters['key'] ||= options[:key] if options[:key]
35         self.parameters['user_ip'] ||= options[:user_ip] if options[:user_ip]
36         self.headers = options[:headers] || []
37         if options[:body]
38           self.body = options[:body]
39         elsif options[:merged_body]
40           self.merged_body = options[:merged_body]
41         elsif options[:body_object]
42           if options[:body_object].respond_to?(:to_json)
43             serialized_body = options[:body_object].to_json
44           elsif options[:body_object].respond_to?(:to_hash)
45             serialized_body = JSON.generate(options[:body_object].to_hash)
46           else
47             raise TypeError,
48               'Could not convert body object to JSON.' +
49               'Must respond to :to_json or :to_hash.'
50           end
51           self.merged_body = serialized_body
52         else
53           self.merged_body = ''
54         end
55         unless self.api_method
56           self.http_method = options[:http_method] || 'GET'
57           self.uri = options[:uri]
58         end
59       end
60
61       def api_method
62         return @api_method
63       end
64
65       def api_method=(new_api_method)
66         if new_api_method.kind_of?(Google::APIClient::Method) ||
67             new_api_method == nil
68           @api_method = new_api_method
69         elsif new_api_method.respond_to?(:to_str) ||
70             new_api_method.kind_of?(Symbol)
71           unless @client
72             raise ArgumentError,
73               "API method lookup impossible without client instance."
74           end
75           new_api_method = new_api_method.to_s
76           # This method of guessing the API is unreliable. This will fail for
77           # APIs where the first segment of the RPC name does not match the
78           # service name. However, this is a fallback mechanism anyway.
79           # Developers should be passing in a reference to the method, rather
80           # than passing in a string or symbol. This should raise an error
81           # in the case of a mismatch.
82           api = new_api_method[/^([^.]+)\./, 1]
83           @api_method = @client.discovered_method(
84             new_api_method, api, @version
85           )
86           if @api_method
87             # Ditch the client reference, we won't need it again.
88             @client = nil
89           else
90             raise ArgumentError, "API method could not be found."
91           end
92         else
93           raise TypeError,
94             "Expected Google::APIClient::Method, got #{new_api_method.class}."
95         end
96       end
97
98       def parameters
99         return @parameters
100       end
101
102       def parameters=(new_parameters)
103         # No type-checking needed, the Method class handles this.
104         @parameters = new_parameters
105       end
106
107       def body
108         return @body
109       end
110
111       def body=(new_body)
112         if new_body.respond_to?(:each)
113           @body = new_body
114         else
115           raise TypeError, "Expected body to respond to :each."
116         end
117       end
118
119       def merged_body
120         return (self.body.inject(StringIO.new) do |accu, chunk|
121           accu.write(chunk)
122           accu
123         end).string
124       end
125
126       def merged_body=(new_merged_body)
127         if new_merged_body.respond_to?(:string)
128           new_merged_body = new_merged_body.string
129         elsif new_merged_body.respond_to?(:to_str)
130           new_merged_body = new_merged_body.to_str
131         else
132           raise TypeError,
133             "Expected String or StringIO, got #{new_merged_body.class}."
134         end
135         self.body = [new_merged_body]
136       end
137
138       def headers
139         return @headers ||= []
140       end
141
142       def headers=(new_headers)
143         if new_headers.kind_of?(Array) || new_headers.kind_of?(Hash)
144           @headers = new_headers
145         else
146           raise TypeError, "Expected Hash or Array, got #{new_headers.class}."
147         end
148       end
149
150       def http_method
151         return @http_method ||= self.api_method.http_method
152       end
153
154       def http_method=(new_http_method)
155         if new_http_method.kind_of?(Symbol)
156           @http_method = new_http_method.to_s.upcase
157         elsif new_http_method.respond_to?(:to_str)
158           @http_method = new_http_method.to_str.upcase
159         else
160           raise TypeError,
161             "Expected String or Symbol, got #{new_http_method.class}."
162         end
163       end
164
165       def uri
166         return @uri ||= self.api_method.generate_uri(self.parameters)
167       end
168
169       def uri=(new_uri)
170         @uri = Addressable::URI.parse(new_uri)
171       end
172
173       def to_request
174         if self.api_method
175           return self.api_method.generate_request(
176             self.parameters, self.merged_body, self.headers
177           )
178         else
179           return [self.http_method, self.uri, self.headers, self.body]
180         end
181       end
182
183       def to_hash
184         options = {}
185         if self.api_method
186           options[:api_method] = self.api_method
187           options[:parameters] = self.parameters
188         else
189           options[:http_method] = self.http_method
190           options[:uri] = self.uri
191         end
192         options[:headers] = self.headers
193         options[:body] = self.body
194         return options
195       end
196     end
197   end
198 end