Skip to content

Commit a138ad8

Browse files
committed
Split out the API for better seperation of the implementation.
1 parent 6d8be6e commit a138ad8

8 files changed

+263
-252
lines changed

lib/auth.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Auth= function Auth(request) {
2+
this._request= request;
3+
};
4+
5+
Auth.prototype.getFrob= function() {
6+
return this._request.getRequestPromise("flickr.auth.getFrob",
7+
{}, true, function(res) { return res._content; });
8+
};
9+
10+
Auth.prototype.getToken= function(frob) {
11+
return this._request.getRequestPromise("flickr.auth.getToken",
12+
{"frob":frob}, true, function(res) {
13+
return {
14+
"token": res.token._content,
15+
"perms": res.perms._content,
16+
"user": res.user
17+
};
18+
});
19+
};
20+
21+
exports.Auth= Auth;

lib/blogs.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Blogs= function Blogs(request) {
2+
this._request= request;
3+
};
4+
Blogs.prototype.getList= function() {
5+
return this._request.getRequestPromise("flickr.blogs.getList",
6+
{}, true);
7+
};
8+
9+
exports.Blogs = Blogs;

lib/flickr.js

+1-252
Original file line numberDiff line numberDiff line change
@@ -1,252 +1 @@
1-
2-
var sys = require("sys"),
3-
http = require("http"),
4-
md5 = require('./md5');
5-
var flickr= {};
6-
var flickr_request = http.createClient(80, "api.flickr.com");
7-
8-
flickr.Request= function Request(api_key, shared_secret, auth_token) {
9-
this._configure(api_key, shared_secret, auth_token);
10-
};
11-
12-
flickr.Request.prototype._configure= function(api_key, shared_secret, auth_token) {
13-
this.api_key= api_key;
14-
this.shared_secret= shared_secret;
15-
this.auth_token= auth_token;
16-
};
17-
18-
flickr.Request.prototype.setAuthenticationToken= function(auth_token) {
19-
this._configure(this.api_key, this.shared_secret, auth_token);
20-
};
21-
22-
flickr.Request.prototype.generateSignature= function(shared_secret, arguments) {
23-
var argument_pairs= [];
24-
for(var key in arguments ) {
25-
argument_pairs[argument_pairs.length]= [key, arguments[key]];
26-
}
27-
28-
argument_pairs.sort(function(a,b) {
29-
if ( a[0]== b[0] ) return 0 ;
30-
return a[0] < b[0] ? -1 : 1;
31-
});
32-
var args= "";
33-
for(var i=0;i<argument_pairs.length;i++) {
34-
args+= argument_pairs[i][0];
35-
args+= argument_pairs[i][1];
36-
}
37-
var sig= shared_secret+args;
38-
return md5.md5(sig);
39-
};
40-
41-
flickr.Request.prototype.getRequestPromise= function(method, arguments, sign_it, result_mapper) {
42-
var promise= new process.Promise()
43-
var argumentString = "";
44-
var api_sig= undefined;
45-
if( arguments === undefined ) arguments = {};
46-
47-
// apply default arguments
48-
arguments.format= "json";
49-
arguments.nojsoncallback= "1";
50-
arguments["method"]= method;
51-
arguments.api_key= this.api_key;
52-
if( this.auth_token ) arguments.auth_token= this.auth_token;
53-
54-
if( this.shared_secret && (sign_it || this.auth_token) ) {
55-
api_sig= this.generateSignature(this.shared_secret, arguments);
56-
if( api_sig ) {
57-
arguments.api_sig= api_sig;
58-
}
59-
}
60-
var operator= "?";
61-
for(var key in arguments) {
62-
argumentString+= (operator + key + "=" + arguments[key]);
63-
if( operator == "?" ) operator= "&";
64-
}
65-
var request= flickr_request.request("GET",
66-
"/services/rest"+ argumentString,
67-
{"host": "api.flickr.com"});
68-
request.finish(function (response) {
69-
var result= "";
70-
response.setBodyEncoding("utf8");
71-
response.addListener("body", function (chunk) {
72-
result+= chunk;
73-
});
74-
response.addListener("complete", function () {
75-
var res= JSON.parse(result);
76-
if( res.stat == "ok" ) {
77-
// Munge the response to strip out the stat and just return the response value
78-
for(var key in res) {
79-
if( key !== "stat" ) {
80-
res= res[key];
81-
}
82-
}
83-
if( result_mapper ) {
84-
res= result_mapper(res);
85-
}
86-
promise.emitSuccess(res);
87-
}
88-
else {
89-
promise.emitError({code: res.code, message: res.message});
90-
}
91-
});
92-
});
93-
return promise;
94-
};
95-
96-
flickr.Auth= function Auth(request) {
97-
this._request= request;
98-
};
99-
100-
flickr.Auth.prototype.getFrob= function() {
101-
return this._request.getRequestPromise("flickr.auth.getFrob",
102-
{}, true, function(res) { return res._content; });
103-
};
104-
105-
flickr.Auth.prototype.getToken= function(frob) {
106-
return this._request.getRequestPromise("flickr.auth.getToken",
107-
{"frob":frob}, true, function(res) {
108-
return {
109-
"token": res.token._content,
110-
"perms": res.perms._content,
111-
"user": res.user
112-
};
113-
});
114-
};
115-
116-
flickr.Blogs= function Blogs(request) {
117-
this._request= request;
118-
};
119-
flickr.Blogs.prototype.getList= function() {
120-
return this._request.getRequestPromise("flickr.blogs.getList",
121-
{}, true);
122-
};
123-
124-
125-
flickr.People= function People(request) {
126-
this._request= request;
127-
};
128-
129-
flickr.People.prototype.findByUsername= function(username){
130-
return this._request.getRequestPromise("flickr.people.findByUsername",
131-
{"username": username});
132-
};
133-
134-
flickr.Photos= function Photos(request) {
135-
this._request= request;
136-
};
137-
138-
flickr.Photos.prototype.getInfo= function(photo_id, secret) {
139-
var arguments= {"photo_id": photo_id};
140-
if( secret !== undefined ) arguments.secret= secret;
141-
142-
return this._request.getRequestPromise("flickr.photos.getInfo", arguments);
143-
};
144-
145-
//TODO: parameter based API is a bit rubbish here :(
146-
flickr.Photos.prototype.search= function(user_id,tags,tag_mode,text,min_upload_date,max_upload_date,min_taken_date,max_taken_date,license,sort,bbox,accuracy,safe_search,content_type,machine_tags,machine_tag_mode,group_id,contacts,woe_id,place_id,media,has_geo,geo_context,lat,lon,radius,radius_units,is_commons,in_gallery,extras,per_page,page) {
147-
var arguments= {};
148-
if( user_id !== undefined ) arguments.user_id= user_id;
149-
if( tags !== undefined ) arguments.tags= tags;
150-
if( tag_mode !== undefined ) arguments.tag_mode= tag_mode;
151-
if( text !== undefined ) arguments.text= text;
152-
if( min_upload_date !== undefined ) arguments.min_upload_date= min_upload_date;
153-
if( max_upload_date !== undefined ) arguments.max_upload_date= max_upload_date;
154-
if( min_taken_date !== undefined ) arguments.min_taken_date= min_taken_date;
155-
if( max_taken_date !== undefined ) arguments.max_taken_date= max_taken_date;
156-
if( license !== undefined ) arguments.license= license;
157-
if( sort !== undefined ) arguments.sort= sort;
158-
if( bbox !== undefined ) arguments.bbox= bbox;
159-
if( accuracy !== undefined ) arguments.accuracy= accuracy;
160-
if( safe_search !== undefined ) arguments.safe_search= safe_search
161-
if( content_type !== undefined ) arguments.content_type= content_type;
162-
if( machine_tags !== undefined ) arguments.machine_tags= machine_tags;
163-
if( machine_tag_mode !== undefined ) arguments.machine_tag_mode= machine_tag_mode;
164-
if( group_id !== undefined ) arguments.group_id= group_id;
165-
if( contacts !== undefined ) arguments.contacts= contacts;
166-
if( woe_id !== undefined ) arguments.woe_id= woe_id;
167-
if( place_id !== undefined ) arguments.place_id= place_id;
168-
if( media !== undefined ) arguments.media= media;
169-
if( has_geo !== undefined ) arguments.has_geo= has_geo;
170-
if( geo_context !== undefined ) arguments.geo_context= geo_context;
171-
if( lat !== undefined ) arguments.lat= lat;
172-
if( lon !== undefined ) arguments.lon= lon;
173-
if( radius !== undefined ) arguments.radius= radius;
174-
if( radius_units !== undefined ) arguments.radius_units= radius_units;
175-
if( is_commons !== undefined ) arguments.is_commons = is_commons;
176-
if( in_gallery !== undefined ) arguments.in_gallery= in_gallery;
177-
if( extras !== undefined ) arguments.extras= extras;
178-
if( per_page !== undefined ) arguments.per_page= per_page;
179-
if( page !== undefined ) arguments.page= page;
180-
181-
return this._request.getRequestPromise("flickr.photos.search", arguments);
182-
};
183-
184-
flickr.Photosets= function Photosets(request) {
185-
this._request= request;
186-
};
187-
188-
flickr.Photosets.prototype.getInfo= function(photoset_id) {
189-
return this._request.getRequestPromise("flickr.photosets.getInfo", {"photoset_id": photoset_id});
190-
};
191-
192-
flickr.Photosets.prototype.getList= function(user_id) {
193-
return this._request.getRequestPromise("flickr.photosets.getList", {"user_id": user_id});
194-
};
195-
196-
flickr.Photosets.prototype.getPhotos= function(photoset_id, extras, privacy_filter, per_page, page, media) {
197-
var arguments= {"photoset_id": photoset_id};
198-
199-
if( extras !== undefined && extras.join !== undefined ) arguments.extras= extras.join(",");
200-
if( privacy_filter !== undefined ) arguments.privacy_filter= privacy_filter;
201-
if( per_page !== undefined ) arguments.per_page= per_page;
202-
if( page !== undefined ) arguments.page= page;
203-
if( media !== undefined ) arguments.media= media;
204-
return this._request.getRequestPromise("flickr.photosets.getPhotos", arguments);
205-
};
206-
207-
flickr.FlickrAPI= function FlickrAPI(api_key, shared_secret, auth_token) {
208-
this._configure(api_key, shared_secret, auth_token);
209-
};
210-
211-
flickr.FlickrAPI.prototype._configure= function(api_key, shared_secret, auth_token) {
212-
this._request= new flickr.Request(api_key, shared_secret, auth_token);
213-
214-
this.people= new flickr.People(this._request);
215-
this.photos= new flickr.Photos(this._request);
216-
this.photosets= new flickr.Photosets(this._request);
217-
this.auth= new flickr.Auth(this._request);
218-
this.blogs= new flickr.Blogs(this._request);
219-
};
220-
221-
222-
flickr.FlickrAPI.prototype.setAuthenticationToken= function(auth_token) {
223-
this._request.setAuthenticationToken(auth_token);
224-
};
225-
226-
flickr.FlickrAPI.prototype.getLoginUrl= function(permissions, frob) {
227-
var promise= new process.Promise();
228-
if( frob === undefined ) {
229-
var self= this;
230-
this.auth.getFrob()
231-
.addErrback(function(err) {promise.emitError(err);})
232-
.addCallback(function(frob){
233-
var sig= generate_signature(self.shared_secret, {
234-
"api_key": self.api_key,
235-
"perms": permissions,
236-
"frob":frob
237-
});
238-
promise.emitSuccess("http://flickr.com/services/auth/?api_key="+self.api_key+"&perms="+permissions+"&frob="+frob+"&api_sig="+ sig, frob)
239-
});
240-
} else {
241-
var sig= generate_signature(this.shared_secret, {
242-
"api_key": this.api_key,
243-
"perms": permissions,
244-
"frob":frob
245-
});
246-
promise.emitSuccess("http://flickr.com/services/auth/?api_key="+self.api_key+"&perms="+permissions+"&frob="+frob+"&api_sig="+ sig, frob)
247-
}
248-
return promise;
249-
};
250-
251-
252-
exports.FlickrAPI = flickr.FlickrAPI;
1+
exports.FlickrAPI = require("flickrapi").FlickrAPI;

