Permalink
Browse files

Merge branch 'master' into shallow

  • Loading branch information...
2 parents 9632d4f + f433709 commit eb3f7963df2bfa053e70921e836bce7ca1a0186f @sergeychernyshev sergeychernyshev committed Sep 17, 2016
View
@@ -7,9 +7,6 @@
[submodule "dbupgrade"]
path = dbupgrade
url = git://github.com/sergeychernyshev/DBUpgrade.git
-[submodule "phptidy"]
- path = phptidy
- url = git://github.com/sergeychernyshev/phptidy.git
[submodule "twig"]
path = twig
url = git://github.com/fabpot/Twig.git
@@ -37,3 +34,9 @@
[submodule "swiftmailer"]
path = swiftmailer
url = git://github.com/swiftmailer/swiftmailer.git
+[submodule "phptidy"]
+ path = phptidy
+ url = https://github.com/cmrcx/phptidy.git
+[submodule "swagger-ui"]
+ path = swagger-ui
+ url = https://github.com/swagger-api/swagger-ui.git
View
@@ -1,147 +1,100 @@
<?php
require_once(__DIR__ . '/global.php');
-if (!array_key_exists('call', $_GET)) {
- header('HTTP/1.0 400 Bad Request');
- ?>
- <style>
- code {
- font-family: monospace,"Courier New";
- background-color: #f9f9f9;
- padding: 0.2em;
- }
-
- dl {
- margin-bottom: 2em;
- }
+if (!array_key_exists('call', $_GET) && array_key_exists('swagger-spec', $_GET)) {
+ $swagger_spec = array(
+ 'swagger' => '2.0',
+ 'info' => array(
+ 'contact' => array(
+ 'email' => UserConfig::$supportEmailFromEmail
+ ),
+ 'version' => UserConfig::$apiSpecVersion
+ ),
+ 'basePath' => UserConfig::$USERSROOTURL . '/api.php'
+ );
- dt {
- margin-bottom: 0.3em;
- }
+ if (UserConfig::$appName) {
+ $swagger_spec['info']['title'] = UserConfig::$appName;
+ }
- dd {
- margin-bottom: 0.5em;
- }
+ if (UserConfig::$termsOfServiceFullURL) {
+ $swagger_spec['info']['termsOfService'] = UserConfig::$termsOfServiceFullURL;
+ }
- b.call {
- color: green;
- }
+ // Swagger tags, e.g. API namespaces
+ foreach (\StartupAPI\API\Endpoint::getNamespaces() as $namespace) {
+ $swagger_spec['tags'][] = array(
+ 'name' => $namespace->getSlug(),
+ 'description' => $namespace->getName()
+ );
+ }
- b.param {
- color: blue;
- }
- </style>
- <h1>400 Bad Request</h1>
- <p>Required parameter: <b>call</b></p>
- <?php
- $user = StartupAPI::getUser();
- if (!is_null($user) && $user->isAdmin()) {
- ?>
- <p>
- Available endpoints:
- <?php
- $all_endpoints = \StartupAPI\API\Endpoint::getAllEndpointsBySlug();
- foreach (\StartupAPI\API\Endpoint::getNamespaces() as $namespace) {
- ?>
- <h2><?php echo $namespace->getName(); ?></h2>
- <ul>
- <?php
- $namespace_slug = $namespace->getSlug();
- foreach ($all_endpoints[$namespace_slug] as $endpoint_slug => $endpoints) {
- ?>
- <li>
- <h3><?php echo $endpoint_slug ?></h3>
- <dl>
- <?php
- foreach ($endpoints as $method => $endpoint) {
- $call = "/$namespace_slug$endpoint_slug";
-
- $params = $endpoint->getParams();
-
- $sample_params_urlencoded = '';
- if (count($params) > 0) {
- foreach ($params as $name => $param) {
- if (!$param->isOptional()) {
- $sample_params_urlencoded .= '<b class="param">' . $name . '</b>=';
- $sample_params_urlencoded .= urldecode($param->getSampleValue());
- }
- }
-
- if (!empty($sample_params_urlencoded)) {
- $sample_params_urlencoded = '&amp;' . $sample_params_urlencoded;
- }
- }
- ?>
- <dt>
- <code>
- <?php echo $method; ?>
- <?php
- if ($method == 'GET') {
- ?>
- <a href="?call=<?php echo $call . strip_tags($sample_params_urlencoded) ?>"><?php echo UserConfig::$USERSROOTFULLURL ?>/api.php?call=<b class="call"><?php echo $call ?></b><?php echo $sample_params_urlencoded ?></a>
- <?php
- } else {
- echo UserConfig::$USERSROOTFULLURL;
- ?>/api.php?call=<b class="call"><?php echo $call; ?></b>
- <?php
- }
- ?>
- </code>
- </dt>
- <dd>
- <?php echo $endpoint->getDescription(); ?>
- <?php
- if (count($params) > 0) {
- ?>
- <h4>Parameters:</h4>
- <dl>
- <?php
- foreach ($params as $name => $param) {
- ?>
- <dt>
- <code><?php echo $name; ?></code>
- <?php
- if ($param->isOptional()) {
- ?> (optional)<?php
- } else {
- ?> (required)<?php
- }
- ?>
- </dt>
- <dd>
- <?php
- echo $param->getDescription();
-
- if ($param->allowsMultipleValues()) {
- ?>
- (allows multiple values)
- <?php
- }
- ?>
- </dd>
- <?php
- }
- ?>
- </dl>
- <?php
- }
- ?>
- </dd>
- <?php
- }
- ?>
- </dl>
- </li>
- <?php
+ // Swagger paths, e.g. API Endpoints groupped by path
+ $all_endpoints = \StartupAPI\API\Endpoint::getAllEndpointsBySlug();
+ foreach ($all_endpoints as $namespace_slug => $namespace_endpoints) {
+ foreach ($namespace_endpoints as $endpoint_slug => $endpoints) {
+ foreach ($endpoints as $method => $endpoint) {
+ $operation = array(
+ 'tags' => array(
+ $namespace_slug
+ ),
+ 'summary' => $endpoint->getDescription(),
+ 'description' => $endpoint->getDescription(),
+ 'operationId' => get_class($endpoint),
+ 'responses' => array(
+ '200' => array(
+ 'description' => 'success'
+ ),
+ '400' => array(
+ 'description' => 'invalid input'
+ )
+ )
+ );
+
+ $params = $endpoint->getParams();
+
+ foreach ($params as $name => $param) {
+ $param_spec = array(
+ 'name' => $name,
+ 'description' => $param->getDescription(),
+ 'required' => !$param->isOptional()
+ );
+
+ if ($method === 'GET') {
+ $param_spec['in'] = 'query';
+ } else {
+ $param_spec['in'] = 'formData';
+ }
+
+ if ($param->allowsMultipleValues()) {
+ $param_spec['type'] = 'array';
+ $param_spec['collectionFormat'] = 'multi';
+ }
+ $operation['parameters'][] = $param_spec;
}
- ?>
- </ul>
- <?php
+
+ $swagger_spec['paths']
+ ["/api.php?call=/$namespace_slug$endpoint_slug"]
+ [strtolower($method)] = $operation;
+ }
}
- ?>
- </p>
- <?php
}
+
+ header('Content-type: application/json');
+ echo json_encode($swagger_spec);
+
+ if (json_last_error() !== JSON_ERROR_NONE) {
+ header('HTTP/1.1 400 Bad Request');
+ header('Content-type: text/plain');
+ echo "Error encoding JSON result";
+ }
+
+ exit;
+}
+
+if (!array_key_exists('call', $_GET)) {
+ $template_info = StartupAPI::getTemplateInfo();
+ StartupAPI::$template->display('@startupapi/swagger-ui.html.twig', $template_info);
exit;
}
@@ -172,7 +125,7 @@
'result' => $endpoint->call($params, $raw_request_body)
);
} catch (\StartupAPI\API\NotFoundException $ex) {
- header('HTTP/1.0 404 Not Found');
+ header('HTTP/1.1 404 Not Found');
$response = array(
'meta' => array(
'success' => false,
@@ -181,7 +134,7 @@
)
);
} catch (\StartupAPI\API\MethodNotAllowedException $ex) {
- header('HTTP/1.0 405 Method not allowed');
+ header('HTTP/1.1 405 Method not allowed');
$response = array(
'meta' => array(
'success' => false,
@@ -190,7 +143,7 @@
)
);
} catch (\StartupAPI\API\UnauthenticatedException $ex) {
- header('HTTP/1.0 401 Authentication Required');
+ header('HTTP/1.1 401 Authentication Required');
header('WWW-Authenticate: FormBased');
$response = array(
'meta' => array(
@@ -200,7 +153,7 @@
)
);
} catch (\StartupAPI\API\UnauthorizedException $ex) {
- header('HTTP/1.0 403 Forbidden');
+ header('HTTP/1.1 403 Forbidden');
$response = array(
'meta' => array(
'success' => false,
@@ -209,7 +162,7 @@
)
);
} catch (\StartupAPI\API\BadParameterException $ex) {
- header('HTTP/1.0 400 Bad Parameter');
+ header('HTTP/1.1 400 Bad Parameter');
$response = array(
'meta' => array(
'success' => false,
@@ -218,7 +171,7 @@
)
);
} catch (\StartupAPI\API\APIException $ex) {
- header('HTTP/1.0 500 Server Error');
+ header('HTTP/1.1 500 Server Error');
$response = array(
'meta' => array(
'success' => false,
@@ -10,8 +10,7 @@
require_once(__DIR__ . '/EndpointNameSpace.php');
// APIs Endpoints to be registered
-require_once(__DIR__ . '/v1/Login.php');
-require_once(__DIR__ . '/v1/User.php');
+require_once(__DIR__ . '/v1/User/Get.php');
require_once(__DIR__ . '/v1/Accounts.php');
require_once(dirname(__DIR__) . '/AuthenticationModule.php');
@@ -73,14 +72,10 @@ public static function register(EndpointNameSpace $namespace, $method, self $end
* Helper function that registers core API endoiunts in the system
*/
public static function registerCoreEndpoints() {
- $namespace = new EndpointNameSpace(
- 'startupapi', 'StartupAPI', 'Startup API core endpoints'
- );
- if (!is_null(\AuthenticationModule::get('usernamepass'))) {
- self::register($namespace, 'POST', new \StartupAPI\API\v1\Login());
+ if (\UserConfig::$apiNamespace) {
+ self::register(\UserConfig::$apiNamespace, 'GET', new \StartupAPI\API\v1\User\Get());
+ self::register(\UserConfig::$apiNamespace, 'GET', new \StartupAPI\API\v1\Accounts());
}
- self::register($namespace, 'GET', new \StartupAPI\API\v1\User());
- self::register($namespace, 'GET', new \StartupAPI\API\v1\Accounts());
}
protected function __construct($slug, $description) {
@@ -270,9 +265,9 @@ protected function call($values, $raw_request_body = null) {
*/
/**
- * Abstract class for all API Exceptions
+ * Root class for all API Exceptions
*/
-abstract class APIException extends \StartupAPIException {
+class APIException extends \StartupAPIException {
}
@@ -25,17 +25,16 @@ public function call($values, $raw_request_body = null) {
$accounts = $user->getAccounts();
- // @TODO Implement general API serialization logic for all objects
- return array_map(function(Account $account) {
- $users_and_roles = $account->getUsers();
- return array(
+ $results = array();
+ foreach ($accounts as $account) {
+ $results[] = array(
'id' => $account->getID(),
'name' => $account->getName(),
- 'member_ids' => array_map(function($user_and_role) {
- return $user_and_role[0]->getID();
- }, $users_and_roles)
+ 'is_admin' => ($account->getUserRole($user) == \Account::ROLE_ADMIN)
);
- }, $accounts);
+ }
+
+ return $results;
}
}
Oops, something went wrong.

0 comments on commit eb3f796

Please sign in to comment.