Added method executeMethod() to \KimchiAPI > KimchiAPI

This commit is contained in:
Zi Xing 2021-12-15 14:15:02 -05:00
parent 78f10145b2
commit 8b4248d07c
2 changed files with 29 additions and 1 deletions

View File

@ -5,7 +5,7 @@
use Exception;
use Throwable;
class MethodNotFondException extends Exception
class MethodNotFoundException extends Exception
{
/**
* @param string $message

View File

@ -3,9 +3,13 @@
namespace KimchiAPI;
// Define server information for response headers
use Exception;
use KimchiAPI\Exceptions\MethodAlreadyRegisteredException;
use KimchiAPI\Exceptions\MethodNotFoundException;
use KimchiAPI\Exceptions\MissingComponentsException;
use KimchiAPI\Interfaces\MethodInterface;
use KimchiAPI\Objects\Request;
use KimchiAPI\Objects\Response;
use KimchiAPI\Utilities\Converter;
use RuntimeException;
@ -75,4 +79,28 @@
$this->methods = $methods_clean;
}
/**
* Executes a method in the server and returns the response
*
* @param Request $request
* @return Response
*/
public function executeMethod(Request $request): ?Response
{
if(isset($this->methods[$request->Method]) == false)
{
$truncated_method = Converter::truncateString($request->Method, 20);
return Response::fromException(new MethodNotFoundException("The requested method '" . $truncated_method . "' was not found."));
}
try
{
return $this->methods[$request->Method]->execute($request);
}
catch(Exception $e)
{
return Response::fromException($e);
}
}
}