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