api calls

This commit is contained in:
Jeena Paradies 2013-05-26 09:51:01 +02:00
parent 422dbd34ff
commit f73ce7d196
10 changed files with 371 additions and 86 deletions

View file

@ -0,0 +1,285 @@
define([
"jquery",
"helper/HostApp",
"helper/Hmac",
"helper/Cache"
],
function(jQuery, HostApp, Hmac, Cache) {
var APICalls = {};
APICalls.cache = new Cache();
APICalls.getUrlVars = function(url) {
var vars = [], hash;
if(url.indexOf("#") > -1) url = url.slice(0, url.indexOf("#"));
var hashes = url.slice(url.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
/*
APICalls.http_call = function(url, http_method, callback, data, auth_header, accepts) {
if(accepts !== false) accepts = accepts || "application/vnd.tent.post.v0+json";
var options = {
beforeSend: function(xhr) {
if(accepts !== false) xhr.setRequestHeader("Accept", accepts);
if (data) xhr.setRequestHeader("Content-Length", data.length);
if (auth_header) { // if is_set? auth_header
xhr.setRequestHeader("Authorization", auth_header);
} else {
var user_access_token = HostApp.stringForKey("user_access_token");
if (auth_header !== false && typeof user_access_token != "undefined") {
auth_header = Hmac.makeAuthHeader(
url,
http_method,
HostApp.secret(),
user_access_token
);
xhr.setRequestHeader("Authorization", auth_header);
}
}
},
url: url,
contentType: 'application/vnd.tent.post.v0+json; type="https://tent.io/types/app/v0#"',
type: http_method,
complete: callback,
data: data,
processData: false,
error: function(xhr, ajaxOptions, thrownError) {
console.error("getURL (" + xhr.status + ")" + xhr.statusText + " " + http_method + " (" + url + "): '" + xhr.responseText + "'");
}
}
debug(url)
jQuery.ajax(options);
}
*/
APICalls.http_call = function(options) {
if(!options.content_type) {
console.error("No content type for " + options.url);
return;
}
var settings = {
beforeSend: function(xhr) {
if (options.data) xhr.setRequestHeader("Content-Length", data.length);
if (options.accept) xhr.setRequestHeader("Accept", "application/vnd.tent.post.v0+json");
var user_access_token = HostApp.stringForKey("user_access_token");
if (!no_auth && user_access_token) {
var auth_header = Hmac.makeHawkAuthHeader(
options.url,
options.http_method,
HostApp.secret(),
user_access_token
);
xhr.setRequestHeader("Authorization", auth_header);
} else {
console.error("No user_access_token yet - " + options.url);
}
}
url: options.url,
contentType: options.content_type,
type: url.http_method,
complete: options.callback,
data: options.data,
processData: false,
error: function(xhr, ajaxOptions, thrownError) {
console.error("HTTP CALL (" + xhr.status + ")" + xhr.statusText + " " + options.http_method + " (" + options.url + "): '" + xhr.responseText + "'");
}
};
jQuery.ajax(settings);
}
APICalls.get = function(url, options) {
var settings = {
url: url,
http_method: "GET",
accept: null,
data: null,
no_auth: false
content_type: null
};
jQuery.extend(settings, options);
APICalls.http_call(settings);
}
APICalls.post = function(url, data, options) {
var settings = {
url: url,
http_method: "POST",
data: data
};
jQuery.extend(settings, options);
APICalls.http_call(settings);
}
APICalls.postMultipart = function(url, callback, data, boundary, accepts) {
accepts = accepts || "application/vnd.tent.v0+json";
jQuery.ajax({
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", accepts);
if (data) xhr.setRequestHeader("Content-Length", data.length);
var user_access_token = HostApp.stringForKey("user_access_token");
if (user_access_token) {
auth_header = Hmac.makeAuthHeader(
url,
"POST",
HostApp.secret(),
user_access_token
);
xhr.setRequestHeader("Authorization", auth_header);
}
},
url: url,
contentType: "multipart/form-data;boundary=" + boundary,
type: "POST",
complete: callback,
data: data,
processData: false,
error: function(xhr, ajaxOptions, thrownError) {
console.error("postMultipart (" + xhr.status + ")" + xhr.statusText + " (" + url + "): '" + xhr.responseText + "'");
}
});
}
APICalls.findProfileURL = function(entity, callback, errorCallback) {
var profile_url = APICalls.cache.profile_urls.getItem(entity);
if (profile_url && profile_url != "null") {
callback(profile_url);
} else {
jQuery.ajax({
url: entity,
type: "HEAD",
complete: function(resp) {
if(resp) {
var headers = resp.getAllResponseHeaders();
var profile_urls = APICalls.parseHeaderForProfiles(headers);
var profile_url = null;
if(profile_urls.length > 0) {
var profile_url = profile_urls[0];
if (!profile_url.startsWith("http")) {
profile_url = entity + profile_url;
}
}
if (profile_url) {
APICalls.cache.profile_urls.setItem(entity, profile_url);
callback(profile_url);
} else {
APICalls.http_call(entity, "GET", function(resp) {
if (resp.status >= 200 && resp.status < 300) {
var doc = document.implementation.createHTMLDocument("");
doc.documentElement.innerHTML = resp.responseText;
var links = $(doc).find("link[rel='https://tent.io/rels/meta-post']");
if (links.length > 0) {
var href = links.get(0).href;
APICalls.cache.profile_urls.setItem(entity, href);
if (!href.startsWith("http")) {
href = entity + href;
}
callback(href);
} else {
if(errorCallback) errorCallback(entity + " has no profile URL");
}
} else {
if(errorCallback) errorCallback(entity + " has no profile URL");
}
}, null, false, false);
//if(errorCallback) errorCallback(entity + " has no profile URL");
}
}
},
error: function(xhr, ajaxOptions, thrownError) {
console.error("findProfileURL " + xhr.statusText + " (" + entity + "): " + xhr.responseText);
if (errorCallback) errorCallback(xhr.statusText + " - " + xhr.responseText)
}
});
}
}
APICalls.mkApiRootPath = function(path) {
var api_root = HostApp.stringForKey("api_root");
if((api_root.substring(api_root.length - 1, api_root.length) != "/") && (path.substring(0, 1) != "/")) {
api_root += "/";
} else if((api_root.substring(api_root.length - 1, api_root.length) == "/") && (path.substring(0, 1) == "/")) {
api_root = api_root.substring(0, api_root.length -1);
}
return api_root + path;
}
APICalls.parseHeaderForProfiles = function(header_string) {
var regexp = /https:\/\/tent.io\/rels\/meta-post/i;
return APICalls.parseHeaderForLink(header_string, regexp);
}
APICalls.parseHeaderForLink = function(header_string, match) {
var headers = header_string.split(/\n/);
var links = [];
for (var i = 0; i < headers.length; i++) {
var header = headers[i];
if (header.match(/^Link:(.*)/i)) {
links.push(header.replace(/\r/, "").substr(5).trim());
}
}
var items = [];
for (var i = 0; i < links.length; i++) {
items = items.concat(links[i].split(","));
}
var things = [];
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (item.match(match)) {
var n = item.match(/<([^>]*)>/);
if (n) {
things.push(n[1]);
}
}
}
return things;
}
return APICalls;
});

