close

  
<p> (function () {

  var top = this.__proto__; // get access to the script global object
  
  // ---------------------- Freebase API Enhancements ----------------------

  function post(url,headers,content,sign) {
    if (!headers) headers = {};
    headers['X-Metaweb-Request'] = "1";
    return check_results(JSON.parse(acre.urlfetch(url, "POST", headers, content, sign).body));
  }

  /**
  * Make sure that the JSON result generated by the freebase API is not erroneous
  */
  
  function check_results(result) {
    if (result.status != "200 OK") {
      var exception = new FreebaseError('HTTP error: ' + result.status);
      exception.response = result;
      throw exception;
    }
    if (result.code != "/api/status/ok") {
      var exception = new FreebaseError(result.code + ': ' + result.messages[0])
          exception.response = result;
      throw exception;
    }
    return result;
  }

  /**
  * Prepare the content payload of the POST request
  */
  function prepareContent(query, envelope, params) {
    if (!query) throw new Error("You must provide a query");
    if (!envelope) envelope = {};
    envelope.query = query;
    envelope.escape = false;
    if (!params) params = {};
    params.query = JSON.stringify(envelope);
    return acre.formencode(params);
  }

  /**
  * Perform a mqlwrite
  */
  top.acre.freebase.upload = function(document) {
    acre.response.set_header('mwLastWriteTime', 1);
    return post("http://api.freebase.com/api/service/upload?text_encoding=utf-8&amp;media_type=text/plain", {}, document, true);
  };

                  
})();
 </p>

Comments

Hide