-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.rb
199 lines (171 loc) · 5.7 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
module SoundCloud
class Client
include HTTMultiParty
USER_AGENT = "SoundCloud Ruby Wrapper #{VERSION}"
CLIENT_ID_PARAM_NAME = :client_id
API_SUBHOST = 'api'
AUTHORIZE_PATH = '/connect'
TOKEN_PATH = '/oauth2/token'
DEFAULT_OPTIONS = {
:site => 'soundcloud.com',
:on_exchange_token => lambda {}
}
attr_accessor :options
headers({"User-Agent" => USER_AGENT})
def initialize(options={})
store_options(options)
if access_token.nil? && (options_for_refresh_flow_present? || options_for_credentials_flow_present? || options_for_code_flow_present?)
exchange_token
end
raise ArgumentError, "At least a client_id or an access_token must be present" if client_id.nil? && access_token.nil?
end
def get(path, query={}, options={})
handle_response {
self.class.get(*construct_query_arguments(path, options.merge(:query => query)))
}
end
def post(path, body={}, options={})
handle_response {
self.class.post(*construct_query_arguments(path, options.merge(:body => body), :body))
}
end
def put(path, body={}, options={})
handle_response {
self.class.put(*construct_query_arguments(path, options.merge(:body => body), :body))
}
end
def delete(path, query={}, options={})
handle_response {
self.class.delete(*construct_query_arguments(path, options.merge(:query => query)))
}
end
def head(path, query={}, options={})
handle_response {
self.class.head(*construct_query_arguments(path, options.merge(:query => query)))
}
end
# accessors for options
def client_id
@options[:client_id]
end
def client_secret
@options[:client_secret]
end
def access_token
@options[:access_token]
end
def refresh_token
@options[:refresh_token]
end
def redirect_uri
@options[:redirect_uri]
end
def expires_at
@options[:expires_at]
end
def expired?
(expires_at.nil? || expires_at < Time.now)
end
def use_ssl?
!!(@options[:use_ssl?] || access_token)
end
def site
@options[:site]
end
alias host site
def api_host
[API_SUBHOST, host].join('.')
end
def authorize_url(options={})
additional_params = [:display, :state, :scope].map do |param_name|
value = options.delete(param_name)
"#{param_name}=#{CGI.escape value}" unless value.nil?
end.compact.join("&")
store_options(options)
"https://#{host}#{AUTHORIZE_PATH}?response_type=code_and_token&client_id=#{client_id}&redirect_uri=#{URI.escape(redirect_uri)}&#{additional_params}"
end
def exchange_token(options={})
store_options(options)
raise ArgumentError, 'client_id and client_secret is required to retrieve an access_token' if client_id.nil? || client_secret.nil?
client_params = {:client_id => client_id, :client_secret => client_secret}
params = if options_for_refresh_flow_present?
{
:grant_type => 'refresh_token',
:refresh_token => refresh_token,
}
elsif options_for_credentials_flow_present?
{
:grant_type => 'password',
:username => @options[:username],
:password => @options[:password],
}
elsif options_for_code_flow_present?
{
:grant_type => 'authorization_code',
:redirect_uri => @options[:redirect_uri],
:code => @options[:code],
}
end
params.merge!(client_params)
response = handle_response(false) {
self.class.post("https://#{api_host}#{TOKEN_PATH}", :query => params)
}
@options.merge!(:access_token => response.access_token, :refresh_token => response.refresh_token)
@options[:expires_at] = Time.now + response.expires_in if response.expires_in
@options[:on_exchange_token].call(*[(self if @options[:on_exchange_token].arity == 1)].compact)
response
end
def on_exchange_token(&block)
store_options(:on_exchange_token => block)
end
private
def handle_response(refreshing_enabled=true, &block)
response = block.call
if response && !response.success?
if response.code == 401 && refreshing_enabled && options_for_refresh_flow_present?
exchange_token
# TODO it should return the original
handle_response(false, &block)
else
raise ResponseError.new(response)
end
elsif response.is_a?(Hash)
HashResponseWrapper.new(response)
elsif response.is_a?(Array)
ArrayResponseWrapper.new(response)
elsif response && response.success?
response
end
end
def options_for_refresh_flow_present?
!!@options[:refresh_token]
end
def options_for_credentials_flow_present?
!!(@options[:username] && @options[:password])
end
def options_for_code_flow_present?
!!(@options[:code] && @options[:redirect_uri])
end
def store_options(options={})
@options ||= DEFAULT_OPTIONS.dup
@options.merge!(options)
end
def construct_query_arguments(path_or_uri, options={}, body_or_query=:query)
uri = URI.parse(path_or_uri)
path = uri.path
scheme = use_ssl? ? 'https' : 'http'
options = options.dup
options[body_or_query] ||= {}
options[body_or_query][:format] = "json"
if access_token
options[body_or_query][:oauth_token] = access_token
else
options[body_or_query][CLIENT_ID_PARAM_NAME] = client_id
end
[
"#{scheme}://#{api_host}#{path}#{uri.query ? "?#{uri.query}" : ""}",
options
]
end
end
end