Sending Requests Using the SparkRequests API
SparkRequest API allows you to send GameSparks Requests from within Cloud Code.
Creating a Request
The Request object can be easily created using the following notation:
1
2 var authenticationRequest = new SparkRequests.AuthenticationRequest();
3
Setting Request Parameters
All of the parameters that are available for the particular Request, can be set using the following notation:
1
2 authenticationRequest.userName = "username";
3 authenticationRequest.password = "password";
4
Simply Sending a Request or Sending and Executing Request Cloud Code
When you send a request using the SparkRequest API, you can:
- Simply send the request and not execute the Cloud Code attached to the request.
- Send the request and also execute the request's Cloud Code.
Here's an example of how to do this for an AccountDetailsRequest:
1
2 // Create Request
3 var req = new SparkRequests.AccountDetailsRequest();
4
5 // Send Request without executing Cloud Code
6 req.Send();
7
8 // Send Request and execute Cloud Code
9 req.Execute();
10
11 // Send Request as another player without executing Cloud Code
12 req.SendAs(playerId);
13
14 // Send Request as another player and execute Cloud Code
15 req.ExecuteAs(playerId);
Sending the Request and Grabbing the Response
There are a few ways to dispatch the request and they primarily differ by who the request is sent by:
1 // Create Request
2 var req = new SparkRequests.AccountDetailsRequest();
3
4 // Send Request
5 var response = req.Send();
6
7 //return the response in scriptData
8 Spark.setScriptData("response", response)
The Response data
The Response is a JSON object that can be easily queried, here’s an example:
1 // Create Request
2 var request = new SparkRequests.AuthenticationRequest();
3
4 //Set the parameters
5 request.password = "password";
6 request.userName = "userName";
7
8 //Grab the response
9 var response = request.Send();
10
11 //Check it for errors
12 if(response.error != null){
13 // Do something
14 }