Created
October 24, 2021 16:18
-
-
Save amattu2/fbd051dfe369e841b3521da04e27c2f0 to your computer and use it in GitHub Desktop.
A PHP based API encapsulation boilerplate; Has the basic setup for a class for the API functions, the HTTP request, and the API key.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a PHP demo/boilerplate for a basic API which relies on a API key. | |
**/ | |
namespace amattu\api; | |
/** | |
* The main API request manager class | |
* | |
* NOTE: | |
* (1) This is where the main API functions go | |
* (2) If no errors are thrown during construction | |
* it's safe to assume the token is valid | |
*/ | |
class APIManager { | |
/** | |
* Instance of HttpRequest | |
* | |
* @var HttpRequest | |
*/ | |
protected $HttpRequest; | |
/** | |
* Class Constructor | |
* | |
* @param array $request_header fields | |
* @throws TypeError | |
* @throws InvalidAPITokenException | |
* @author Alec M. <https://amattu.com> | |
* @date 2021-10-24T12:00:27-040 | |
*/ | |
public function __construct(array $request_header) | |
{ | |
try { | |
$this->HttpRequest = new HttpRequest($request_header); | |
} catch (InvalidAPITokenException $e) { | |
// Handle invalid API token error | |
} catch (InvalidRequestException $e) { | |
// Handle invalid HTTP request error | |
} catch (TypeError $e) { | |
// Invalid type passed to a constructor | |
} | |
} | |
/** Add more API functionality here; i.e. Fetch users, CRUD, etc **/ | |
/** Access token info with $this->HttpRequest->getToken() **/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a PHP demo/boilerplate for a basic API which relies on a API key. | |
**/ | |
namespace amattu\api\token; | |
class InvalidAPITokenException extends \Exception {}; | |
/** | |
* API Token Encapsulation Class | |
*/ | |
class APIToken { | |
/** | |
* A reference to the API token | |
* | |
* @var string | |
*/ | |
private $APIToken; | |
/** | |
* Class Constructor | |
* | |
* @param string $token | |
* @throws TypeError | |
* @throws InvalidAPITokenException | |
* @author Alec M. <https://amattu.com> | |
* @date 2021-10-24T11:55:44-040 | |
*/ | |
public function __construct(string $token) | |
{ | |
$this->APIToken = $token; | |
if (!$this->isValid()) | |
throw new InvalidAPITokenException("Invalid API token provided"); | |
} | |
/** | |
* Determine if the API token is valid | |
* | |
* @return bool valid token | |
* @throws None | |
* @author Alec M. <https://amattu.com> | |
* @date 2021-10-24T11:56:08-040 | |
*/ | |
public function isValid() : bool | |
{ | |
return true; // Add validation logic here | |
} | |
/** Add more functions here; i.e. Permission level, expiry, user info, etc **/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a PHP demo/boilerplate for a basic API which relies on a API key. | |
**/ | |
namespace amattu\api; | |
class InvalidRequestException extends \Exception {}; | |
/** | |
* API HTTP Request Representation Class | |
*/ | |
class HttpRequest { | |
/** | |
* A class instance of APIToken | |
* | |
* @var APIToken | |
*/ | |
private $APIToken = null; | |
/** | |
* A array of HTTP header fields | |
* | |
* @var array | |
*/ | |
private $fields = []; | |
/** | |
* Class Constructor | |
* | |
* @param array HTTP $request header fields | |
* @throws TypeError | |
* @throws InvalidAPITokenException | |
* @throws InvalidRequestException | |
* @author Alec M. <https://amattu.com> | |
* @date 2021-10-24T11:56:37-040 | |
*/ | |
public function __construct(array $headers) | |
{ | |
if (!$request["bearer"]) | |
throw new InvalidRequestException("No bearer token found in request"); | |
$this->APIToken = new APIToken($request["bearer"]); | |
$this->fields = $request; | |
} | |
/** | |
* Return instance of APIToken | |
* | |
* @return APIToken instance of API token | |
* @throws TypeError | |
* @author Alec M. <https://amattu.com> | |
* @date 2021-10-24T12:11:57-040 | |
*/ | |
public function getToken() : APIToken | |
{ | |
return $this->APIToken; | |
} | |
/** Do anything else with the request here; i.e. access more fields, additional validation, etc **/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment