Hi Buddy,

Welcome back to the Magento 2 Web APIs topic 🙂 As I promised last time in the Getting started with Magento 2 Web APIs post, today I will continue this topic with s a little script to make APIs visualization easier.

For example, this script prints out URLs, but does not display any information about the content of any JSON or XML data structures passed to or from REST API calls.

The first step is to put the following PHP program into a file “rest.php”. This script must be run from the Magento 2 home directory for it to work correctly. (It has the path app/code hard coded into it.)

<? 
foreach (glob("app/code/Magento/*/etc/webapi.xml") as $webapixml) {
 	$module = preg_replace('(app/code/Magento/(.*)/etc/webapi.xml)', '$1', $webapixml);
 	print "==== $module ====\n\n";
 	$xml = simplexml_load_file($webapixml);
 	foreach ($xml as $key => $value) {
    	$attr = $value->attributes();
    	$method = (string) $attr["method"];
    	$url = (string) $attr["url"];
    	$line = "	".(str_pad($method, 6))." ".$url;
    	print "$line\n";
	}
	print "\n";
}

The script will output the URLs and HTTP actions supported by each module. This is useful to understand the coverage of the API. The following shows the URLs for the first few modules.

==== Backend ====
 
	GET	/V1/modules
 
==== Bundle ====
 
	POST   /V1/bundle-products/:sku/links/:optionId
	PUT	/V1/bundle-products/:sku/links/:id
	GET	/V1/bundle-products/:productSku/children
	DELETE /V1/bundle-products/:sku/options/:optionId/children/:childSku
	GET	/V1/bundle-products/:sku/options/all
	GET	/V1/bundle-products/options/types
	GET	/V1/bundle-products/:sku/options/:optionId
	POST   /V1/bundle-products/options/add
	PUT	/V1/bundle-products/options/:optionId
	DELETE /V1/bundle-products/:sku/options/:optionId

==== Catalog ====
 
	POST   /V1/products
	PUT	/V1/products/:sku
	DELETE /V1/products/:sku
	GET	/V1/products
	GET	/V1/products/:sku
	GET	/V1/products/attributes/types
	GET	/V1/products/attributes/:attributeCode
	GET	/V1/products/attributes

Also of note is there is a directory of API tests in dev/tests/api-functional. In particular, you can turn on unit tests writing sample API REST calls and responses to disk by editing dev/tests/api-functional/phpunit.xml.dist to change the following constant to “true”.

<!--Generate documentation from REST tests and put it into var/log/rest-documentation directory-->
 <const name="GENERATE_REST_DOCUMENTATION" value="false" />

This constant is used by dev/tests/api-functional/framework/Magento/TestFramework/TestCase/Webapi/Adapter/Rest.php. Running the tests with PHPunit will generate quite detailed sample input and output parameter values based on the invoked test cases.

———

Other developers are challenging themselves with Magento test tool, beat them now earn rewards!

I’m gonna beat them!

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

Write A Comment