-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAPIService.js
81 lines (67 loc) · 2.69 KB
/
APIService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* APIService.js */
function APIService( endpoint, appid )
{
var endpoint = endpoint;
var appid = appid;
/*
executeCommand : This method will execute the specified command as an api request.
'Name' is the name of the command you wish to execute, eg: GetIndexItemInfo.
'Params' is a string containing the command parameters, eg: 'datasource=fresh&item=majesticseo.com'.
'Timeout' specifies the amount of time to wait before aborting the transaction. This defaults to 5 seconds which should be plenty for most requests.
*/
this.executeCommand = function( name, params, timeout )
{
// call the command
var xmlhttp=new XMLHttpRequest();
var params = "app_api_key=" + encodeURIComponent(appid)
+ "&cmd=" + encodeURIComponent(name)
+ "&" + params;
xmlhttp.open("POST",endpoint,false);
//xmlhttp.timeout = timeout || 5;
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(params);
// check that the request was carried out ok and return the response
var response = new Response();
if ( xmlhttp.status == "200" )
{
response.new_ok( new XMLSerializer().serializeToString(xmlhttp.responseXML) );
}
else
{
response.new_failed("ConnectionError", "Problem connecting to the data source.");
}
return response;
}
/*
executeOpenAppRequest : This will execute the specified command as an OpenApp request.
'AccessToken' is the token provided by the user to access their resources.
'CommandName' is the name of the command you wish to execute, eg: GetIndexItemInfo
'Params' is a string containing the command parameters.
'Timeout' specifies the amount of time to wait before aborting the transaction.
This defaults to 5 seconds which should be plenty for most requests.
*/
this.executeOpenAppRequest = function( accesstoken, commandname, params, timeout )
{
// call the command
var xmlhttp=new XMLHttpRequest();
var params = "accesstoken=" + encodeURIComponent(accesstoken)
+ "&privatekey=" + encodeURIComponent(appid)
+ "&cmd=" + encodeURIComponent(commandname)
+ "&" + params;
xmlhttp.open("POST",endpoint,false);
//xmlhttp.timeout = timeout || 5;
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(params);
// check that the request was carried out ok and return the response
var response = new Response();
if ( xmlhttp.status == "200" )
{
response.new_ok( new XMLSerializer().serializeToString(xmlhttp.responseXML) );
}
else
{
response.new_failed("ConnectionError", "Problem connecting to the data source.");
}
return response;
}
}