|
|
|
|
Gigya's Developer Documentation > Wildfire Sharing Tool > Additional ActionScript APIs > Developer's Guide
Developer's GuideFrom $1Table of contents
Welcome to the developer's documentation for Gigya ActionScript API. The Developer's Guide is a practical tutorial-like guide for widget developers who wish to incorporate Gigya ActionScript API in their widget.
Basic ConceptsAn Asynchronous Programming ModelGigya Socialize uses an asynchronous programming model in which operations are triggered and then run in the background until they are completed. Upon successful or unsuccessful completion, the operation invokes a callback function - which is provided by the developer, and returns a response object that includes the results of the operation. The callback function should handle the response in an appropriate manner, as determined by the application. The same callback function may be used to handle multiple events. Definitions:Callback function - Callback functions are used to return results of Gigya ActionScript API function calls when the method is expected to take a long time to execute. This programming model is known as asynchronous because the result of a function call is not returned when the function call returns but instead it is returned after a while by calling the associated callback function. Context object - Context objects are commonly used in asynchronous programming to allow the application to store and pass information between callbacks. All the Gigya ActionScript API method calls can accept a context object and then pass it back to the application as a parameter of the callback function. Gigya ActionScript API never attempts to read the context object and it is left to the application to create and interpret its meaning.
The API namespaceThe Gigya API uses a 3 level hierarchical namespace, for specifying method names, with the scheme: The only exception to this rule is the gigya.load() method, which is described below.
Getting StartedUsing Gigya API is a four step process:
Step 1: Include the gigya.swc file in your projectDownload the gigya.swc file appropriate for your development environment from the Gigya website: gigya.swc for ActionScript2 or gigya.swc for ActionScript3. Unzip the file and include it in your project:
click the "Browse to SWC file" button (red icon) > browse and select gigya.swc file > click 'OK' on both windows. ![]() Place the file in your "libs" directory:
The gigya.swc file provides boot strapping code for the Gigya API and should never be changed. We encourage you to get the latest version of this file whenever you make changes to your project so that it would include the latest improvements and fixes. Note: The gigya.swc will implicitly give the Gigya API code permissions to access your SWF's data members. If the Gigya API is used in a SWF which is loaded by another SWF, or within an hierarchy of other SWFs, then all the parent SWFs must give Gigya permissions to access them as well. Implement that by adding the following lines of code:
System.security.allowDomain("cdn.gigya.com");
System.security.allowInsecureDomain("cdn.gigya.com")
Security.allowDomain("cdn.gigya.com");
Security.allowInsecureDomain("cdn.gigya.com");
Step 2: Create a global configuration objectEvery Gigya API method requires a configuration object as its first parameter. This object is used for storing global configuration parameters, which are common to all the API calls and are expected to remain static for the lifetime of the application. The configuration object can be created once and passed to all the method calls in the lifetime of the application. Currently the object's members are:
Best practice is, to define one global 'conf' object and use it throughout the application:
// Step 2 - Define a configuration object. Insert your Gigya partner-ID below:
var loadConf:Object = {
mcRoot: _root,
pid: "Put Your partner-ID here"
}
// Step 2 - Define a configuration object. Insert your Gigya partner-ID below:
var conf:Object = {
mcRoot: root,
pid: "Put Your partner-ID here"
}
// Step 2 - Define a configuration object. Insert your Gigya partner-ID below:
var conf:Object = {
pid: "Put Your partner-ID here"
}
Notes:
1. Modify "Put Your partner-ID here" in the code, to your Gigya partner-ID. 2. In Flex mcRoot parameter can not be initialized statically, since the value this.root can not be accessed statically. Instead it should be initialized inside the init() method: // Initialize the "mcRoot" attribute of the conf object - should refer to the root of the flash container this.conf.mcRoot=this.root;
Step 3: Load the required service/sPrior to using Gigya API methods the corresponding services must be loaded. Loading services is done by executing the gigya.load(conf, params) method and waiting for your callback method to be called with an indication if the load operation being successful or not. Note: You should not call any API method before your load callback method is called and an indication of successful load is received.
Step 3.1: Create a load's params objectEvery Gigya API method requires a "params" object as its second parameter. The params object enfolds all the specific parameters sent to the API method. The method gigya.load expects the following members in the params object:
In other words: the gigya.load method requires a parameter called 'services', in addition to the optional parameters 'callback' and 'context'. The 'var loadParams' object in the following example conforms to this definition, and thus can be passed as the second parameter of the gigya.load method: // 3.1 Create the load-parameters object
var loadParams:Object = {
services: 'social,reports',
callback: onGigyaInit
}
Step 3.2: Execute the gigya.load MethodCall the API method gigya.load. Transfer the 'conf', and 'params' objects as parameters: // 3.2 Load the services gigya.load(conf,loadParams);
Step 3.3: Define a Callback Function for the Load OperationThe API uses an asynchronous programming model, meaning - that gigya.load will run in the background and call a developer provided callback function when it completes. Recall that we specified the callback function in the "callback" attribute of the params object - callback:onGigyaInit. Thus in our example the onGigyaInit function is called when the processing of the method gigya.load completes: // 3.3 Wait for the load to complete and handle failure\success
function onGigyaInit(response:Object): void
{
if (res.hadError) {
// handle failure
trace('An error has occurred while attemting to load Socilaize service');
} else {
// successful load
//continue knowing that the required services are now available.
//please note that some services may require initialization after they are loaded.
}
}
The callback function is a developer-defined function. The only constraint is on its signature - Gigya expects the callback function to receive one parameter, which is the response object. The response object contains the values returned from the API method. The callback function should analyze the response object and handle it appropriately. For example - display an error message to the end-user if an error has occurred. The response object for the gigya.load operation include the following members:
The callback function should initially check the value of hadError field as indication for load failure\success.
Step 4: Making API callsStep 4.1: Create a method specific params objectAs mentioned above, every Gigya API method requires a "params" object as the second parameter. The params object enfolds all the specific parameters sent to the API method. Each method in the Gigya API accepts different set of parameters, as appropriate for that method. Additionally, all the methods in the Gigya API accept the following common parameters:
The API Reference, specifies the list of parameters ("params" object members) which each API method accepts - some are required and some are optional. Note: It is important to define an independent params object per method call, as opposed to editing and reusing the same object for several method calls. This is due to the fact that the methods are running asynchronously. Best practice is to concatenate the method name to the params object name. For example:
var paramsGetGeo:Object = {
callback: getGeoResponse,
cid: '123'
};
gigya.services.env.getGeo(conf,paramsGetGeo);
var paramsGetFriends:Object = {
callback: getFriendsResponse
};
gigya.services.social.getFriends(conf, paramsGetFriends);
Another option is to define the params object inline. For example:gigya.services.env.getGeo(conf, {
callback: getGeoResponse,
cid: '123'
});
gigya.services.social.getFriends(conf, {
callback: getFriendsResponse
});
Step 4.2: Execute the API MethodAll the methods in the Gigya API (except the gigya.load method) have a common signature: gigya.services.service_name.method_name(conf,params);
Both "conf" and "params" are actually objects that contain multiple members used for configuring the method. We choose to use these two objects instead of passing paremeters in the classical way to give us flexibility in adding new parameters without breaking older code.
The API uses an asynchronous programming model where operations are started by calling and API method, run in the background and call a user supplied callback function when they are complete. The callback function is called regardless if the operation succeeded of failed, so the application can handle both cases gracefully. A single callback function may be used to handle multiple events, in this case the application can set a context object when it calls the API and the callback function can later use this object to determine the context in which it was called and react accordingly.
Step 4.3: Define a Callback function (optional)When an API call is made it returns immediately and starts processing the request in the background. When the processing is complete the user provided callback function is called with the purpose of passing the API method's return values. The callback function is a developer-defined function. The only constraint is on its signature - Gigya expects the callback function to receive one parameter, which is the response object. The response object contains the values returned from the specific API method. The callback function should analyze the response object and handle it appropriately. For example - display an error message to the end-user if an error has occurred. In particular, the callback function should verify that the status field of the response object is "OK", as any other value indicates an error. Every response object contains common members (values which are returned by all API methods). The common members of the response object are specified in the following table:
Each API method defines its additional members in the response object. Method specific members are detailed in the reference section for each API call.
For example, in the following code we define a callback method for the social.getFriends API method: // Step 4.3 - Define callback function:
function getFriendsResponse(response: Object): void {
if (response.hadError)
{
// handle errors
trace(response.errorMessage);
} else {
//Use the friends data
var friends:Array = response.friends.asArray();
}
}
Notice how we used the response object in the implementation of getFriendsResponse(response) function: We used some of the common data members: response.hadError, response.errorMessage. We also used the response.friends data member, which is specific to the getFriends method.
What's Next?You should now be ready to start using the basic features of Gigya ActionScript API. You may consult the API Reference section - for full specifications of the Gigya Socialize API
< Back to 'Gigya ActionScript API' | Next to 'API Reference' >
Tags:
|
||||||||||||||||||||||||||||||||||
|