This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
/
YOSMethodBase.as
294 lines (253 loc) · 7.23 KB
/
YOSMethodBase.as
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.social.methodgroups
{
import com.adobe.serialization.json.JSON;
import com.yahoo.oauth.OAuthConnection;
import com.yahoo.oauth.OAuthConsumer;
import com.yahoo.oauth.OAuthRequest;
import com.yahoo.oauth.OAuthToken;
import com.yahoo.social.YahooUser;
import flash.events.EventDispatcher;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequestHeader;
/**
* Dispatched when a request fails due to a security error.
*/
[Event(name="securityError", type="SecurityErrorEvent")]
/**
* The base class for all YOS API wrappers.
* @author Zach Graves (zachg@yahoo-inc.com)
*
*/
public class YOSMethodBase extends EventDispatcher
{
//--------------------------------------
// Constants (namespaces & host names)
//--------------------------------------
/**
* The hostname for Yahoo Social APIs.
*/
protected static const SOCIAL_WS_HOSTNAME:String = "social.yahooapis.com";
/**
* The hostname for the Yahoo Application Platform data store.
*/
protected static const APPSTORE_WS_HOSTNAME:String = "appstore.apps.yahooapis.com";
/**
* The hostname for the YQL API.
*/
protected static const YQL_HOSTNAME:String = "query.yahooapis.com";
/**
* The hostname for the Yahoo login service (OAuth) API.
*/
protected static var OAUTH_HOSTNAME:String = "api.login.yahoo.com";
/**
* The name of the OAuth realm for Yahoo! web-services.
*/
protected static var OAUTH_REALM:String = "yahooapis.com";
/**
* The Yahoo namespace used in XML responses.
* @private
*/
private static const YAHOOAPIS_NAMESPACE:Namespace = new Namespace("http://yahooapis.com/v1/base.rng");
//--------------------------------------
// Protected variables
//--------------------------------------
/**
* The YahooUser to reference in all requests.
*/
protected var $user:YahooUser;
/**
* The response format to apply to all requests.
*/
protected var $format:String = "json";
/**
* The version of the API.
*/
protected var $version:String = "v1";
/**
* The hostname to be used in constructing URLs for all requests.
*/
protected var $hostname:String;
/**
* An OAuthConsumer object containing consumer key and secret values.
*/
protected var $consumer:OAuthConsumer;
/**
* An OAuthToken object containing access token key and secret values.
*/
protected var $token:OAuthToken;
/**
* The name of the OAuth request type used for all requests in this class.
*/
protected var $oauthRequestType:String;
/**
* Determines if the request parameters in the signature base string
* should be encoded using <code>encodeURIComponent</code> when signing the request.
*/
protected var $useExplicitEncoding:Boolean=true;
//--------------------------------------
// Public Methods
//--------------------------------------
/**
* Constructor
*
* Creates a new YOSMethodBase object. This is the base class for all YOS API wrappers.
* @param user
*
*/
public function YOSMethodBase(user:YahooUser=null)
{
super();
this.$user = user;
this.$oauthRequestType = OAuthRequest.OAUTH_REQUEST_TYPE_OBJECT;
}
/**
* An OAuthConsumer object containing consumer key and secret values.
*
* If you wish to make a 2-legged OAuth request, do not pass a YahooUser in the
* constructor and set this value with your consumer credentials.
* @see OAuthConsumer
*/
public function get consumer():OAuthConsumer
{
if(this.$user) {
this.consumer = this.$user.session.getConsumer();
}
return $consumer;
}
/**
* @private
* @param value
*
*/
public function set consumer(value:OAuthConsumer):void
{
this.$consumer = value;
}
/**
* An OAuthToken object containing access token key and secret values
*
* This value is only used if a user is not defined when signing a request with OAuth.
*
* If you wish to make a 3-legged OAuth request, do not pass a YahooUser in the
* constructor and set this value with your access token credentials.
* @see OAuthToken
*/
public function get token():OAuthToken
{
if(this.$user) {
this.token = this.$user.session.getAccessToken();
}
return $token;
}
/**
* @private
* @param value
*
*/
public function set token(value:OAuthToken):void
{
this.$token = value;
}
/**
* The user referenced in all data requests.
* @return
*
*/
public function get user():YahooUser
{
return this.$user;
}
/**
* @private
* @param value
*
*/
public function set user(value:YahooUser):void
{
this.$user = value;
}
//--------------------------------------
// Protected Methods
//--------------------------------------
/**
* Returns an object containing the required or default request parameters.
* @return
*
*/
protected function getDefaultArguments():Object
{
var args:Object = new Object();
args.format = this.$format;
if(this.$user) { // if there is a user, apply the language and region to the request.
args.lang = this.$user.lang;
args.region = this.$user.region;
}
return args;
}
/**
* A simple wrapper of the JSON::decode function.
* @param s
* @return
* @see http://code.google.com/p/as3corelib/
*/
protected function decodeJSON(s:String):*
{
return JSON.decode(s);
}
/**
* A simple wrapper of the JSON::encode function.
* @param o
* @return
* @see http://code.google.com/p/as3corelib/
*/
protected function encodeJSON(o:Object):String
{
return JSON.encode(o);
}
/**
* Signs the request with OAuth credentials then sends the request to the web-service with the supplied callbacks.
* @param httpMethod
* @param url
* @param callback
* @param args
*
*/
protected function sendRequest(httpMethod:String, url:String, callback:Object, args:Object=null, headers:Array=null):void
{
if(!headers) headers = [];
// headers.push( new URLRequestHeader("Accept","application/json") );
var connection:OAuthConnection = OAuthConnection.fromConsumerAndToken(this.consumer, this.token);
connection.realm = OAUTH_REALM;
connection.requestType = $oauthRequestType;
connection.useExplicitEncoding = $useExplicitEncoding;
connection.asyncRequestSigned(httpMethod, url, callback, args, headers);
}
/**
* Dispatches a <code>SECURITY_ERROR</code> event.
* @param response
*/
protected function handleSecurityError(response:Object):void
{
var event:SecurityErrorEvent = new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR);
event.text = response.responseText;
this.dispatchEvent(event);
}
/**
* Returns true if the status code is 200. (AIR-Only)
*
* Will also return true if the code is 0 to bypass the browser not providing an http status code.
*
* @param status
* @return
*
*/
protected function getResponseStatusOk(status:int):Boolean
{
return (status == 200 || status == 0);
}
}
}