Skip to content

Commit

Permalink
Add full API for saving and loading graphs 🎉
Browse files Browse the repository at this point in the history
The only thing missing froom #18 is a GUI
  • Loading branch information
controversial committed May 9, 2016
1 parent 07999d1 commit ecd01ef
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
44 changes: 39 additions & 5 deletions js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,53 @@
// as well as a more general function which is also used to fetch the README
// for rendering.



// GLOBALS

var api_endpoint = "http://luke.deentaylor.com/wikipedia/api/";

//Make a synchronous request and return the response


// BASIC METHODS

//Make an asynchronous GET request and execute the onSuccess callback with the data
function requestPage(url, onSuccess) {
var data = null;
onSuccess = onSuccess || function(){};
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
onSuccess(xhttp.responseText);
}
};
xhttp.open("GET",url,true);
xhttp.open("GET", url, true);
xhttp.send();
}

//Make an AJAX request to the server while passing a page
function apiRequest(api,page,onSuccess) {
// Send an asynchronous POST request to store data
function postData(url, data, onSuccess) {
onSuccess = onSuccess || function(){};
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
onSuccess(xhttp.responseText);
}
};
xhttp.open("POST", url, true);
xhttp.send(data);
}

// Send an AJAX GET request to the server, passing the name of a Wikipedia page
function apiRequest(api, page, onSuccess) {
var url=api_endpoint+api+"?page="+page;
requestPage(url, function (data){
onSuccess(JSON.parse(data));
});
}

// API calls

// WIKIPEDIA PARSING ----------

//Get the name of all pages linked to by a page
function getSubPages(page,onSuccess) {
Expand All @@ -47,3 +71,13 @@ function getSuggestions(text, onSuccess) {
onSuccess(JSON.parse(data));
});
}

// GRAPH STORAGE / RETRIEVAL ----------

function storeJSON(data, onSuccess) {
postData(api_endpoint + "storejson", data, onSuccess);
}

function getJSON(id, onSuccess) {
requestPage("http://luke.deentaylor.com/wikipedia/graphs/" + id, onSuccess);
}
12 changes: 12 additions & 0 deletions js/network_serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,15 @@ function networkFromJson(data) {

return out;
}



// MAIN FUNCTIONS

function storeGraph(callback) {
storeJSON(networkToJson(), callback);
}

function loadGraph(id) {
getJSON(id, resetNetworkFromJson);
}

0 comments on commit ecd01ef

Please sign in to comment.