View file

@ -40,12 +40,12 @@ function(URI, CacheStorage, require) {
}
}
var url = URI(require("helper/Paths").mkApiRootPath("/followings"));
var url = URI(require("helper/APICalls").mkApiRootPath("/followings"));
if (this.followings_before_id) {
url.addSearch("before_id", this.followings_before_id);
}
require("helper/Paths").getURL(url, "GET", callback);
require("helper/APICalls").getURL(url, "GET", callback);
}
Cache.prototype.periodicallyGetFollowings = function() {

View file

@ -1,6 +1,6 @@
define([
"jquery",
"helper/Paths",
"helper/APICalls",
"lib/URI",
"helper/HostApp",
"helper/Cache",
@ -8,7 +8,7 @@ define([
"lib/SingleDoubleClick"
],
function(jQuery, Paths, URI, HostApp, Cache) {
function(jQuery, APICalls, URI, HostApp, Cache) {
function Core() {
this.cache = new Cache();
@ -247,10 +247,10 @@ function(jQuery, Paths, URI, HostApp, Cache) {
} else {
Paths.findProfileURL(status.entity, function(profile_url) {
APICalls.findProfileURL(status.entity, function(profile_url) {
if (profile_url) {
Paths.getURL(profile_url, "GET", function(resp) {
APICalls.http_call(profile_url, "GET", function(resp) {
var p = JSON.parse(resp.responseText);
if (p && p != "null") {
_this.cache.profiles.setItem(status.entity, p);
@ -308,11 +308,11 @@ function(jQuery, Paths, URI, HostApp, Cache) {
}
if (status.entity == HostApp.stringForKey("entity")) {
var url = Paths.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name);
Paths.getURL(url, "GET", callback, null, null, attachment.type);
var url = APICalls.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name);
APICalls.http_call(url, "GET", callback, null, null, attachment.type);
} else {
var url = Paths.mkApiRootPath("/posts/" + encodeURIComponent(status.entity) + "/" + status.id + "/attachments/" + attachment.name);
Paths.getURL(url, "GET", callback, null, null, attachment.type);
var url = APICalls.mkApiRootPath("/posts/" + encodeURIComponent(status.entity) + "/" + status.id + "/attachments/" + attachment.name);
APICalls.http_call(url, "GET", callback, null, null, attachment.type);
}
})();
}
@ -431,9 +431,9 @@ function(jQuery, Paths, URI, HostApp, Cache) {
});
var _this = this;
Paths.findProfileURL(repost.entity, function(profile_url) {
APICalls.findProfileURL(repost.entity, function(profile_url) {
if (profile_url) {
Paths.getURL(profile_url, "GET", function(resp) {
APICalls.http_call(profile_url, "GET", function(resp) {
if (resp.status >= 200 && resp.status < 400) {
var _p = JSON.parse(resp.responseText);
_this.cache.profiles.setItem(repost.entity, _p);
@ -460,14 +460,14 @@ function(jQuery, Paths, URI, HostApp, Cache) {
}
}
Paths.findProfileURL(repost.content.entity, function(profile_url) {
APICalls.findProfileURL(repost.content.entity, function(profile_url) {
if (profile_url) {
Paths.getURL(profile_url, "GET", function(resp) {
APICalls.http_call(profile_url, "GET", function(resp) {
var profile = JSON.parse(resp.responseText);
var server = profile["https://tent.io/types/info/core/v0.1.0"].servers[0];
Paths.getURL(URI(server + "/posts/" + repost.content.id).toString(), "GET", callback, null, false);
APICalls.http_call(URI(server + "/posts/" + repost.content.id).toString(), "GET", callback, null, false);
}, null, false); // do not send auth-headers
}
@ -483,7 +483,7 @@ function(jQuery, Paths, URI, HostApp, Cache) {
} else {
var url = URI(Paths.mkApiRootPath("/posts"));
var url = URI(APICalls.mkApiRootPath("/posts"));
var http_method = "POST";
@ -517,13 +517,13 @@ function(jQuery, Paths, URI, HostApp, Cache) {
}
}
Paths.getURL(url.toString(), http_method, callback, JSON.stringify(data));
APICalls.http_call(url.toString(), http_method, callback, JSON.stringify(data));
}
}
Core.prototype.repost = function(id, entity, callback) {
var url = URI(Paths.mkApiRootPath("/posts"));
var url = URI(APICalls.mkApiRootPath("/posts"));
var data = {
"type": "https://tent.io/types/post/repost/v0.1.0",
@ -549,12 +549,12 @@ function(jQuery, Paths, URI, HostApp, Cache) {
_this.highlight(id);
}
Paths.getURL(url.toString(), "POST", new_callback, JSON.stringify(data));
APICalls.http_call(url.toString(), "POST", new_callback, JSON.stringify(data));
}
Core.prototype.sendNewMessageWithImage = function(content, in_reply_to_status_id, in_reply_to_entity, location, image_data_uri, is_private, callback) {
var url = URI(Paths.mkApiRootPath("/posts"));
var url = URI(APICalls.mkApiRootPath("/posts"));
var data = {
"type": "https://tent.io/types/post/photo/v0.1.0",
@ -623,14 +623,14 @@ function(jQuery, Paths, URI, HostApp, Cache) {
callback(resp);
}
Paths.postMultipart(url.toString(), newCallback, post, boundary);
APICalls.postMultipart(url.toString(), newCallback, post, boundary);
}
Core.prototype.remove = function(id, callback, type) {
type = type || "post";
if (confirm("Really delete this " + type + "?")) {
var url = URI(Paths.mkApiRootPath("/posts/" + id));
Paths.getURL(url.toString(), "DELETE", callback);
var url = URI(APICalls.mkApiRootPath("/posts/" + id));
APICalls.http_call(url.toString(), "DELETE", callback);
}
}
@ -738,9 +738,9 @@ function(jQuery, Paths, URI, HostApp, Cache) {
} else {
Paths.findProfileURL(mention.entity, function(profile_url) {
APICalls.findProfileURL(mention.entity, function(profile_url) {
if (profile_url) {
Paths.getURL(profile_url, "GET", function(resp) {
APICalls.http_call(profile_url, "GET", function(resp) {
if (resp.status >= 200 && resp.status < 400) {
var p = JSON.parse(resp.responseText);
_this.cache.profiles.setItem(mention.entity, p);
@ -838,7 +838,7 @@ function(jQuery, Paths, URI, HostApp, Cache) {
} else if(word.startsWith("http://youtube.com/") || word.startsWith("http://www.youtube.com/") || word.startsWith("https://youtube.com/") || word.startsWith("https://www.youtube.com/")) {
var v = Paths.getUrlVars(word)["v"];
var v = APICalls.getUrlVars(word)["v"];
this.addYouTube(v, images);
} else if (word.startsWith("http://youtu.be/") || word.startsWith("https://youtu.be/")) {