Add basic request, response, and endpoint definitions for API
[MAILPOET-4135]
This commit is contained in:
25
mailpoet/lib/Automation/API/Endpoint.php
Normal file
25
mailpoet/lib/Automation/API/Endpoint.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\API;
|
||||
|
||||
abstract class Endpoint {
|
||||
public function get(Request $request): Response {
|
||||
return $this->methodNotAllowed();
|
||||
}
|
||||
|
||||
public function post(Request $request): Response {
|
||||
return $this->methodNotAllowed();
|
||||
}
|
||||
|
||||
public function put(Request $request): Response {
|
||||
return $this->methodNotAllowed();
|
||||
}
|
||||
|
||||
public function delete(Request $request): Response {
|
||||
return $this->methodNotAllowed();
|
||||
}
|
||||
|
||||
private function methodNotAllowed(): ErrorResponse {
|
||||
return new ErrorResponse(405, 'Method not allowed', 'mailpoet_automation_method_not_allowed');
|
||||
}
|
||||
}
|
45
mailpoet/lib/Automation/API/Request.php
Normal file
45
mailpoet/lib/Automation/API/Request.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\API;
|
||||
|
||||
use MailPoet\Automation\Exceptions;
|
||||
use WP_REST_Request;
|
||||
|
||||
class Request {
|
||||
/** @var WP_REST_Request */
|
||||
private $wpRequest;
|
||||
|
||||
public function __construct(
|
||||
WP_REST_Request $wpRequest
|
||||
) {
|
||||
$this->wpRequest = $wpRequest;
|
||||
}
|
||||
|
||||
public function getHeader(string $key): ?string {
|
||||
return $this->wpRequest->get_header($key);
|
||||
}
|
||||
|
||||
public function getUrlParams(): array {
|
||||
return $this->wpRequest->get_url_params();
|
||||
}
|
||||
|
||||
public function getUrlParam(string $name): ?string {
|
||||
return $this->getUrlParams()[$name] ?? null;
|
||||
}
|
||||
|
||||
public function getQueryParams(): array {
|
||||
return $this->wpRequest->get_query_params();
|
||||
}
|
||||
|
||||
public function getQueryParam(string $name): ?string {
|
||||
return $this->getQueryParams()[$name] ?? null;
|
||||
}
|
||||
|
||||
public function getBody(): array {
|
||||
return $this->wpRequest->get_json_params() ?? [];
|
||||
}
|
||||
|
||||
public function getRawBody(): string {
|
||||
return $this->wpRequest->get_body();
|
||||
}
|
||||
}
|
14
mailpoet/lib/Automation/API/Response.php
Normal file
14
mailpoet/lib/Automation/API/Response.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\API;
|
||||
|
||||
use WP_REST_Response;
|
||||
|
||||
class Response extends WP_REST_Response {
|
||||
public function __construct(
|
||||
array $data = null,
|
||||
int $status = 200
|
||||
) {
|
||||
parent::__construct(['data' => $data], $status);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user