author | Michael Krelin <hacker@klever.net> | 2011-08-28 11:05:26 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2011-08-28 11:05:26 (UTC) |
commit | 4152a504609522fab4a269dc88d13a1078a09452 (patch) (unidiff) | |
tree | a923baa662a97b865eddedb1394085d943289b0a | |
parent | 5d85cdc10a4affbebc8a06d655f820239ececf37 (diff) | |
download | fireflix-4152a504609522fab4a269dc88d13a1078a09452.zip fireflix-4152a504609522fab4a269dc88d13a1078a09452.tar.gz fireflix-4152a504609522fab4a269dc88d13a1078a09452.tar.bz2 |
Add json-responding api calls
It is somewhat more native to javascript. Or could be, at least.
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | content/flickr.js | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/content/flickr.js b/content/flickr.js index e09d5f0..545144c 100644 --- a/content/flickr.js +++ b/content/flickr.js | |||
@@ -89,64 +89,84 @@ Photo.prototype = { | |||
89 | } | 89 | } |
90 | }; | 90 | }; |
91 | 91 | ||
92 | function Flickr() { } | 92 | function Flickr() { } |
93 | Flickr.prototype = { | 93 | Flickr.prototype = { |
94 | 94 | ||
95 | rest_url: 'http://www.flickr.com/services/rest/', | 95 | rest_url: 'http://www.flickr.com/services/rest/', |
96 | auth_url: 'http://flickr.com/services/auth/', | 96 | auth_url: 'http://flickr.com/services/auth/', |
97 | photo_url: 'http://static.flickr.com/', | 97 | photo_url: 'http://static.flickr.com/', |
98 | photos_url: 'http://www.flickr.com/photos/', | 98 | photos_url: 'http://www.flickr.com/photos/', |
99 | upload_url: 'http://www.flickr.com/services/upload/', | 99 | upload_url: 'http://www.flickr.com/services/upload/', |
100 | uploader_edit_url: 'http://www.flickr.com/tools/uploader_edit.gne', | 100 | uploader_edit_url: 'http://www.flickr.com/tools/uploader_edit.gne', |
101 | 101 | ||
102 | api_sig: function(paramstr) { | 102 | api_sig: function(paramstr) { |
103 | return MD5(toutf8(this.api_shs+paramstr)); | 103 | return MD5(toutf8(this.api_shs+paramstr)); |
104 | }, | 104 | }, |
105 | api_call_url: function(params,url) { | 105 | api_call_url: function(params,url) { |
106 | params.api_key = this.api_key; | 106 | params.api_key = this.api_key; |
107 | var pp = new Array(); | 107 | var pp = new Array(); |
108 | for(var p in params) { | 108 | for(var p in params) { |
109 | pp.push(p); | 109 | pp.push(p); |
110 | } | 110 | } |
111 | var pstr = ''; | 111 | var pstr = ''; |
112 | var rv = (url?url:this.rest_url)+'?'; | 112 | var rv = (url?url:this.rest_url)+'?'; |
113 | for(var p in pp.sort()) { | 113 | for(var p in pp.sort()) { |
114 | var pn = pp[p]; | 114 | var pn = pp[p]; |
115 | pstr += pn+params[pn]; | 115 | pstr += pn+params[pn]; |
116 | rv += pn+'='+params[pn]+'&'; | 116 | rv += pn+'='+params[pn]+'&'; |
117 | } | 117 | } |
118 | rv += 'api_sig='+this.api_sig(pstr); | 118 | rv += 'api_sig='+this.api_sig(pstr); |
119 | return rv; | 119 | return rv; |
120 | }, | 120 | }, |
121 | api_call_json: function(params,on_success,on_failure) { | ||
122 | if(params.auth_token=='default') params.auth_token=this.token; | ||
123 | params.format = 'json'; params.nojsoncallback=1; | ||
124 | var x = new XMLHttpRequest(); | ||
125 | x.open("GET",this.api_call_url(params)); | ||
126 | x.onreadystatechange=function() { | ||
127 | if(x.readyState!=4) return false; | ||
128 | if(x.status==200) { | ||
129 | var rsp=JSON.parse(x.responseText); | ||
130 | if(rsp.stat=='ok') | ||
131 | on_success && on_success(x,rsp); | ||
132 | else | ||
133 | on_failure && on_failure(rsp,rsp.stat,rsp.code,rsp.message); | ||
134 | }else | ||
135 | on_failure && on_failure(x); | ||
136 | return true; | ||
137 | }; | ||
138 | x.send(null); | ||
139 | return true; | ||
140 | }, | ||
121 | api_call: function(params, on_success, on_failure) { | 141 | api_call: function(params, on_success, on_failure) { |
122 | if(params.auth_token == 'default') | 142 | if(params.auth_token == 'default') |
123 | params.auth_token = this.token; | 143 | params.auth_token = this.token; |
124 | var x = new XMLHttpRequest(); | 144 | var x = new XMLHttpRequest(); |
125 | x.open("GET",this.api_call_url(params)); | 145 | x.open("GET",this.api_call_url(params)); |
126 | x.onreadystatechange=function() { | 146 | x.onreadystatechange=function() { |
127 | if(x.readyState!=4) return false; | 147 | if(x.readyState!=4) return false; |
128 | if(x.status==200) { | 148 | if(x.status==200) { |
129 | var stat = x.responseXML.firstChild.getAttribute('stat'); | 149 | var stat = x.responseXML.firstChild.getAttribute('stat'); |
130 | if(stat=='ok') { | 150 | if(stat=='ok') { |
131 | if(on_success) on_success(x); | 151 | if(on_success) on_success(x); |
132 | }else{ | 152 | }else{ |
133 | var e = x.responseXML.getElementsByTagName('err').item(0); | 153 | var e = x.responseXML.getElementsByTagName('err').item(0); |
134 | var ecode = e.getAttribute('code'); | 154 | var ecode = e.getAttribute('code'); |
135 | var emsg = e.getAttribute('msg'); | 155 | var emsg = e.getAttribute('msg'); |
136 | dump(params.method+' failed: '+ecode+' '+emsg+'\n'); | 156 | dump(params.method+' failed: '+ecode+' '+emsg+'\n'); |
137 | if(on_failure) on_failure(x,stat,ecode,emsg); | 157 | if(on_failure) on_failure(x,stat,ecode,emsg); |
138 | } | 158 | } |
139 | }else{ | 159 | }else{ |
140 | if(on_failure) on_failure(x); | 160 | if(on_failure) on_failure(x); |
141 | } | 161 | } |
142 | return true; | 162 | return true; |
143 | } | 163 | } |
144 | x.send(null); | 164 | x.send(null); |
145 | return true; | 165 | return true; |
146 | }, | 166 | }, |
147 | 167 | ||
148 | frob: null, | 168 | frob: null, |
149 | authorize_0: function(perms, on_s, on_f) { | 169 | authorize_0: function(perms, on_s, on_f) { |
150 | var _this = this; | 170 | var _this = this; |
151 | this.api_call( | 171 | this.api_call( |
152 | { method: 'flickr.auth.getFrob' }, | 172 | { method: 'flickr.auth.getFrob' }, |