My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Thursday, January 30, 2020

Mapping template, Velocity Language & JsonPath: Guide to Api Gateway mapping templates mechanism

Amazon’s API Gateway provides the facilities to map an incoming request’s payload to match the required format of an integration backend. It uses the concept of “models” and “mapping templates” to specify the mapping between the client payload and the server payload.

The API Gateway mapping templates are used to transform an incoming payload into a different format. API Gateway allows you to define input mapping templates, for mapping the incoming request from a client to a server format, and output mapping templates, for mapping the outgoing response from the server to a client format.

Let us all remember that the mappings are defined using the Velocity Template Language combined with JSONPath expressions.

Velocity is a Java-based templating engine that uses a template language to reference objects in Java code. Velocity can be used to generate web XML, HTML or SQL from templates, for example. Here, we will use it to define the mapping between an input and output model.
A Velocity Template Language (VTL) template is composed of statements which begin with the # character and a followed by a directive. Also, it has references, which begin with the $ character. More generally, references begin with $ and are used to get or set something. Directives begin with # and are used to do something.
We can use the statement above to define an HTML template that sets a reference to a value, and retrieves that reference.

  
  #set( $foo = "Velocity" )
  Hello $foo World!
  
For more on VTL, consult the user guide.

In API Gateway mapping templates, the input to your Velocity template is defined by the reference variable $input. To access particular portions of the input JSON document within the template, use JSONPath.
JSONPath provides an XPath-like method of referring to a JSON structure. IN JSONPath, the abstract name $ refers to the root-level object. From this root-level object, you can use dot-notation to select more restrictive portions of the document. 
For example, to reference the playerId of the player model, you would use the following JSONPath expression.
$.playerId
or, you can use a bracket-notation to perform the same selection.
#['playerId']
More complex selections can be done using * for a wildcard, @ to refer to the current node in the document, array indexing, or recursive descent.

Now, we can put together a mapping template for our Player model. This template will just copy the input to the output with new names for field properties. In particular, it will convert an input document of
{
    "id": "AJamal",
    "alias": "Aha Jamal",
    "displayName": "Anwar Jamal Faiz",
    "profilePhotoUrl": "https://api.example.com/player/AJamal/avatar.png"
}
to an output document of
{
    "id": "AJamal",    "alias": "Aha Jamal",    "name": "Anwar Jamal Faiz",    "photo": "https://api.example.com/player/AJamal/avatar.png"}
The mapping to perform the above transformation is specified below, with some inline VTL comments added.
## Set the variable inputRoot to the root JSON document
#set($inputRoot = $input.path('$')) {

    ## Assign output properties to input properties
    "id": "$inputRoot.playerId",
    "alias": "$inputRoot.alias",
    "name": "$inputRoot.displayName",
    "photo": "$inputRoot.profilePhotoUrl"
}
Enjoy!!

Thursday, January 19, 2012

Using VBA with Excel : COM

This blog post was Posted over 3 years ago at http://www.yousaytoo.com/using-vba-with-excel-a-manifestation-of-com/10713 I posted this article with the title
Using VBA with Excel : A manifestation of COM
Component Object Model provides excellent technique for code and component reusability. For eg. Excel can interact with microsoft word. API and macros are tools which provide high end techniques for this purpose. Even excel functions can be used. Microsoft now recommends .NET platform over COM. But its efficiency and fame can never be forgotten or underestimated. COM [COM 95] refers to both a specification and implementation developed by Microsoft Corporation which provides a framework for integrating components. This framework supports interoperability and reusability of distributed objects by allowing developers to build systems by assembling reusable components from different vendors which communicate via COM. By applying COM to build systems of preexisting components, developers hope to reap benefits of maintainability and adaptability.

COM defines an application programming interface (API) to allow for the creation of components for use in integrating custom applications or to allow diverse components to interact.

However, in order to interact, components must adhere to a binary structure specified by Microsoft. As long as components adhere to this binary structure, components written in different languages can interoperate Usage Considerations A number of issues must be evaluated when considering COM, DCOM, and COM+. They include -- Platform support. COM and DCOM are best supported on Windows 95 and NT platforms. However, Microsoft has released a version of COM/DCOM for MacOS that supports OLE-style compound documents and the creation of ActiveX controls. Software AG, a Microsoft partner, has released DCOM for some UNIX operating systems, concretely OS/390, HP-UX 11.0, SUN Solaris, AIX 4.2, 4.3, Tru64 Unix 4.0 and Linux. However, DCOM over non-Windows platforms has few supporters.

Until DCOM for alternate platforms has solidified, the technology is best applied in environments that are primarily Windows-based. Platform specificity of COM/DCOM components. Because COM and DCOM are based on a native binary format, components written to these specifications are not platform independent. Thus, either they must be recompiled for a specific platform, or an interpreter for the binary format must become available. Depending on your perspective, the use of a binary format may be either an advantage (faster execution, better use of native platform capabilities) or a disadvantage (ActiveX controls, unlike Java applets, are NOT machine independent). See Java for more information. Security. Because COM/DCOM components have access to a version of the Microsoft Windows API, "bad actors" can potentially damage the user's computing environment. In order to address this problem, Microsoft employs "Authenticode" [Microsoft 96] which uses public key encryption to digitally sign components. Independent certification authorities such as VeriSign issue digital certificates to verify the identity of the source of the component [VeriSign 97]. However, even certified code can contain instructions that accidentally, or even maliciously, compromise the user's environment. Support for distributed objects. COM/DCOM provides basic support for distributed objects.

There is currently no support for situations requiring real time processing, high reliability, or other such specialized component interaction. Stability of APIs. In October of 1996 Microsoft turned over COM/DCOM, parts of OLE, and ActiveX to the Open Group (a merger of Open Software Foundation and X/Open). The Open Group has formed the Active Group to oversee the transformation of the technology into an open standard. The aim of the Active Group is to promote the technology's compatibility across systems (Windows, UNIX, and MacOS) and to oversee future extension by creating working groups dedicated to specific functions. However, it is unclear how much control Microsoft will relinquish over the direction of the technology. Certainly, as the inventor and primary advocate of COM and DCOM, Microsoft is expected to have strong influence on the overall direction of the technology and underlying APIs.

Long-term system maintainability. Microsoft is actively supporting COM and DCOM technology and pushing it in distributed and Web-based directions. Microsoft is also trying to preserve existing investments in COM technology while introducing incremental changes. Microsoft, for example, has ensured backward compatibility of COM+. Although this affirmation is in general true, COM objects that access local information in the registry or in system folders may require modification. In general, the PC community has not been faced with the concern of very long-lived systems, and vendors often provide support only for recent releases.

Now i think this gives enuff primitive ideas on triggering your interests towards COM and VBA, in case you are into that domain. Take some time in reading more articles and clicking on some advertisement links too...

Mohd Anwar Jamal Faiz