Swagger in Magento 2

    The tendency to move from assembling HTML pages on the server side to assembling them on the client side is no longer even a trend, but a trend. Magento 2, keeping pace with the times to the best of its capabilities, is also trying to be in the trend , spreading data processing and presentation. How can a simple developer look into the "clean" data if its presentation is far enough away? There are many other good solutions (starting with tcpdump ), and there is swagger .


    image


    Swagger is reasonably well integrated in Magento 2 (unless you had the idea of ​​a stupid idea to change the code for the "default" storefront). All that is needed to start looking at the “clean” data in Magento 2 from the point of view of a remote application is to use the integrated Swagger in it. In this article I do not consider the features of using Swagger itself, but simply give an example of how to use Swagger integrated in Magneto 2, in anonymous user mode and in authenticated user mode.


    Access to services


    I deployed a simple Magento module at http://module.sample.flancer32.com/ . To start using Swagger as an anonymous user, go to http://module.sample.flancer32.com/swagger First, the Swagger application itself is loaded (quickly), and then the description of web services implemented in Magento is loaded (slowly) :


    image


    The Magento services JSON scheme can also be pulled out by the link itself: http://module.sample.flancer32.com/rest/default/schema?services=all


    Anonymous Queries


    After parsing the scheme, Swagger generates a list of services available to an anonymous client:


    image


    and allows not only to study the services themselves in human-readable form:


    image


    ... but also form queries:


    image


    ... and analyze the answers:


    image


    Access to limited services


    First you need to create a separate user in Magento (using administrative accounts to access services is not a good idea):


    image


    and assign her access rights (User Roles):


    image
    Rather, the role is first made, and then, when created, it is assigned to the new user. Granting all rights through a role is also not a good idea, but I am not perfect either. Therefore, in our example, the user "swagger" is assigned the role "Swagger", which is granted access rights to any resources.


    Authentication via the web-based interface is not provided, maybe someday someone will finish this point in Magento 2, but for now you have to authenticate manually: through your favorite universal REST API client, through program code or through "curl":


    curl -X POST "http://module.sample.flancer32.com/rest/V1/integration/admin/token" \
         -H "Content-Type:application/json" \
         -d '{"username":"swagger", "password":"0jul4san0lRLKjYQpCqx"}'

    All you need to do is send a specific JSON text with authentication parameters to a specific address (without forgetting the header with the type of request content). In response, the session code for accessing limited services will come:


    "f6j0arlp29vx8i3av9l3eu5sag2pb5qy"

    The code will come in quotation marks, but you need to use it without.


    Well, now, having the code, we enter it into the appropriate field:


    image


    ... and get access to an already wider list of services. For example, to this:


    image


    Further work with services is no different from working in anonymous user mode. The authorization session key is automatically added by Swagger:


    image


    Conclusion


    Swagger is a great tool, wonderfully integrated into the great Magento 2 platform. Developing web services with such tools is a pleasure.


    Also popular now: