Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for Teams #39

Draft
wants to merge 5 commits into
base: q2-2023
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ Options +FollowSymlinks

RewriteEngine On

# ! < TEAMS API > ! #
RewriteRule ^api/v1/(teams\/.*|teams)$ /api/internal/v1/teams/router.php [L,NC]
# ! </ TEAMS API > ! #

RewriteBase /


Expand Down
39 changes: 39 additions & 0 deletions api/impl/v1/teams/create_team.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

require_once($_SERVER['DOCUMENT_ROOT'] . "/global/php/functions.php");

api_controller_base_classes();
teams_rules();
data_validator();

class CreateTeamApiController extends ParameterizedApiController {
public function post(): ApiResult {
$body = JsonBodyReader::read();

// validate body

$validationResult = DataValidator::validate_associative_array($body, [
"team_name" => (new DataValidatorRule())
->must_be_defined()
->must_be_string()
->string_must_have_length(TEAM_NAME_MIN_LENGTH, TEAM_NAME_MAX_LENGTH)
]);

if (!$validationResult->isSuccess())
{
return new BadArgumentsApiResult([
"error_code" => "BODY_NOT_VALIDATED",
"error" => [
"invalid_rule_code" => $validationResult->getInvalidRuleCode(),
"invalid_rule_details" => $validationResult->getInvalidRuleDetails()
]
]);
}

// TODO: create team

return new OkApiResult(new stdClass);
}
}

ApiRouter::on("/api/v1/teams/create", CreateTeamApiController::class);
4 changes: 4 additions & 0 deletions api/impl/v1/teams/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

require_once($_SERVER['DOCUMENT_ROOT'] . "/global/php/functions.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/api/impl/v1/teams/create_team.php");
55 changes: 52 additions & 3 deletions global/php/api_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,20 @@ public function delete(): ApiResult {
}
}

class ParameterizedApiController extends ApiController {
private array $parameters;

public function __construct(array $parameters) {
$this->parameters = $parameters;
}

protected function getParameter(mixed $key): mixed {
return $this->parameters[$key];
}
}

