summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2020-05-13 18:06:52 +0200
committeremkael <emkael@tlen.pl>2020-05-13 18:06:52 +0200
commiteaa70b734619af7dfbc316642c1a17c623ad6e1f (patch)
tree90d62b4e67b2e51bc54acc5d6b7ecf5f9b696df3
parent4ff0fd0db663153bee3c2f499d152c29cc3c3bd3 (diff)
API call reusable
-rw-r--r--http/api-inc.php17
-rw-r--r--http/api.php40
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)));
?>