Skip to content
Snippets Groups Projects
Commit 30f3492f authored by Lucas García's avatar Lucas García
Browse files

feat: add supports for http methods on rest setting

parent 2e139f53
No related branches found
No related tags found
1 merge request!5REST API HTTP Method
......@@ -7,8 +7,6 @@ use WP_Error;
use Exception;
use TypeError;
use function WPCT_HTTP\wpct_http_post;
/**
* Integration abstract class.
*
......@@ -196,9 +194,9 @@ abstract class Integration extends Singleton
);
$this->apply_pipes($hook['pipes'], $payload);
$headers = $backend->get_headers();
if (isset($hook['endpoint'])) {
if (isset($hook['method'], $hook['endpoint'])) {
$url = $backend->get_endpoint_url($hook['endpoint']);
$res = wpct_http_post($url, [
$res = $this->submit_rest($hook['method'], $url, [
'data' => $payload,
'files' => $attachments,
'headers' => $headers,
......@@ -350,6 +348,36 @@ abstract class Integration extends Singleton
);
}
/**
* Submit REST requests over HTTP methods.
*
* @since 3.0.4
*
* @param string $method HTTP method (GET, POST, PUT, DELETE).
* @param string $url Target URL.
* @param array $args Request arguments.
* @return array|WP_Error Request response.
*/
private function submit_rest($method, $url, $args)
{
$m = strtolower($method);
$func = "\WPCT_HTTP\wpct_http_{$m}";
if (!is_callable($func)) {
return new WP_Error(
'http_method_not_allowed',
__("Unkown HTTP method: {$method}", 'wpct-erp-forms'),
['status' => 405]
);
}
if (in_array($method, ['GET', 'DELETE'])) {
unset($args['headers']['Content-Type']);
$args['params'] = $args['data'];
}
return $func($url, $args);
}
/**
* JSON RPC login request.
*
......
......@@ -128,6 +128,10 @@ class Settings extends BaseSettings
'backend' => ['type' => 'string'],
'form_id' => ['type' => 'string'],
'endpoint' => ['type' => 'string'],
'method' => [
'type' => 'string',
'enum' => ['GET', 'POST', 'PUT', 'DELETE'],
],
'pipes' => [
'type' => 'array',
'items' => [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment