This commit is contained in:
2020-09-08 23:43:57 +00:00
parent f70cb8907a
commit a8f2a1e51c
2 changed files with 15 additions and 9 deletions

View File

@@ -264,7 +264,7 @@ The ayGetFromAPI function works with GET routes in the API:
```async function ayGetFromAPI(route, token) {...```
The `token` parameter is optional and if not provided will be set by default to the current User's access token.
*POST*
@@ -274,17 +274,19 @@ The ayPostToAPI function works with POST routes in the API:
```async function ayPostToAPI(route, data, token) {...```
The `token` parameter is optional and if not provided will be set by default to the current User's access token.
The `route` parameter will be automatically prepended with the server local api url if you do not provide the starting "http" in the URL. Do not include the initial slash.
*Parameters*
The `route` parameter should be the complete route to the api method required and can be constructed from the meta data provided to the report, for example to fetch data from the `server-info` API route:
The `route` parameter is required and will automatically include the current API server URL prepended if not provided (it looks for "http" at the start). For example you can specifiy the `route` parameter as simply the end portion of the route without the slash: "server-info" or the full route to the server from the server URL meta property. For example the full route to fetch data from the `server-info` API route can be constructed as follows:
```let route=`${reportData.ayServerMetaData.ayApiUrl}server-info`;```
The `token` parameter is the authorization token provided in the Client data as follows:
The `token` parameter is optional and if not provided will be set by default to the current User's access token or you can specify it by using the authorization token provided in the Client data as follows:
```let token=reportData.ayClientMetaData.Authorization;```
The `data` parameter of a POST route differs for each route and is documented in the developer's API documentation.
The following is an example of accessing the API with both a GET and POST action:
```
@@ -293,27 +295,25 @@ async function ayPrepareData(reportData) {
//Example API GET method
//to fetch data from API server
//using the "server-info" route
let route=`${reportData.ayServerMetaData.ayApiUrl}server-info`;
//Add the data to the main report data object
//so it's available to the template
reportData.myData=
{
ServerInfo:await ayGetFromAPI(
route
"server-info"
)
};
//Example API POST method to fetch data from api server
//using the "search" route
route=`${reportData.ayServerMetaData.ayApiUrl}search`;
//construct the POST object
let searchPostData={phrase: "Fish"};
reportData.myData.SearchResults = await ayPostToAPI(
route,
"search",
searchPostData
);
return reportData;

View File

@@ -74,6 +74,9 @@ async function ayGetTranslations(keys) {
//
async function ayGetFromAPI(route, token) {
token = token || AYMETA.ayClientMetaData.Authorization;
if(route && !route.startsWith("http")){
route = AYMETA.ayServerMetaData.ayApiUrl + route;
}
try {
let r = await fetch(route, {
method: "get",
@@ -98,6 +101,9 @@ async function ayGetFromAPI(route, token) {
//
async function ayPostToAPI(route, data, token) {
token = token || AYMETA.ayClientMetaData.Authorization;
if(route && !route.startsWith("http")){
route = AYMETA.ayServerMetaData.ayApiUrl + route;
}
try {
fetchOptions = {
method: "post",