forked from jquery/testswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
70 lines (62 loc) · 1.78 KB
/
api.php
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
<?php
/**
* This is the main front-end entry point for scripts.
* All API responses start here.
* @example ./api.php (same as: api.php?format=json&action=info)
* @example ./api.php?action=scores
* @example ./api.php?format=php&action=job&item=1
* @example ./api.php?format=jsonp&action=swarmstate
*
* @author Timo Tijhof, 2012
* @since 1.0.0
* @package TestSwarm
*/
// Valid entry point
define( 'SWARM_ENTRY', 'API' );
header( "X-Robots-Tag: noindex,nofollow", true );
require_once "inc/init.php";
$action = $swarmContext->getRequest()->getVal( "action", "info" );
if ( !$action ) {
// getVal will only fallback to "info" if "action" isn't set,
// if it is falsy, also use infno (we don't want to instantiate Action
// directly if it is an empty string
$action = "info";
}
$format = $swarmContext->getRequest()->getVal( "format", "json" );
$className = ucfirst( $action ) . "Action";
$className = class_exists( $className ) ? $className : null;
if ( !Api::isGreyFormat( $format ) ) {
session_start();
}
if ( $className ) {
try {
$actionObj = $className::newFromContext( $swarmContext );
$actionObj->doAction();
$response = array();
if ( $actionObj->getError() ) {
$response["error"] = $actionObj->getError();
}
if ( $actionObj->getData() ) {
$response[$action] = $actionObj->getData();
}
} catch ( Exception $e ) {
$response = array(
"error" => array(
"code" => "internal-error",
"info" => "An internal error occurred. Action could not be performed. Error message:\n" . $e->getMessage(),
),
);
}
} else {
$response = array(
"error" => array(
"code" => "invalid-input",
"info" => "Action `$action` does not exist",
),
);
}
$api = Api::newFromContext( $swarmContext );
$api->setFormat( $format );
$api->setResponse( $response );
$api->output();
exit;