<% /*
    $Revision$
    $Id$

    File:           model_apis.inc

    Description:    Api model.
    Parameters:
    Context:

    Notes:
*/

%>
<!--#include file="./api2_dispatch.inc"-->
<!-- #include file="./model_apis.inc" -->
<%
model_apis =
{
    table: "apis",
    primary: "id",
    records_name: "swaggers",
    record_name: "swagger",
    fields : { "id"           : { typ: "varchar",      label: "Api" },
               "module"       : { typ: "varchar",      label: "Module" },
               "name"         : { typ: "varchar",      label: "Api" }
             },

    formatted_get: true,
    REST_GET: function _GET(params)
        {
            var autfunction = "WEB_APIDOC"; // is dit niet erg streng?
            user.checkAutorisation(autfunction); // pessimistisch

            var result = {
                "swagger": "2.0",
                "info":{
                   "description":"This is a description of the Facilitor API2."
                               + "<br>You are logged in as {0} ({1})".format(user.naam(), user.oslogin())
                               + "\n\nWarning: for POST/PUT/DELETE you must be logged off and specify an APIKEY in &lt;Authorize&gt;",
                   "version": FCLTVersion,
                   "title":"Facilitor",
                   "termsOfService":"http://swagger.io/terms/",
                   "contact":{
                      "email":"info@facilitor.nl"
                   }
                },
                "host": String(Request.ServerVariables("SERVER_NAME")),
                "basePath": rooturl + "/api2",
                "schemes":[
                   "http",
                   "https"
                ],
                "consumes":[
                   "application/json",
                   "application/xml"
                ],
                "produces":[
                   "application/json",
                   "application/xml"
                ],
               "securityDefinitions": {
                    "api_key": {
                        "type": "apiKey",
                        "name": "X-FACILITOR-API-Key",
                        "in": "header"
                      }
                    },
               "paths":{}
            };

            result.tags = [];
            var donetags = {};
            var oktags = {};
            var nn = 0;
            for (var dispatch in api2_mapper)
            {
                if (api2_mapper[dispatch].hidden)
                    continue;

                //if (nn++ > 4)
                //    break;
                var filepath = api2_mapper[dispatch].filename;
                var filename = filepath.split('/').pop();
                var module = api2_mapper[dispatch].module || filename.substr(0, 3).toUpperCase();
                if (!(module in oktags))
                {
                    oktags[module] = user.func_enabled2(module, { isOptional: true })
                    if (!oktags[module].anyfound) // niets geen rechten
                        __DoLog("Not authorized for module " + module);
                }

                if (!oktags[module].anyfound) // niets geen rechten
                    continue;

                if (!api2_mapper[dispatch].nodoc)
                {
                    var trans = "&swmodule={1}&fac_lang={2}".format(dispatch, module, getQParamSafe("fac_lang", user_lang)) + (api2_mapper[dispatch].docparam||"");
                    result.paths["/" + dispatch] =
                    {
                        "$ref": HTTP.urlzelf() + "/api2/{0}.api?swagger=1".format(dispatch) + trans
                    }
                    result.paths["/" + dispatch + "/{id}"] =
                    {
                        "$ref": HTTP.urlzelf() + "/api2/{0}.api?swagger=1&single=1".format(dispatch) + trans
                    }
                }

                if (donetags[module])
                    continue;
                donetags[module] = true;
                result.tags.push({ name: module, description: L("lcl_module_" + module) });
            }
            return result;
        },

    search: { autosearch: true },
    list: { default_url: "api2/{0}.scf" }
//    list: { default_url: "api2/{0}.api?pretty=1" }
}
%>