Updated to use Signet.
[arvados.git] / lib / google / api_client.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 module Google #:nodoc:
16   ##
17   # This class manages communication with a single API.
18   class APIClient
19
20     def initialize(options={})
21       @options = {
22         # TODO: What configuration options need to go here?
23       }.merge(options)
24       unless @options[:parser]
25         require 'google/api_client/parser/json_parser'
26         # NOTE: Do not rely on this default value, as it may change
27         @options[:parser] = JSONParser.new
28       end
29       unless @options[:authorization]
30         require 'signet/oauth_1/client'
31         # NOTE: Do not rely on this default value, as it may change
32         @options[:authorization] = Signet::OAuth1::Client.new(
33           :temporary_credential_uri =>
34             'https://www.google.com/accounts/OAuthGetRequestToken',
35           :authorization_uri =>
36             'https://www.google.com/accounts/OAuthAuthorizeToken',
37           :token_credential_uri =>
38             'https://www.google.com/accounts/OAuthGetAccessToken',
39           :client_credential_key => 'anonymous',
40           :client_credential_secret => 'anonymous'
41         )
42       end
43       unless @options[:http_adapter]
44         require 'httpadapter/adapters/net_http'
45         @options[:http_adapter] = HTTPAdapter::NetHTTPRequestAdapter
46       end
47     end
48
49     ##
50     # Returns the parser used by the client.
51     def parser
52       return @options[:parser]
53     end
54
55     ##
56     # Returns the authorization mechanism used by the client.
57     def authorization
58       return @options[:authorization]
59     end
60
61     def build_request(*args, &block)
62       if !args.empty? || block
63         # Build the request!
64         # TODO(bobaman): No-op / Debug code!
65         return args
66       else
67         require 'google/api_client/discovery/method_builder'
68         return ::Google::APIClient::MethodBuilder.new(self, :build_request)
69       end
70     end
71   end
72 end
73
74 require 'google/api_client/version'