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

Merge branch 'feat/rest-http-method' into 'main'

REST API HTTP Method

See merge request !5
parents 2e139f53 69083e99
No related branches found
No related tags found
1 merge request!5REST API HTTP Method
...@@ -7,8 +7,6 @@ use WP_Error; ...@@ -7,8 +7,6 @@ use WP_Error;
use Exception; use Exception;
use TypeError; use TypeError;
use function WPCT_HTTP\wpct_http_post;
/** /**
* Integration abstract class. * Integration abstract class.
* *
...@@ -196,9 +194,9 @@ abstract class Integration extends Singleton ...@@ -196,9 +194,9 @@ abstract class Integration extends Singleton
); );
$this->apply_pipes($hook['pipes'], $payload); $this->apply_pipes($hook['pipes'], $payload);
$headers = $backend->get_headers(); $headers = $backend->get_headers();
if (isset($hook['endpoint'])) { if (isset($hook['method'], $hook['endpoint'])) {
$url = $backend->get_endpoint_url($hook['endpoint']); $url = $backend->get_endpoint_url($hook['endpoint']);
$res = wpct_http_post($url, [ $res = $this->submit_rest($hook['method'], $url, [
'data' => $payload, 'data' => $payload,
'files' => $attachments, 'files' => $attachments,
'headers' => $headers, 'headers' => $headers,
...@@ -350,6 +348,36 @@ abstract class Integration extends Singleton ...@@ -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. * JSON RPC login request.
* *
......
...@@ -128,6 +128,10 @@ class Settings extends BaseSettings ...@@ -128,6 +128,10 @@ class Settings extends BaseSettings
'backend' => ['type' => 'string'], 'backend' => ['type' => 'string'],
'form_id' => ['type' => 'string'], 'form_id' => ['type' => 'string'],
'endpoint' => ['type' => 'string'], 'endpoint' => ['type' => 'string'],
'method' => [
'type' => 'string',
'enum' => ['GET', 'POST', 'PUT', 'DELETE'],
],
'pipes' => [ 'pipes' => [
'type' => 'array', 'type' => 'array',
'items' => [ 'items' => [
......
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
"Edit pipes": ["Editar tubs"], "Edit pipes": ["Editar tubs"],
"Remove form": ["Eliminar formulari"], "Remove form": ["Eliminar formulari"],
"Add Form": ["Afegir Formulari"], "Add Form": ["Afegir Formulari"],
"Add form": ["Afegir formulari"],
"Form Hooks": ["Ganxos de Formulari"], "Form Hooks": ["Ganxos de Formulari"],
"REST API": [""], "REST API": [""],
"Model": [""], "Model": [""],
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
"Edit pipes": [""], "Edit pipes": [""],
"Remove form": [""], "Remove form": [""],
"Add Form": [""], "Add Form": [""],
"Add form": [""],
"Form Hooks": [""], "Form Hooks": [""],
"REST API": [""], "REST API": [""],
"Model": [""], "Model": [""],
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
"Edit pipes": ["Editar tubos"], "Edit pipes": ["Editar tubos"],
"Remove form": ["Eliminar formulario"], "Remove form": ["Eliminar formulario"],
"Add Form": ["A\u00f1adir Formulario"], "Add Form": ["A\u00f1adir Formulario"],
"Add form": ["A\u00f1adir formulario"],
"Form Hooks": ["Ganchos de Formulario"], "Form Hooks": ["Ganchos de Formulario"],
"REST API": [""], "REST API": [""],
"Model": ["Modelo"], "Model": ["Modelo"],
......
// vendor // vendor
import React from "react"; import React from "react";
import { TextControl, SelectControl, Button } from "@wordpress/components"; import {
TextControl,
SelectControl,
Button,
__experimentalSpacer as Spacer,
} from "@wordpress/components";
import { useState, useRef, useEffect } from "@wordpress/element"; import { useState, useRef, useEffect } from "@wordpress/element";
// source // source
...@@ -10,6 +15,25 @@ import useHookNames from "../../hooks/useHookNames"; ...@@ -10,6 +15,25 @@ import useHookNames from "../../hooks/useHookNames";
import FormPipes from "../../FormPipes"; import FormPipes from "../../FormPipes";
import { useI18n } from "../../providers/I18n"; import { useI18n } from "../../providers/I18n";
const methodOptions = [
{
label: "GET",
value: "GET",
},
{
label: "POST",
value: "POST",
},
{
label: "PUT",
value: "PUT",
},
{
label: "DELETE",
value: "DELETE",
},
];
function NewFormHook({ add }) { function NewFormHook({ add }) {
const __ = useI18n(); const __ = useI18n();
const [{ backends }] = useGeneral(); const [{ backends }] = useGeneral();
...@@ -27,6 +51,7 @@ function NewFormHook({ add }) { ...@@ -27,6 +51,7 @@ function NewFormHook({ add }) {
const [name, setName] = useState(""); const [name, setName] = useState("");
const [backend, setBackend] = useState(backendOptions?.[0].value || ""); const [backend, setBackend] = useState(backendOptions?.[0].value || "");
const [method, setMethod] = useState("POST");
const [endpoint, setEndpoint] = useState(""); const [endpoint, setEndpoint] = useState("");
const [formId, setFormId] = useState(formOptions?.[0].value || ""); const [formId, setFormId] = useState(formOptions?.[0].value || "");
const [nameConflict, setNameConflict] = useState(false); const [nameConflict, setNameConflict] = useState(false);
...@@ -36,9 +61,17 @@ function NewFormHook({ add }) { ...@@ -36,9 +61,17 @@ function NewFormHook({ add }) {
setName(name.trim()); setName(name.trim());
}; };
const onClick = () => add({ name, backend, endpoint, form_id: formId }); const onClick = () =>
add({ name, backend, method, endpoint, form_id: formId });
const disabled = !(name && backend && endpoint && formId && !nameConflict); const disabled = !(
name &&
backend &&
method &&
endpoint &&
formId &&
!nameConflict
);
return ( return (
<div <div
...@@ -52,47 +85,89 @@ function NewFormHook({ add }) { ...@@ -52,47 +85,89 @@ function NewFormHook({ add }) {
style={{ style={{
display: "flex", display: "flex",
gap: "1em", gap: "1em",
flexWrap: "wrap",
}} }}
> >
<TextControl <div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
label={__("Name", "wpct-erp-forms")} <TextControl
help={ label={__("Name", "wpct-erp-forms")}
nameConflict help={
? __("This name is already in use", "wpct-erp-forms") nameConflict
: "" ? __("This name is already in use", "wpct-erp-forms")
} : ""
value={name} }
onChange={handleSetName} value={name}
__nextHasNoMarginBottom onChange={handleSetName}
/> __nextHasNoMarginBottom
<SelectControl />
label={__("Backend", "wpct-erp-forms")} </div>
value={backend} <div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
onChange={setBackend} <SelectControl
options={backendOptions} label={__("Backend", "wpct-erp-forms")}
__nextHasNoMarginBottom value={backend}
/> onChange={setBackend}
<TextControl options={backendOptions}
label={__("Endpoint", "wpct-erp-forms")} __nextHasNoMarginBottom
value={endpoint} />
onChange={setEndpoint} </div>
__nextHasNoMarginBottom <div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
/> <SelectControl
<SelectControl label={__("Method", "wpct-erp-forms")}
label={__("Form", "wpct-erp-forms")} value={method || "POST"}
value={formId} onChange={setMethod}
onChange={setFormId} options={methodOptions}
options={formOptions} __nextHasNoMarginBottom
__nextHasNoMarginBottom />
/> </div>
<Button <div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
variant="primary" <TextControl
onClick={() => onClick()} label={__("Endpoint", "wpct-erp-forms")}
style={{ marginTop: "auto", height: "32px" }} value={endpoint}
disabled={disabled} onChange={setEndpoint}
> __nextHasNoMarginBottom
{__("Add", "wpct-erp-forms")} />
</Button> </div>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<SelectControl
label={__("Form", "wpct-erp-forms")}
value={formId}
onChange={setFormId}
options={formOptions}
__nextHasNoMarginBottom
/>
</div>
</div>
<Spacer paddingY="calc(8px)" />
<div
style={{
display: "flex",
gap: "1em",
flexWrap: "wrap",
}}
>
<div>
<label
style={{
display: "block",
fontWeight: 500,
textTransform: "uppercase",
fontSize: "11px",
margin: 0,
marginBottom: "calc(4px)",
maxWidth: "100%",
}}
>
{__("Add form", "wpct-erp-forms")}
</label>
<Button
variant="primary"
onClick={() => onClick()}
style={{ width: "130px", justifyContent: "center", height: "32px" }}
disabled={disabled}
>
{__("Add", "wpct-erp-forms")}
</Button>
</div>
</div> </div>
</div> </div>
); );
...@@ -151,42 +226,69 @@ export default function FormHook({ update, remove, ...data }) { ...@@ -151,42 +226,69 @@ export default function FormHook({ update, remove, ...data }) {
style={{ style={{
display: "flex", display: "flex",
gap: "1em", gap: "1em",
flexWrap: "wrap",
}}
>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<TextControl
ref={nameInput}
label={__("Name", "wpct-erp-forms")}
help={
nameConflict
? __("This name is already in use", "wpct-erp-forms")
: ""
}
value={name}
onChange={handleSetName}
onFocus={() => (focus = true)}
onBlur={() => (focus = false)}
__nextHasNoMarginBottom
/>
</div>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<SelectControl
label={__("Backend", "wpct-erp-forms")}
value={data.backend}
onChange={(backend) => update({ ...data, backend })}
options={backendOptions}
__nextHasNoMarginBottom
/>
</div>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<SelectControl
label={__("Method", "wpct-erp-forms")}
value={data.method}
onChange={(method) => update({ ...data, method })}
options={methodOptions}
__nextHasNoMarginBottom
/>
</div>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<TextControl
label={__("Endpoint", "wpct-erp-forms")}
value={data.endpoint}
onChange={(endpoint) => update({ ...data, endpoint })}
__nextHasNoMarginBottom
/>
</div>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<SelectControl
label={__("Form", "wpct-erp-forms")}
value={data.form_id}
onChange={(form_id) => update({ ...data, form_id })}
options={formOptions}
__nextHasNoMarginBottom
/>
</div>
</div>
<Spacer paddingY="calc(8px)" />
<div
style={{
display: "flex",
gap: "1em",
flexWrap: "wrap",
}} }}
> >
<TextControl
ref={nameInput}
label={__("Name", "wpct-erp-forms")}
help={
nameConflict
? __("This name is already in use", "wpct-erp-forms")
: ""
}
value={name}
onChange={handleSetName}
onFocus={() => (focus = true)}
onBlur={() => (focus = false)}
__nextHasNoMarginBottom
/>
<SelectControl
label={__("Backend", "wpct-erp-forms")}
value={data.backend}
onChange={(backend) => update({ ...data, backend })}
options={backendOptions}
__nextHasNoMarginBottom
/>
<TextControl
label={__("Endpoint", "wpct-erp-forms")}
value={data.endpoint}
onChange={(endpoint) => update({ ...data, endpoint })}
__nextHasNoMarginBottom
/>
<SelectControl
label={__("Form", "wpct-erp-forms")}
value={data.form_id}
onChange={(form_id) => update({ ...data, form_id })}
options={formOptions}
__nextHasNoMarginBottom
/>
<div> <div>
<label <label
style={{ style={{
......
...@@ -9,12 +9,13 @@ import { useI18n } from "../../providers/I18n"; ...@@ -9,12 +9,13 @@ import { useI18n } from "../../providers/I18n";
export default function FormHooks({ hooks, setHooks }) { export default function FormHooks({ hooks, setHooks }) {
const __ = useI18n(); const __ = useI18n();
const tabs = hooks const tabs = hooks
.map(({ backend, endpoint, form_id, name, pipes }) => ({ .map(({ backend, method, endpoint, form_id, name, pipes }) => ({
name, name,
title: name, title: name,
form_id,
endpoint,
backend, backend,
method,
endpoint,
form_id,
pipes, pipes,
})) }))
.concat([ .concat([
......
// vendor // vendor
import React from "react"; import React from "react";
import { TextControl, SelectControl, Button } from "@wordpress/components"; import {
TextControl,
SelectControl,
Button,
__experimentalSpacer as Spacer,
} from "@wordpress/components";
import { useState, useRef, useEffect } from "@wordpress/element"; import { useState, useRef, useEffect } from "@wordpress/element";
// source // source
...@@ -52,47 +57,80 @@ function NewFormHook({ add }) { ...@@ -52,47 +57,80 @@ function NewFormHook({ add }) {
style={{ style={{
display: "flex", display: "flex",
gap: "1em", gap: "1em",
flexWrap: "wrap",
}} }}
> >
<TextControl <div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
label={__("Name", "wpct-erp-forms")} <TextControl
help={ label={__("Name", "wpct-erp-forms")}
nameConflict help={
? __("This name is already in use", "wpct-erp-forms") nameConflict
: "" ? __("This name is already in use", "wpct-erp-forms")
} : ""
value={name} }
onChange={handleSetName} value={name}
__nextHasNoMarginBottom onChange={handleSetName}
/> __nextHasNoMarginBottom
<SelectControl />
label={__("Backend", "wpct-erp-forms")} </div>
value={backend} <div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
onChange={setBackend} <SelectControl
options={backendOptions} label={__("Backend", "wpct-erp-forms")}
__nextHasNoMarginBottom value={backend}
/> onChange={setBackend}
<TextControl options={backendOptions}
label={__("Model", "wpct-erp-forms")} __nextHasNoMarginBottom
value={model} />
onChange={setModel} </div>
__nextHasNoMarginBottom <div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
/> <TextControl
<SelectControl label={__("Model", "wpct-erp-forms")}
label={__("Form", "wpct-erp-forms")} value={model}
value={formId} onChange={setModel}
onChange={setFormId} __nextHasNoMarginBottom
options={formOptions} />
__nextHasNoMarginBottom </div>
/> <div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<Button <SelectControl
variant="primary" label={__("Form", "wpct-erp-forms")}
onClick={() => onClick()} value={formId}
style={{ marginTop: "auto", height: "32px" }} onChange={setFormId}
disabled={disabled} options={formOptions}
> __nextHasNoMarginBottom
{__("Add", "wpct-erp-forms")} />
</Button> </div>
</div>
<Spacer paddingY="calc(8px)" />
<div
style={{
display: "flex",
gap: "1em",
flexWrap: "wrap",
}}
>
<div>
<label
style={{
display: "block",
fontWeight: 500,
textTransform: "uppercase",
fontSize: "11px",
margin: 0,
marginBottom: "calc(4px)",
maxWidth: "100%",
}}
>
{__("Add form", "wpct-erp-forms")}
</label>
<Button
variant="primary"
onClick={() => onClick()}
style={{ width: "130px", justifyContent: "center", height: "32px" }}
disabled={disabled}
>
{__("Add", "wpct-erp-forms")}
</Button>
</div>
</div> </div>
</div> </div>
); );
...@@ -152,42 +190,60 @@ export default function FormHook({ update, remove, ...data }) { ...@@ -152,42 +190,60 @@ export default function FormHook({ update, remove, ...data }) {
style={{ style={{
display: "flex", display: "flex",
gap: "1em", gap: "1em",
flexWrap: "wrap",
}}
>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<TextControl
ref={nameInput}
label={__("Name", "wpct-erp-forms")}
help={
nameConflict
? __("This name is already in use", "wpct-erp-forms")
: ""
}
value={name}
onChange={handleSetName}
onFocus={() => (focus = true)}
onBlur={() => (focus = false)}
__nextHasNoMarginBottom
/>
</div>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<SelectControl
label={__("Backend", "wpct-erp-forms")}
value={data.backend}
onChange={(backend) => update({ ...data, backend })}
options={backendOptions}
__nextHasNoMarginBottom
/>
</div>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<TextControl
label={__("Model", "wpct-erp-forms")}
value={data.model}
onChange={(model) => update({ ...data, model })}
__nextHasNoMarginBottom
/>
</div>
<div style={{ flex: 1, minWidth: "150px", maxWidth: "250px" }}>
<SelectControl
label={__("Form", "wpct-erp-forms")}
value={data.form_id}
onChange={(form_id) => update({ ...data, form_id })}
options={formOptions}
__nextHasNoMarginBottom
/>
</div>
</div>
<Spacer paddingY="calc(8px)" />
<div
style={{
display: "flex",
gap: "1em",
flexWrap: "wrap",
}} }}
> >
<TextControl
ref={nameInput}
label={__("Name", "wpct-erp-forms")}
help={
nameConflict
? __("This name is already in use", "wpct-erp-forms")
: ""
}
value={name}
onChange={handleSetName}
onFocus={() => (focus = true)}
onBlur={() => (focus = false)}
__nextHasNoMarginBottom
/>
<SelectControl
label={__("Backend", "wpct-erp-forms")}
value={data.backend}
onChange={(backend) => update({ ...data, backend })}
options={backendOptions}
__nextHasNoMarginBottom
/>
<TextControl
label={__("Model", "wpct-erp-forms")}
value={data.model}
onChange={(model) => update({ ...data, model })}
__nextHasNoMarginBottom
/>
<SelectControl
label={__("Form", "wpct-erp-forms")}
value={data.form_id}
onChange={(form_id) => update({ ...data, form_id })}
options={formOptions}
__nextHasNoMarginBottom
/>
<div> <div>
<label <label
style={{ style={{
......
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