lib/flickrapi.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var Request= require("request").Request,
2+
Auth= require("auth").Auth,
3+
Blogs= require("blogs").Blogs,
4+
People= require("people").People,
5+
Photos= require("photos").Photos,
6+
Photosets= require("photosets").Photosets;
7+
8+
var FlickrAPI= function FlickrAPI(api_key, shared_secret, auth_token) {
9+
this._configure(api_key, shared_secret, auth_token);
10+
};
11+
12+
FlickrAPI.prototype._configure= function(api_key, shared_secret, auth_token) {
13+
this._request= new Request(api_key, shared_secret, auth_token);
14+
15+
this.people= new People(this._request);
16+
this.photos= new Photos(this._request);
17+
this.photosets= new Photosets(this._request);
18+
this.auth= new Auth(this._request);
19+
this.blogs= new Blogs(this._request);
20+
};
21+
22+
FlickrAPI.prototype.setAuthenticationToken= function(auth_token) {
23+
this._request.setAuthenticationToken(auth_token);
24+
};
25+
26+
FlickrAPI.prototype.getLoginUrl= function(permissions, frob) {
27+
var promise= new process.Promise();
28+
if( frob === undefined ) {
29+
var self= this;
30+
this.auth.getFrob()
31+
.addErrback(function(err) {promise.emitError(err);})
32+
.addCallback(function(frob){
33+
var sig= self._request.generateSignature(self.shared_secret, {
34+
"api_key": self.api_key,
35+
"perms": permissions,
36+
"frob":frob
37+
});
38+
promise.emitSuccess("http://flickr.com/services/auth/?api_key="+self.api_key+"&perms="+permissions+"&frob="+frob+"&api_sig="+ sig, frob)
39+
});
40+
} else {
41+
var sig= generate_signature(this.shared_secret, {
42+
"api_key": this.api_key,
43+
"perms": permissions,
44+
"frob":frob
45+
});
46+
promise.emitSuccess("http://flickr.com/services/auth/?api_key="+self.api_key+"&perms="+permissions+"&frob="+frob+"&api_sig="+ sig, frob)
47+
}
48+
return promise;
49+
};
50+
51+
52+
exports.FlickrAPI = FlickrAPI;

lib/people.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
People= function People(request) {
2+
this._request= request;
3+
};
4+
5+
People.prototype.findByUsername= function(username){
6+
return this._request.getRequestPromise("flickr.people.findByUsername",
7+
{"username": username});
8+
};
9+
10+
exports.People= People;

0 commit comments

Comments
 (0)