Commit 007e2a2e authored by Cédric Le Ninivin's avatar Cédric Le Ninivin Committed by Cédric Le Ninivin

erp5_api_style: Add view API call as cURL in API view gadget

parent 967dcdc8
...@@ -68,6 +68,52 @@ ...@@ -68,6 +68,52 @@
} }
function renderCurlCall(gadget, data, element) {
var curl_call;
curl_call = "curl -X 'POST' \\\n" +
" " + gadget.state.api_url + " \\\n" +
" -H 'accept: application/json' \\\n" +
" -H 'Content-Type: application/json' \\\n" +
" -d '" + data + "' \\\n" +
" -u your_username:your_password";
domsugar(
element,
[
domsugar("section", {"class": "ui-content-header-plain"},
[domsugar("br"), domsugar("h3", ["Curl Call"]),
domsugar("br")]),
domsugar("p", [domsugar("code", [domsugar("pre", [curl_call])])])
]
);
}
function displayCurlContent(gadget) {
var form_gadget;
return gadget.getDeclaredGadget("form_schema")
.push(function (result) {
form_gadget = result;
return form_gadget.checkValidity()
.push(function (is_valid) {
if (!is_valid) {
return gadget.notifySubmitted({
"message": "Data is not Valid, please make sure no error is left.",
"status": "failed"
});
}
return form_gadget.getContent()
.push(function (content_dict) {
var json_data = content_dict.my_key;
return renderCurlCall(
gadget,
json_data,
gadget.element.querySelector('.data-response')
);
});
});
});
}
function sendContent(gadget, data) { function sendContent(gadget, data) {
var url = gadget.state.api_url, var url = gadget.state.api_url,
http_method; http_method;
...@@ -128,7 +174,6 @@ ...@@ -128,7 +174,6 @@
var json_data = content_dict.my_key, var json_data = content_dict.my_key,
status, status,
message; message;
console.log(json_data);
return sendContent(gadget, json_data) return sendContent(gadget, json_data)
.push(function (response) { .push(function (response) {
enableButton(); enableButton();
...@@ -289,15 +334,26 @@ ...@@ -289,15 +334,26 @@
domsugar( domsugar(
gadget.element gadget.element
.querySelector('.dialog_button_container'), .querySelector('.dialog_button_container'),
[domsugar( [
'input', domsugar(
{ 'input',
name: 'action_confirm', {
'class': 'dialogconfirm', name: 'action_confirm',
type: 'submit', 'class': 'dialogconfirm',
value: "Send" type: 'submit',
} value: "Send"
)] }
),
domsugar(
'input',
{
name: 'get_curl',
'class': 'dialogconfirm',
type: 'submit',
value: "Generate Curl"
}
)
]
); );
displayAPIInformation(gadget); displayAPIInformation(gadget);
return gadget.getUrlFor({command: 'display', options: { return gadget.getUrlFor({command: 'display', options: {
...@@ -319,6 +375,10 @@ ...@@ -319,6 +375,10 @@
evt.preventDefault(); evt.preventDefault();
return submitDialog(this); return submitDialog(this);
} }
if (evt.target.name === "get_curl") {
evt.preventDefault();
return displayCurlContent(this);
}
}, false, false) }, false, false)
.onEvent('submit', function submit() { .onEvent('submit', function submit() {
return submitDialog(this); return submitDialog(this);
......
...@@ -242,7 +242,7 @@ ...@@ -242,7 +242,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1000.376.45673.9557</string> </value> <value> <string>1000.3573.16294.22084</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1651654628.56</float> <float>1651846416.67</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment