From eaa70b734619af7dfbc316642c1a17c623ad6e1f Mon Sep 17 00:00:00 2001 From: emkael Date: Wed, 13 May 2020 18:06:52 +0200 Subject: API call reusable --- http/api-inc.php | 17 +++++++++++++++++ http/api.php | 40 +++++++++++----------------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/http/api-inc.php b/http/api-inc.php index 565e66c..96c9ca7 100644 --- a/http/api-inc.php +++ b/http/api-inc.php @@ -28,6 +28,23 @@ function recursive_ksort(&$array, $flags = SORT_REGULAR) { class ParametersException extends Exception {}; +function run($parameters) { + $versionClasses = array( + '1' => 'ApiPklV1', // RegKlas 2018.11.01 + '2' => 'ApiPklV2', // RegKMP 2020.01.01 + '_default' => 'ApiPklV2' + ); + $version = isset($parameters['version']) ? $parameters['version'] : '_default'; + $apiClass = isset($versionClasses[$version]) ? $versionClasses[$version] : $versionClasses['_default']; + $api = new $apiClass($parameters); + if ($parameters['tournament_rank'] == ApiPkl::RANK_KMP) { + $result = $api->calculate_kmp_points(); + } else { + $result = $api->calculate_points(); + } + return $result; +} + class ApiPkl { const RANK_KMP = 101; diff --git a/http/api.php b/http/api.php index acf2752..8bae5f0 100644 --- a/http/api.php +++ b/http/api.php @@ -30,35 +30,17 @@ Parametry: require_once('api-inc.php'); -function run($parameters) { - - try { - $versionClasses = array( - '1' => 'ApiPklV1', // RegKlas 2018.11.01 - '2' => 'ApiPklV2', // RegKMP 2020.01.01 - '_default' => 'ApiPklV2' - ); - $version = isset($parameters['version']) ? $parameters['version'] : '_default'; - $apiClass = isset($versionClasses[$version]) ? $versionClasses[$version] : $versionClasses['_default']; - $api = new $apiClass($parameters); - if ($parameters['tournament_rank'] == ApiPkl::RANK_KMP) { - $result = $api->calculate_kmp_points(); - } else { - $result = $api->calculate_points(); - } - return $result; - } - catch (ParametersException $e) { - http_response_code(400); - die(json_encode($e->getMessage())); - } - catch (Exception $e) { - http_response_code(500); - die(json_encode($e->getMessage())); - } +try { + header('Content-Type: application/json'); + print(json_encode(run($_REQUEST))); +} +catch (ParametersException $e) { + http_response_code(400); + die(json_encode($e->getMessage())); +} +catch (Exception $e) { + http_response_code(500); + die(json_encode($e->getMessage())); } - -header('Content-Type: application/json'); -print(json_encode(run($_REQUEST))); ?> -- cgit v1.2.3