API Connect

 View Only
Expand all | Collapse all

Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

  • 1.  Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Tue October 25, 2022 10:34 AM
    I use apim and I add var apim = require('apim') to get and set variables from header and body. it's good with the apim but i have this warning .. How i can solve it ? 


    ------------------------------
    Hamza Bayar
    ------------------------------


  • 2.  RE: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Wed October 26, 2022 10:25 AM
    Use context variable etc.https://www.ibm.com/docs/en/api-connect/10.0.5.x_lts?topic=reference-api-connect-context-variables

    ------------------------------
    Gang Wu
    ------------------------------



  • 3.  RE: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Wed October 26, 2022 10:34 AM
    Hi Gang,
    thank you yes I start using that.

    ------------------------------
    Hamza Bayar
    ------------------------------



  • 4.  RE: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Wed October 26, 2022 04:24 PM

    Hi Hamza,

    The apim module in v10 provides v5 compatibility.  The module  also interfaces the new v10 context with the migrated code using the apim.functionname without needed to change.  The recommendation for new APIs is to use the native context functions (and thus the warnings), but understand that the results of the context functions may require some additional or different coding procedures.  For example, apim.getvariable('contextname.body')  if that payload is an XML payload as determined by contextname.headers.content-type, in v5 was returned as a XML Nodelist, whereas a parsed XML document in the v10 context will return a XML Document.  The apim module handles that difference for you assuming you have subsequent nodelist functions processing that nodelist, whereas when writing new code, you work directly with the XML Document that is returned.

    Best Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 5.  RE: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Thu September 14, 2023 10:07 AM

    Hi Steve,

    Thank for your explanation.

    But if I need to get full document (full API's swagger/yaml) that has possible to retrieve in version v5 and v10.0.1.9 without warning by methods:  apim.getVariable('document') in v5 and apim.getSwaggerDocument() in v10.0.1.9 , how can I do this? 

    Maybe there is any workaround ?

    Thanks,

    And with Regards,

    Sergey



    ------------------------------
    Sergey Paponov
    ------------------------------



  • 6.  RE: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Mon September 18, 2023 09:46 AM

    Hi Sergey,

    The native way to retrieve the swagger in a v10 API Gateway API without using the apim compatibility module is to use the asynchronous readAsJSON function as follows

    context.swagger.readAsJSON(function(error, swaggerDoc) {
      // continue with the code that would have been done after the apim.getSwaggerDocument
      // with either by embedding that code here or calling a callback function
    };

    The apim.getSwaggerDocument compatibility function will call the callback function for you, but you'll need to make that call yourself.
    Best Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 7.  RE: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Tue September 26, 2023 02:15 AM

    Thank you Steve!

    This is work.



    ------------------------------
    Sergey Paponov
    ------------------------------



  • 8.  RE: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Mon October 09, 2023 01:13 PM

    Hi Steve,

    Thank you for explaining this, I need a native way to retrieve this header

    var auth = apim.getvariable('request.headers.authorization').toString();

     And for returning an authentication error

    apim.error('authentication_error', 401, 'Unauthorized', error);

    what's the best practice here?



    ------------------------------
    Stefano Paci
    ------------------------------



  • 9.  RE: Warning : this gatewayscript policy should not use the apim module, which is only for migration old apis

    Posted Thu October 12, 2023 03:00 PM

    Hi Stefano,
    A great place to look in the documentation for this is https://www.ibm.com/docs/en/api-connect/10.0.1.x?topic=aplc-using-context-variables-in-gatewayscript-xslt-policies-datapower-api-gateway which has examples for both of these.
     
    For getting a request header 

    var auth = apim.getvariable('request.headers.authorization').toString();

    use 

    var auth = context.request.header.get('authorization').toString();

    the toString() function is redundant as the header is already a string. Note that the header.get function is case insensitive.  You could also use a context.get('request.headers.authorization') function but that is case sensitive, so it would be best to use the header.get function instead.

    For 

    apim.error('authentication_error', 401, 'Unauthorized', error);

    The apim.error v5(c) function allowed you to specify the error name, HTTP status code and reason phrase, and an error string, and would reject the transaction by under the covers doing a session.reject function. The context.reject function will abort your GatewayScript action, specifying the error name and error string, but setting the HTTP status code and phrase is done by setting message.statusCode explicitly.

    context.reject('authentication_error', error);
    context.message.statusCode = '401 Unauthorized';

    Best Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------