class JsonBodyReader {
public static function read() {
public static function read(): array {
$result = json_decode(file_get_contents('php://input'), true);
if (json_last_error() !== JSON_ERROR_NONE)
throw new Exception("could not decode json, code: " . json_last_error());
Expand All @@ -127,7 +139,7 @@ public function getContentType(): string {
}

class ApiControllerExecutor {
public static function execute(ApiController $controller, ApiResultSerializer $serializer) {
public static function execute(mixed $controller, ApiResultSerializer $serializer) {
try {
$method = $_SERVER['REQUEST_METHOD'];

Expand Down Expand Up @@ -157,11 +169,48 @@ public static function execute(ApiController $controller, ApiResultSerializer $s
} catch (Exception $exception) {
error_log($exception->getMessage());
Logging::PutLog("Got exception in " . $controller::class . ":" . $exception->getMessage());
$result = new UnknownErrorResult();
$result = new UnknownErrorResult(["message" => $exception->getMessage()]);
}

http_response_code($result->getStatusCode());
header("Content-Type: " . $serializer->getContentType());
echo $serializer->serialize($result);
}
}

class ApiRouter {
private static ?ApiResultSerializer $defaultSerializer;

public static function getSerializer(): ApiResultSerializer {
if (!isset(self::$defaultSerializer))
self::$defaultSerializer = new JsonApiResultSerializer;

return self::$defaultSerializer;
}

public static function setSerializer(ApiResultSerializer $serializer): void {
self::$defaultSerializer = $serializer;
}

public static function on(string $regex, string $controller_class_name): void {
$params = $_SERVER['REQUEST_URI'];
$params = (stripos($params, "/") !== 0) ? "/" . $params : $params;
$regex = str_replace('/', '\/', $regex);

$is_match = preg_match('/^' . ($regex) . '$/', $params, $matches, PREG_OFFSET_CAPTURE);

if ($is_match) {
// first value is normally the route, lets remove it
array_shift($matches);
// Get the matches as parameters
$params = array_map(function ($param) {
return $param[0];
}, $matches);

$ref = new ReflectionClass($controller_class_name);
$instance = $ref->newInstanceArgs(array($params));

ApiControllerExecutor::execute($instance, self::getSerializer());
}
}
}
96 changes: 96 additions & 0 deletions global/php/data_validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

class DataValidatorRule {
private array $rules;

public function __construct() {
$this->rules = array();
}

private function add_function_rule(string $rule_code, \Closure $closure, ?array $details = null) {
array_push($this->rules, [$closure, $rule_code, $details]);
}

public function must_be_int(): DataValidatorRule {
$this->add_function_rule("MUST_BE_INT", function($v) { return is_int($v); });

return $this;
}

public function must_be_string(): DataValidatorRule {
$this->add_function_rule("MUST_BE_STRING", function($v) { return is_string($v); });

return $this;
}

public function string_must_have_length($minLength, $maxLength): DataValidatorRule {
$this->add_function_rule(
"STRING_MUST_HAVE_LENGTH",
function($v) use ($minLength, $maxLength) {
$len = strlen($v);
return $minLength <= $len && $len <= $maxLength;
},
[ "min_length" => $minLength, "max_length" => $maxLength ]
);

return $this;
}

public function must_be_defined(): DataValidatorRule {
$this->add_function_rule("MUST_BE_DEFINED", function($v) {
return isset($v);
});

return $this;
}

public function validate($v): DataValidationResult {
foreach ($this->rules as $rule) {
if (!$rule[0]($v))
return new DataValidationResult(false, $rule[1], $rule[2]);
}

return new DataValidationResult(true);;
}
}

class DataValidationResult {
private bool $success;
private ?string $invalid_rule_code;
private ?array $invalid_rule_code_details;

public function __construct(
bool $success,
?string $invalid_rule_code = null,
?array $invalid_rule_code_details = null)
{
$this->success = $success;
$this->invalid_rule_code = $invalid_rule_code;
$this->invalid_rule_code_details = $invalid_rule_code_details;
}

public function isSuccess(): bool {
return $this->success;
}

public function getInvalidRuleCode(): ?string {
return $this->invalid_rule_code;
}

public function getInvalidRuleDetails(): ?array {
return $this->invalid_rule_code_details;
}
}

class DataValidator {
public static function validate_associative_array(array $array, array $rules): DataValidationResult {
foreach ($rules as $key => $rule) {
$validationResult = $rule->validate($array[$key]);

if (!$validationResult->isSuccess())
return $validationResult;
}

return new DataValidationResult(true);
}
}
9 changes: 7 additions & 2 deletions global/php/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,12 @@ function osekai_http_request()
require_once($_SERVER['DOCUMENT_ROOT'] . "/global/php/osekaiHttpRequest.php");
}

function json_validator()
function data_validator()
{
require_once($_SERVER['DOCUMENT_ROOT'] . "/global/php/json_validator.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/global/php/data_validator.php");
}

function teams_rules()
{
require_once($_SERVER['DOCUMENT_ROOT'] . "/global/php/rules/teams.php");
}
39 changes: 0 additions & 39 deletions global/php/json_validator.php

This file was deleted.

12 changes: 12 additions & 0 deletions global/php/rules/teams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

define("TEAM_NAME_MAX_LENGTH", 32);
define("TEAM_NAME_MIN_LENGTH", 4);
define("TEAM_BIO_MAX_LENGTH", 250);
define("TEAM_BIO_MIN_LENGTH", 1);
define("TEAM_USERPAGE_MAX_LENGTH", 5000);
define("TEAM_USERPAGE_MIN_LENGTH", 1);
define("TEAM_AVATAR_MAX_WIDTH", 512);
define("TEAM_AVATAR_MAX_HEIGHT", 512);
define("TEAM_BANNER_MAX_WIDTH", 1100);
define("TEAM_BANNER_MAX_HEIGHT", 300);
12 changes: 6 additions & 6 deletions medals/api/favourite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
include_once($_SERVER['DOCUMENT_ROOT'] . "/global/php/functions.php");

api_controller_base_classes();
json_validator();
data_validator();

class FavouriteMedalApiController extends ApiController
{
Expand All @@ -25,9 +25,9 @@ public function put(): ApiResult {

$requestJson = JsonBodyReader::read();

if (!JsonValidator::validate_associative_array($requestJson, [
'medal_id' => (new JsonValidatorRule())->must_be_int()
])) {
if (!DataValidator::validate_associative_array($requestJson, [
'medal_id' => (new DataValidatorRule())->must_be_int()
])->isSuccess()) {
return new BadArgumentsApiResult(["message" => "Invalid medal_id"]);
}

Expand All @@ -47,8 +47,8 @@ public function delete(): ApiResult {

$requestJson = JsonBodyReader::read();

if (!JsonValidator::validate_associative_array($requestJson, [
'medal_id' => (new JsonValidatorRule())->must_be_int()
if (!DataValidator::validate_associative_array($requestJson, [
'medal_id' => (new DataValidatorRule())->must_be_int()
])) {
return new BadArgumentsApiResult(["message" => "Invalid medal_id"]);
}
Expand Down