Web Performance

Swagger in Magento ® 2. Tips for Developers

Feb 21, 2017 1 min read 277 views
Listen audio
Swagger in Magento ® 2. Tips for Developers

It is surely no surprise for developers to work with HTML-pages on the server side and to assemble them on the client’s side. Magento ® 2 also attempts to provide such advantages by differentiating the data processing and data presentation process.

In this article, we review such tips.

We don’t consider the use of the features of Swagger, we just give an example of how to use it when integrated with Magento ® 2, in the anonymous and authenticated user modes.

Access to  Magento ® 2 Services

We deployed a module for Magento ® at http://mage220.gomage.com/swagger/

(login: gomage; password: uQuo1xae).

To start using Swagger in the anonymous user mode, you should go http://mage220.gomage.com/swagger/ . At first, the Swagger application is loaded and it is fast enough, and then there is loading of the web-services description, which is slow in Magento ®.

img_Swagger1

Anonymous requests

Then Swagger generates a list of services that are available to an anonymous client:

img_Swagger2

And it allows you the ability to analyze these services in a comfortable way:

img_Swagger3

and also it allows users to form queries:

img_Swagger4

and analyze responses:

img_Swagger5

Access to the limited services

First, you need to make an extra user account in Magento ® (specifying admin user for access to the service is not a good idea given the security hole that is opened up as a result):

img_Swagger_Magento_6

then assign access rights (User Roles):

img_Swagger_Magento_75

We recommend following this procedure:

First, create a role and then create a new user, and assign the role to that user.

In this process, note that providing all rights through the role is not a correct approach, but it is possible. In our example, the role of “GoMage” is assigned to the user “GoMage” and full access to any resources has been assigned accordingly.

Authentication via the web-interface is not available in Magento ® 2 which means you need to authenticate manually through universal REST API client, through code or through the “curl”:

curl -X POST "http://mage220.gomage.com/rest/V1/integration/admin/token" \
     -H "Content-Type:application/json" \
     -d '{"username":"GoMage", "password":"dsgdifksFlsjgkcxvgdrgxj"}'

All you need to do is send some JSON-text with the parameters of authentication to a specific address (don’t forget about the header with request content type). In reply, you receive the session code to access the limited services.

The code will be received in quotation marks, but you can use it without them. Then enter the code in the appropriate field:

img_Swagger_Magento_8

And you get access to an extensive list of services.

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

There is no doubt that using Swagger makes your work much more convenient and gives a lot of advantages in the process. If you have any questions or propositions, contact us at [email protected].

 

That's where you contact us!

    By submitting this form you agree to GoMage's Terms of Use and Privacy Policy
    woo-hoo! Now its time to keep checking your inbox, as we will be getting in touch soon. Promise :)
    oops! Thanks. But it seems like some kind of technical issues stop you from meeting GOMAGE. Could you try again?