@@ -4,8 +4,17 @@ var sys = require("sys"),
4
4
5
5
var flickr_request = http . createClient ( 80 , "api.flickr.com" ) ;
6
6
7
- var Request = function Request ( api_key , shared_secret , auth_token ) {
7
+ var Request = function Request ( api_key , shared_secret , auth_token , isFeedRequest ) {
8
8
this . _configure ( api_key , shared_secret , auth_token ) ;
9
+ this . isFeedRequest = false ;
10
+ if ( isFeedRequest !== undefined ) this . isFeedRequest = isFeedRequest ;
11
+
12
+ if ( this . isFeedRequest ) {
13
+ this . baseUrl = "/services/feeds" ;
14
+ }
15
+ else {
16
+ this . baseUrl = "/services/rest" ;
17
+ }
9
18
} ;
10
19
11
20
Request . prototype . _configure = function ( api_key , shared_secret , auth_token ) {
@@ -41,19 +50,26 @@ Request.prototype.getRequestPromise= function(method, arguments, sign_it, result
41
50
var promise = new process . Promise ( )
42
51
var argumentString = "" ;
43
52
var api_sig = undefined ;
53
+
44
54
if ( arguments === undefined ) arguments = { } ;
45
55
46
56
// apply default arguments
47
57
arguments . format = "json" ;
48
58
arguments . nojsoncallback = "1" ;
49
- arguments [ "method" ] = method ;
50
- arguments . api_key = this . api_key ;
51
- if ( this . auth_token ) arguments . auth_token = this . auth_token ;
59
+
60
+ if ( this . isFeedRequest ) {
61
+ argumentString = "/" + method ;
62
+ }
63
+ else {
64
+ arguments . api_key = this . api_key ;
65
+ arguments [ "method" ] = method ;
66
+ if ( this . auth_token ) arguments . auth_token = this . auth_token ;
52
67
53
- if ( this . shared_secret && ( sign_it || this . auth_token ) ) {
54
- api_sig = this . generateSignature ( this . shared_secret , arguments ) ;
55
- if ( api_sig ) {
56
- arguments . api_sig = api_sig ;
68
+ if ( this . shared_secret && ( sign_it || this . auth_token ) ) {
69
+ api_sig = this . generateSignature ( this . shared_secret , arguments ) ;
70
+ if ( api_sig ) {
71
+ arguments . api_sig = api_sig ;
72
+ }
57
73
}
58
74
}
59
75
var operator = "?" ;
@@ -62,30 +78,49 @@ Request.prototype.getRequestPromise= function(method, arguments, sign_it, result
62
78
if ( operator == "?" ) operator = "&" ;
63
79
}
64
80
var request = flickr_request . request ( "GET" ,
65
- "/services/rest" + argumentString ,
81
+ this . baseUrl + argumentString ,
66
82
{ "host" : "api.flickr.com" } ) ;
83
+ var isFeedRequest = this . isFeedRequest ;
67
84
request . finish ( function ( response ) {
68
85
var result = "" ;
69
86
response . setBodyEncoding ( "utf8" ) ;
70
87
response . addListener ( "body" , function ( chunk ) {
71
88
result += chunk ;
72
89
} ) ;
73
90
response . addListener ( "complete" , function ( ) {
74
- var res = JSON . parse ( result ) ;
75
- if ( res . stat == "ok" ) {
76
- // Munge the response to strip out the stat and just return the response value
77
- for ( var key in res ) {
78
- if ( key !== "stat" ) {
79
- res = res [ key ] ;
80
- }
91
+ if ( isFeedRequest ) {
92
+ //Dear-god this is bound to fail horribly.
93
+ var m ;
94
+ if ( ! result || ( m = result . match ( / [ \S \s ] + W e w e r e u n a b l e t o g e n e r a t e t h e f e e d y o u r e q u e s t e d , f o r t h e f o l l o w i n g r e a s o n : < \/ p > \s * ? < p [ \S \s ] + ? > ( [ \S \s ] + ? ) < \/ p > [ \S \s ] + / ) ) ) {
95
+ var errorString = m [ 1 ] . replace ( / ^ \s + / , "" ) ;
96
+ errorString = errorString . replace ( / \s + $ / , "" ) ;
97
+ promise . emitError ( { code : - 1 , message : errorString } ) ;
81
98
}
82
- if ( result_mapper ) {
83
- res = result_mapper ( res ) ;
99
+ else {
100
+ var res = JSON . parse ( result ) ;
101
+ if ( result_mapper ) {
102
+ res = result_mapper ( res ) ;
103
+ }
104
+ promise . emitSuccess ( res ) ;
84
105
}
85
- promise . emitSuccess ( res ) ;
86
106
}
87
107
else {
88
- promise . emitError ( { code : res . code , message : res . message } ) ;
108
+ var res = JSON . parse ( result ) ;
109
+ if ( res . stat && res . stat == "ok" ) {
110
+ // Munge the response to strip out the stat and just return the response value
111
+ for ( var key in res ) {
112
+ if ( key !== "stat" ) {
113
+ res = res [ key ] ;
114
+ }
115
+ }
116
+ if ( result_mapper ) {
117
+ res = result_mapper ( res ) ;
118
+ }
119
+ promise . emitSuccess ( res ) ;
120
+ }
121
+ else {
122
+ promise . emitError ( { code : res . code , message : res . message } ) ;
123
+ }
89
124
}
90
125
} ) ;
91
126
} ) ;
0 commit comments