db310a3763fcc6209f0c8b36cc1d34d6d9919165
[arvados.git] / lib / google / api_client / discovery / api.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
16 require 'addressable/uri'
17
18 require 'google/inflection'
19 require 'google/api_client/discovery/resource'
20 require 'google/api_client/discovery/method'
21
22 module Google
23   class APIClient
24     ##
25     # A service that has been described by a discovery document.
26     class API
27
28       ##
29       # Creates a description of a particular version of a service.
30       #
31       # @param [String] api
32       #   The identifier for the service.  Note that while this frequently
33       #   matches the first segment of all of the service's RPC names, this
34       #   should not be assumed.  There is no requirement that these match.
35       # @param [String] version
36       #   The identifier for the service version.
37       # @param [Hash] api_description
38       #   The section of the discovery document that applies to this service
39       #   version.
40       #
41       # @return [Google::APIClient::API] The constructed service object.
42       def initialize(document_base, discovery_document)
43         @document_base = Addressable::URI.parse(document_base)
44         @discovery_document = discovery_document
45         metaclass = (class <<self; self; end)
46         self.resources.each do |resource|
47           method_name = Google::INFLECTOR.underscore(resource.name).to_sym
48           if !self.respond_to?(method_name)
49             metaclass.send(:define_method, method_name) { resource }
50           end
51         end
52         self.methods.each do |method|
53           method_name = Google::INFLECTOR.underscore(method.name).to_sym
54           if !self.respond_to?(method_name)
55             metaclass.send(:define_method, method_name) { method }
56           end
57         end
58       end
59
60       ##
61       # Returns the id of the service.
62       #
63       # @return [String] The service id.
64       def id
65         return (
66           @discovery_document['id'] ||
67           "#{self.name}:#{self.version}"
68         )
69       end
70
71       ##
72       # Returns the identifier for the service.
73       #
74       # @return [String] The service identifier.
75       def name
76         return @discovery_document['name']
77       end
78
79       ##
80       # Returns the version of the service.
81       #
82       # @return [String] The service version.
83       def version
84         return @discovery_document['version']
85       end
86
87       ##
88       # Returns the parsed section of the discovery document that applies to
89       # this version of the service.
90       #
91       # @return [Hash] The service description.
92       def description
93         return @discovery_document['description']
94       end
95
96       ##
97       # Returns true if this is the preferred version of this API.
98       #
99       # @return [TrueClass, FalseClass]
100       #   Whether or not this is the preferred version of this API.
101       def preferred
102         return !!@discovery_document['preferred']
103       end
104
105       ##
106       # Returns the list of API features.
107       #
108       # @return [Array]
109       #   The features supported by this API.
110       def features
111         return @discovery_document['features'] || []
112       end
113
114       ##
115       # Returns true if this API uses a data wrapper.
116       #
117       # @return [TrueClass, FalseClass]
118       #   Whether or not this API uses a data wrapper.
119       def data_wrapper?
120         return self.features.include?('dataWrapper')
121       end
122
123       ##
124       # Returns the base URI for the discovery document.
125       #
126       # @return [Addressable::URI] The base URI.
127       attr_reader :document_base
128
129       ##
130       # Returns the base URI for this version of the service.
131       #
132       # @return [Addressable::URI] The base URI that methods are joined to.
133       def method_base
134         if @discovery_document['basePath']
135           return @method_base ||= (
136             self.document_base +
137             Addressable::URI.parse(@discovery_document['basePath'])
138           ).normalize
139         else
140           return nil
141         end
142       end
143
144       ##
145       # Updates the hierarchy of resources and methods with the new base.
146       #
147       # @param [Addressable::URI, #to_str, String] new_base
148       #   The new base URI to use for the service.
149       def method_base=(new_method_base)
150         @method_base = Addressable::URI.parse(new_method_base)
151         self.resources.each do |resource|
152           resource.method_base = @method_base
153         end
154         self.methods.each do |method|
155           method.method_base = @method_base
156         end
157       end
158
159       ##
160       # A list of schemas available for this version of the API.
161       #
162       # @return [Hash] A list of {Google::APIClient::Schema} objects.
163       def schemas
164         return @schemas ||= (
165           (@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
166             accu[k] = Google::APIClient::Schema.parse(self, v)
167             accu
168           end
169         )
170       end
171
172       ##
173       # Returns a schema for a kind value.
174       #
175       # @return [Google::APIClient::Schema] The associated Schema object.
176       def schema_for_kind(kind)
177         api_name, schema_name = kind.split('#', 2)
178         if api_name != self.name
179           raise ArgumentError,
180             "The kind does not match this API. " +
181             "Expected '#{self.name}', got '#{api_name}'."
182         end
183         for k, v in self.schemas
184           return v if k.downcase == schema_name.downcase
185         end
186         return nil
187       end
188
189       ##
190       # A list of resources available at the root level of this version of the
191       # API.
192       #
193       # @return [Array] A list of {Google::APIClient::Resource} objects.
194       def resources
195         return @resources ||= (
196           (@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
197             accu << Google::APIClient::Resource.new(
198               self, self.method_base, k, v
199             )
200             accu
201           end
202         )
203       end
204
205       ##
206       # A list of methods available at the root level of this version of the
207       # API.
208       #
209       # @return [Array] A list of {Google::APIClient::Method} objects.
210       def methods
211         return @methods ||= (
212           (@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
213             accu << Google::APIClient::Method.new(self, self.method_base, k, v)
214             accu
215           end
216         )
217       end
218
219       ##
220       # Converts the service to a flat mapping of RPC names and method objects.
221       #
222       # @return [Hash] All methods available on the service.
223       #
224       # @example
225       #   # Discover available methods
226       #   method_names = client.discovered_api('buzz').to_h.keys
227       def to_h
228         return @hash ||= (begin
229           methods_hash = {}
230           self.methods.each do |method|
231             methods_hash[method.id] = method
232           end
233           self.resources.each do |resource|
234             methods_hash.merge!(resource.to_h)
235           end
236           methods_hash
237         end)
238       end
239
240       ##
241       # Returns a <code>String</code> representation of the service's state.
242       #
243       # @return [String] The service's state, as a <code>String</code>.
244       def inspect
245         sprintf(
246           "#<%s:%#0x ID:%s>", self.class.to_s, self.object_id, self.id
247         )
248       end
249     end
250   end
251 end