@@ -4,7 +4,21 @@ var sys = require("sys"),
4
4
md5 = require ( 'md5' ) ;
5
5
var flickr = http . createClient ( 80 , "api.flickr.com" ) ;
6
6
7
- function generate_signature ( shared_secret , arguments ) {
7
+ flickr . Request = function Request ( api_key , shared_secret , auth_token ) {
8
+ this . _configure ( api_key , shared_secret , auth_token ) ;
9
+ } ;
10
+
11
+ flickr . Request . prototype . _configure = function ( api_key , shared_secret , auth_token ) {
12
+ this . api_key = api_key ;
13
+ this . shared_secret = shared_secret ;
14
+ this . auth_token = auth_token ;
15
+ } ;
16
+
17
+ flickr . Request . prototype . setAuthenticationToken = function ( auth_token ) {
18
+ this . _configure ( this . api_key , this . shared_secret , auth_token ) ;
19
+ } ;
20
+
21
+ flickr . Request . prototype . generateSignature = function ( shared_secret , arguments ) {
8
22
var argument_pairs = [ ] ;
9
23
for ( var key in arguments ) {
10
24
argument_pairs [ argument_pairs . length ] = [ key , arguments [ key ] ] ;
@@ -21,9 +35,9 @@ function generate_signature(shared_secret, arguments) {
21
35
}
22
36
var sig = shared_secret + args ;
23
37
return md5 . md5 ( sig ) ;
24
- }
38
+ } ;
25
39
26
- function create_flickr_request ( method , arguments , api_key , shared_secret , sign , result_mapper ) {
40
+ flickr . Request . prototype . getRequestPromise = function ( method , arguments , sign_it , result_mapper ) {
27
41
var promise = new process . Promise ( )
28
42
var argumentString = "" ;
29
43
var api_sig = undefined ;
@@ -33,10 +47,11 @@ function create_flickr_request(method, arguments, api_key, shared_secret, sign,
33
47
arguments . format = "json" ;
34
48
arguments . nojsoncallback = "1" ;
35
49
arguments [ "method" ] = method ;
36
- arguments . api_key = api_key ;
50
+ arguments . api_key = this . api_key ;
51
+ if ( this . auth_token ) arguments . auth_token = this . auth_token ;
37
52
38
- if ( shared_secret && sign ) {
39
- api_sig = generate_signature ( shared_secret , arguments ) ;
53
+ if ( this . shared_secret && ( sign_it || this . auth_token ) ) {
54
+ api_sig = this . generateSignature ( this . shared_secret , arguments ) ;
40
55
if ( api_sig ) {
41
56
arguments . api_sig = api_sig ;
42
57
}
@@ -75,63 +90,67 @@ function create_flickr_request(method, arguments, api_key, shared_secret, sign,
75
90
} ) ;
76
91
} ) ;
77
92
return promise ;
78
- }
93
+ } ;
79
94
80
- flickr . Auth = function Auth ( api_key , shared_secret ) {
81
- this . api_key = api_key ;
82
- this . shared_secret = shared_secret ;
95
+ flickr . Auth = function Auth ( request ) {
96
+ this . _request = request ;
83
97
} ;
84
98
85
99
flickr . Auth . prototype . getFrob = function ( ) {
86
- return create_flickr_request ( "flickr.auth.getFrob" ,
87
- { } , this . api_key , this . shared_secret , true , function ( res ) { return res . _content ; } ) ;
100
+ return this . _request . getRequestPromise ( "flickr.auth.getFrob" ,
101
+ { } , true , function ( res ) { return res . _content ; } ) ;
88
102
} ;
89
103
90
104
flickr . Auth . prototype . getToken = function ( frob ) {
91
- return create_flickr_request ( "flickr.auth.getToken" ,
92
- { "frob" :frob } , this . api_key , this . shared_secret , true ) ;
105
+ return this . _request . getRequestPromise ( "flickr.auth.getToken" ,
106
+ { "frob" :frob } , true , function ( res ) {
107
+ return {
108
+ "token" : res . token . _content ,
109
+ "perms" : res . perms . _content ,
110
+ "user" : res . user
111
+ } ;
112
+ } ) ;
113
+ } ;
114
+
115
+ flickr . Blogs = function Blogs ( request ) {
116
+ this . _request = request ;
117
+ } ;
118
+ flickr . Blogs . prototype . getList = function ( ) {
119
+ return this . _request . getRequestPromise ( "flickr.blogs.getList" ,
120
+ { } , true ) ;
93
121
} ;
94
122
95
123
96
- flickr . People = function People ( api_key , shared_secret ) {
97
- this . api_key = api_key ;
98
- this . shared_secret = shared_secret ;
124
+ flickr . People = function People ( request ) {
125
+ this . _request = request ;
99
126
} ;
100
127
101
128
flickr . People . prototype . findByUsername = function ( username ) {
102
- return create_flickr_request ( "flickr.people.findByUsername" ,
103
- { "username" : username } , this . api_key , this . shared_secret ) ;
129
+ return this . _request . getRequestPromise ( "flickr.people.findByUsername" ,
130
+ { "username" : username } ) ;
104
131
} ;
105
132
106
- flickr . Photos = function Photos ( api_key , shared_secret ) {
107
- this . api_key = api_key ;
108
- this . shared_secret = shared_secret ;
133
+ flickr . Photos = function Photos ( request ) {
134
+ this . _request = request ;
109
135
} ;
110
136
111
137
flickr . Photos . prototype . getInfo = function ( photo_id , secret ) {
112
138
var arguments = { "photo_id" : photo_id } ;
113
139
if ( secret !== undefined ) arguments . secret = secret ;
114
-
115
- return create_flickr_request ( "flickr.photos.getInfo" ,
116
- arguments ,
117
- this . api_key , this . shared_secret ) ;
140
+
141
+ return this . _request . getRequestPromise ( "flickr.photos.getInfo" , arguments ) ;
118
142
} ;
119
143
120
- flickr . Photosets = function Photosets ( api_key , shared_secret ) {
121
- this . api_key = api_key ;
122
- this . shared_secret = shared_secret ;
144
+ flickr . Photosets = function Photosets ( request ) {
145
+ this . _request = request ;
123
146
} ;
124
147
125
148
flickr . Photosets . prototype . getInfo = function ( photoset_id ) {
126
- return create_flickr_request ( "flickr.photosets.getInfo" ,
127
- { "photoset_id" : photoset_id } ,
128
- this . api_key , this . shared_secret ) ;
149
+ return this . _request . getRequestPromise ( "flickr.photosets.getInfo" , { "photoset_id" : photoset_id } ) ;
129
150
} ;
130
151
131
152
flickr . Photosets . prototype . getList = function ( user_id ) {
132
- return create_flickr_request ( "flickr.photosets.getList" ,
133
- { "user_id" : user_id } ,
134
- this . api_key , this . shared_secret ) ;
153
+ return this . _request . getRequestPromise ( "flickr.photosets.getList" , { "user_id" : user_id } ) ;
135
154
} ;
136
155
137
156
flickr . Photosets . prototype . getPhotos = function ( photoset_id , extras , privacy_filter , per_page , page , media ) {
@@ -142,18 +161,26 @@ flickr.Photosets.prototype.getPhotos= function(photoset_id, extras, privacy_filt
142
161
if ( per_page !== undefined ) arguments . per_page = per_page ;
143
162
if ( page !== undefined ) arguments . page = page ;
144
163
if ( media !== undefined ) arguments . media = media ;
145
- return create_flickr_request ( "flickr.photosets.getPhotos" ,
146
- arguments , this . api_key , this . shared_secret ) ;
164
+ return this . _request . getRequestPromise ( "flickr.photosets.getPhotos" , arguments ) ;
165
+ } ;
147
166
167
+ flickr . FlickrAPI = function FlickrAPI ( api_key , shared_secret , auth_token ) {
168
+ this . _configure ( api_key , shared_secret , auth_token ) ;
148
169
} ;
149
170
150
- flickr . FlickrAPI = function FlickrAPI ( api_key , shared_secret ) {
151
- this . api_key = api_key ;
152
- this . shared_secret = shared_secret ;
153
- this . people = new flickr . People ( api_key , shared_secret ) ;
154
- this . photos = new flickr . Photos ( api_key , shared_secret ) ;
155
- this . photosets = new flickr . Photosets ( api_key , shared_secret ) ;
156
- this . auth = new flickr . Auth ( api_key , shared_secret ) ;
171
+ flickr . FlickrAPI . prototype . _configure = function ( api_key , shared_secret , auth_token ) {
172
+ this . _request = new flickr . Request ( api_key , shared_secret , auth_token ) ;
173
+
174
+ this . people = new flickr . People ( this . _request ) ;
175
+ this . photos = new flickr . Photos ( this . _request ) ;
176
+ this . photosets = new flickr . Photosets ( this . _request ) ;
177
+ this . auth = new flickr . Auth ( this . _request ) ;
178
+ this . blogs = new flickr . Blogs ( this . _request ) ;
179
+ } ;
180
+
181
+
182
+ flickr . FlickrAPI . prototype . setAuthenticationToken = function ( auth_token ) {
183
+ this . _request . setAuthenticationToken ( auth_token ) ;
157
184
} ;
158
185
159
186
flickr . FlickrAPI . prototype . getLoginUrl = function ( permissions , frob ) {
0 commit comments