JSON API feature#
Description#
The JSON API has a brand new feature added to the module api which allows any 3rd party module developer to add api support to their module.
Requirements#
To use the Miva JSON API you must be using Miva 9.12.00 or greater as well as the 5.32 engine or greater.
Module example#
To add your functions to the json API your module must implement the “json_api” feature. It requires your module to implement the Module_JSON_API function which defines each allowable api function and its response.
<MvFUNCTION NAME = "Module_Description" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "">
<MvASSIGN NAME = "l.module:code" VALUE = "Sample">
<MvASSIGN NAME = "l.module:name" VALUE = "Sample Module">
<MvASSIGN NAME = "l.module:provider" VALUE = "Miva">
<MvASSIGN NAME = "l.module:version" VALUE = "9.0000">
<MvASSIGN NAME = "l.module:api_ver" VALUE = "5.72">
<MvASSIGN NAME = "l.module:features" VALUE = "json_api">
<MvASSIGN NAME = "l.module:description" VALUE = "Example Module">
</MvFUNCTION>
<MvFUNCTION NAME = "Module_JSON_API" PARAMETERS = "module var, function" STANDARDOUTPUTLEVEL = "">
<MvIF EXPR = "{ l.function EQ 'Export_Products' }"> <MvFUNCTIONRETURN VALUE = "{ JSON_API_Export_Products( l.module ) }">
<MvELSEIF EXPR = "{ l.function EQ 'Export_Custom_Data' }"> <MvFUNCTIONRETURN VALUE = "{ JSON_API_Export_Custom_Data( l.module ) }">
</MvIF>
<MvFUNCTIONRETURN VALUE = "{ [ g.Module_JSON ].JSON_Response_Error( 'MER-ERR-CODE-123', 'Invalid function' ) }">
</MvFUNCTION>
<MvFUNCTION NAME="JSON_API_Export_Products" PARAMETERS="module var" STANDARDOUTPUTLEVEL = "text, html, compresswhitespace">
<MvASSIGN NAME="l.ok" VALUE="{ miva_output_header( 'Content-Type', 'application/json' ) }" >
<MvCOMMENT>Load All Products</MvCOMMENT>
<MvIF EXPR="{ NOT [ g.Module_Library_DB ].ProductList_Load_All( l.products ) }">
<MvFUNCTIONRETURN VALUE = 0 >
</MvIF>
<MvCOMMENT>Add Retail Price to Structure</MvCOMMENT>
<MvASSIGN NAME = "g.product_count" VALUE ="{ miva_array_elements( l.products ) }">
<MvASSIGN NAME= "g.counter" value="1">
<MvWHILE EXPR = "{ g.counter LE g.product_count }">
<MvASSIGN NAME = "l.products" INDEX= "{ g.counter }" MEMBER="retail_price" VALUE ="{ l.products[ g.counter ]:price }">
<MvASSIGN NAME= "g.counter" value="{ g.counter + 1 }">
</MvWHILE>
<MvCOMMENT>Load Discountd Prices. This function replaces the price/formatted_price with the sale price</MvCOMMENT>
<MvIF EXPR="{ NOT [ g.module_feature_tui_ut ].CommonComponentFields_Initialize_Product_Discounts_Runtime( l.products, g.product_count ) }">
<MvFUNCTIONRETURN VALUE = 0 >
</MvIF>
<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_Response_Start() }">
<MvEVAL EXPR = "{ [ g.Module_JSON ].JSON_Output(l.products) }">
<MvFUNCTIONRETURN VALUE = "{ [ g.Module_JSON ].JSON_Response_End() }">
</MvFUNCTION>
<MvFUNCTION NAME = "JSON_API_Export_Custom_Data" PARAMETERS = "module var" STANDARDOUTPUTLEVEL = "">
<MvFUNCTIONRETURN VALUE = 1 >
</MvFUNCTION>
Example Request#
Below is a sample request to a custom module. The API endpoint is the same as well as all the authentication.
{
"Store_Code": "{{Store_Code}}",
"Function": "Module",
"Module_Code": "sample",
"Module_Function": "Export_Products"
}