Guest User

Simple SAFE API simple_safe.js

a guest
Aug 25th, 2016
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var token = localStorage["token"];
  2. // Wait for the page to be fully loaded
  3. document.addEventListener('DOMContentLoaded', function() {
  4.     document.getElementById("auth_button").addEventListener("click", function(){
  5.         authorize();
  6.     });
  7.  
  8.     document.getElementById("nfs_post_button").addEventListener("click", function(){
  9.         var filename = document.getElementById("filename").value;
  10.         var content = document.getElementById("content").value;
  11.         post(filename, content);
  12.     });
  13.  
  14.     document.getElementById("nfs_get_button").addEventListener("click", function(){
  15.         var filename = document.getElementById("filename").value;
  16.         get(filename);
  17.     });
  18.  
  19.     document.getElementById("nfs_delete_button").addEventListener("click", function(){
  20.         var filename = document.getElementById("filename").value;
  21.         remove(filename);
  22.     });
  23. }, false);
  24.  
  25.  
  26. function authorize() {
  27.     console.log("authorizing...");
  28.  
  29.     // Prepare the request
  30.     var request = new XMLHttpRequest();
  31.     request.open("post", "http://api.safenet/auth", true);
  32.     request.setRequestHeader("Content-type", "application/json");
  33.  
  34.     // Create callback functions to handle the response.
  35.     request.onerror = function () {
  36.         console.log(this.responseText);
  37.     };
  38.     request.onload = function() {
  39.         if (this.status == 200) {
  40.             console.log("SUCCESS:" + this.responseText);
  41.             var response = JSON.parse(this.responseText);
  42.             token = response.token;
  43.             localStorage["token"] = token;
  44.         } else {
  45.             console.log("ERROR:" + this.responseText);
  46.         }
  47.     };
  48.  
  49.     // Create the payload that will be sent with the request
  50.     var payload = {
  51.         app: {
  52.             name: "Simple SAFE API example",
  53.             version: "1.0.0",
  54.             vendor:  "your_name",
  55.             id: "your_app_id"
  56.         },
  57.         permissions: ["SAFE_DRIVE_ACCESS"]
  58.     };
  59.  
  60.     // Send the request
  61.     var payload_string = JSON.stringify(payload);
  62.     request.send(payload_string);
  63. }
  64.  
  65. function post(filename, content) {
  66.     console.log("post file:" + filename);
  67.  
  68.     // Prepare the request
  69.     var request = new XMLHttpRequest();
  70.     request.open("post", "http://api.safenet/nfs/file/app/" + filename, true);
  71.     request.setRequestHeader("Content-type", "application/json");
  72.     request.setRequestHeader('Authorization', 'Bearer ' + token);
  73.  
  74.     // Create callback functions to handle the response.
  75.     request.onerror = function () {
  76.         console.log(this.responseText);
  77.     };
  78.     request.onload = function() {
  79.         if (this.status == 200) {
  80.             console.log("SUCCESS:" + this.responseText);
  81.         } else {
  82.             console.log("ERROR:" + this.responseText);
  83.         }
  84.     };
  85.  
  86.     // Create the payload that will be sent with the request
  87.     var payload = {
  88.         content:content
  89.     };
  90.  
  91.     // Send the request
  92.     var payload_string = JSON.stringify(payload);
  93.     request.send(payload_string);
  94. }
  95.  
  96. function get(filename) {
  97.     console.log("get file:" + filename);
  98.  
  99.     // Prepare the request
  100.     var request = new XMLHttpRequest();
  101.     request.open("get", "http://api.safenet/nfs/file/app/" + filename, true);
  102.     request.setRequestHeader("Content-type", "application/json");
  103.     request.setRequestHeader('Authorization', 'Bearer ' + token);
  104.  
  105.     // Create callback functions to handle the response.
  106.     request.onerror = function () {
  107.         console.log(this.responseText);
  108.     };
  109.     request.onload = function() {
  110.         if (this.status == 200) {
  111.             console.log("SUCCESS:" + this.responseText);
  112.         } else {
  113.             console.log("ERROR:" + this.responseText);
  114.         }
  115.     };
  116.  
  117.     // There's no payload for the get request
  118.  
  119.     // Send the request
  120.     request.send();
  121. }
  122.  
  123. function remove(filename) {
  124.     console.log("delete file:" + filename);
  125.  
  126.     // Prepare the request
  127.     var request = new XMLHttpRequest();
  128.     request.open("delete", "http://api.safenet/nfs/file/app/" + filename, true);
  129.     request.setRequestHeader("Content-type", "application/json");
  130.     request.setRequestHeader('Authorization', 'Bearer ' + token);
  131.  
  132.     // Create callback functions to handle the response.
  133.     request.onerror = function () {
  134.         console.log(this.responseText);
  135.     };
  136.     request.onload = function() {
  137.         if (this.status == 200) {
  138.             console.log("SUCCESS:" + this.responseText);
  139.         } else {
  140.             console.log("ERROR:" + this.responseText);
  141.         }
  142.     };
  143.  
  144.     // There's no payload for the delete request
  145.  
  146.     // Send the request
  147.     request.send();
  148. }
RAW Paste Data