DiFi object - danopia/deviantart-difi GitHub Wiki

DiFi object

The DiFi object is a globally defined javascript end-point that is responsible to handle the DiFi calls in the DeviantArt website.

It is basically a system that allows the JavaScript on the page to communicate with the server -- and vice versa -- by using asynchronous ajax calls. The object appears to be defined in the deviantart-network.js files, and communicates with the DiFi classes backend on deviantART servers (which is written in PHP).

When the JavaScript executes DiFi.push() or DiFi.pushPost(), it is basically loading the request mentioned in the arguments into a queue. Whenever DiFi.send() is called, the page scripts traverse through that queue, parsing the request into a DiFi-style URL and sending them with the specified verb (either GET or POST).

Callback is the function called when the DiFi request finishes; it takes two arguments: success and data (the response from server).

More examples are available in the JS Access page.

More information about the DiFi object can be acquired by studying the /jms/lib/difi.js file.

Methods

  • push (type, api, method, args, callback, callback_obj)
  • pushPost is an alias of push('post', ...)
  • pushPrivateGet is an alias of push('private', ...)
  • pushPublicGet is an alias of push('public', ...)
  • pushPublicStaticGet is an alias of push('static', ...)
  • setAnalogPayload (key, value) – sets the ap parameter (refer to Protocol#Optional-Additional-Parameters)
  • send

Mechanism

  1. push() adds each called DiFi request to a queue buffer
  2. send() then executes the requests by creating a DiFi URL and gets the result
  3. The callback for each call on the queue is called with the results passed to it

NOTE: The native DiFi object will always use either json or jsonp to retrieve data.

POST

POST is the most common method of DiFi communication, which is often used to perform actions, or to retrieve large requests.

A benefit of POST requests is that they are not cacheable.

Private GET

This method is used to retrieve user-specific information via HTTP GET.

Public GET

This method is used to retrieve public information that can be accessed without being logged-in via HTTP GET.

Static GET

The static GET is the only DiFi method that uses jsonp format for data transmission. Unlike private and public GET methods, the static method will not use XMLHttpRequest (i.e. Ajax) to perform actions, instead it attaches a new <script> element to the document, which in turn will execute the internal DiFi._callbackSI callback.

Examples

// Set an example object to hold our data
var options = { "friendid": "37591934" }

// This is the example callback
var cb = function(success, data) {
	if (!success) {
		// . . . handle errors
		alert("The DiFi call that we made was not successful.");
		return DiFi.stdErr('', data.response.content);
	}
	else {
		// Get the call contents
		var content = data.response.content;  // console.log(content);

		// Get the `realname` content as an example:
		var real_name = content.realname;
		alert( `The userid ${options.friendid}'s realname is: ` + real_name );
	}
}

// Create a new DiFi call and push it to the queue
DiFi.pushPost("Friends", "getFriendAttributesAndGroup", [ options.friendid ], cb );

// Now send our request
DiFi.send();

Remarks

Arguments

The DiFi object converts all numbers into strings, because some browsers (e.g. Opera) have had JSON.stringify() bugs; as stated in the source-code.

Callbacks

Function.call() allows you to set what this means to the function. It is as if you are temporarily assigning a value to this for the duration of the function. It works like this:

myFunction.call(document.getElementById("myLink"), [arguments]);

So the DiFi calls your callback like this:

callback.call(callback_obj, success, data);
⚠️ **GitHub.com Fallback** ⚠️