magento 2 seminar - andra lungu - api in magento 2
TRANSCRIPT
![Page 1: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/1.jpg)
1
API in Magento 2: what you can and you can't do
![Page 2: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/2.jpg)
2
Who Am iAndra Lungu - @iamspringerin
Magento Developer @Bitbull_IT
3+ magento development 3+ .net/java development
![Page 3: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/3.jpg)
3
WhY
ERP
SHOPPING APP
CRM CMS
Javascript widgets
WAREHOUSE
![Page 4: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/4.jpg)
4
API in Magento 1 Supported Protocols
● XML-RPC
● SOAP V1
● SOAP V2 since M1.3, WS-I compliant since M1.6
● REST since M1.7 with less business logic then others protocols *
Authentication:
● API user with assigned roles similar to ACL roles
● * 3-legged OAuth 1.0a
Documentation
● http://devdocs.magento.com/guides/m1x/api/soap/introduction.html
● http://devdocs.magento.com/guides/m1x/api/rest-api-index.html
![Page 5: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/5.jpg)
5
API in Magento 2Supported Protocols
● SOAP
● REST
Authentication:
● OAuth 1.0a 2-legged suggested for third-party applications
● Tokens suggested for mobile applications
● Session based
Documentation
● http://devdocs.magento.com/guides/v2.1/rest/bk-rest.html
● http://devdocs.magento.com/guides/v2.1/soap/bk-soap.html
![Page 6: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/6.jpg)
6
AUTH magento2User type
● Administrator or Integration
● Customer
● Guest user
Authorized resources. Example if authorized for the Magento_Customer::group resource, they can make a GET /V1/customerGroups/:id call.
Resources with anonymous or self permission.
Resources with anonymous permission.
![Page 7: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/7.jpg)
7
AUTH magento2 acl.xml permissions to access the resources
…...<acl> <resources> <resource id="Magento_Backend::admin"> <resource id="Magento_Backend::stores"> <resource id="Magento_Backend::stores_settings"> <resource id="Magento_Config::config"> <resource id="Magento_Customer::config_customer" title="Customers Section" translate="title" sortOrder="50" /> </resource> </resource> <resource id="Magento_Backend::stores_other_settings"> <resource id="Magento_Customer::group" title="Customer Groups" translate="title" sortOrder="10" /> </resource> </resource>……….
![Page 8: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/8.jpg)
8
AUTH magento2webapi.xml reference the permission needed for each api resource
<route url="/V1/customers/:email/activate" method="PUT"> <service class="Magento\Customer\Api\AccountManagementInterface" method="activate"/> <resources> <resource ref="Magento_Customer::manage"/> </resources> </route> <route url="/V1/customers/me/password" method="PUT"> <service class="Magento\Customer\Api\AccountManagementInterface" method="changePasswordById"/> <resources> <resource ref="self"/> </resources> <data> <parameter name="customerId" force="true">%customer_id%</parameter> </data> </route> <route url="/V1/customers/:customerId/password/resetLinkToken/:resetPasswordLinkToken" method="GET"> <service class="Magento\Customer\Api\AccountManagementInterface" method="validateResetPasswordLinkToken"/> <resources> <resource ref="anonymous"/> </resources>
![Page 9: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/9.jpg)
9
OAUTH 1.0a based auth● Requires implementation of the protocol on client side
● Add integration in the admin area and activate it
![Page 10: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/10.jpg)
10
Token based authcurl -X POST "https://magento.host/index.php/rest/V1/integration/customer/token" \ -H "Content-Type:application/json" \ -d '{"username":"[email protected]", "password":"customer1pw"}'
authorization: Bearer nj9plnx828w23ppp5u8e0po9sjrkqe0d
![Page 11: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/11.jpg)
11
SeSsion based authSelf access enables a user to access resources they own.
For example, GET /V1/customers/me fetches the logged-in customer's details typically useful for JavaScript-based widgets.
![Page 12: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/12.jpg)
12
BACKWARDS COMPATIBILITY& PHP annotations
Semantic Versioning MAJOR.MINOR.PATCH● MAJOR indicates incompatible API changes
● MINOR indicates backward-compatible functionality has been added
● PATCH indicates backward-compatible bug fixes
![Page 13: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/13.jpg)
13
BACKWARDS COMPATIBILITY& PHP annotations
Backward compatible applies for classes and methods annotated with @api within MINOR and PATCH updates to our components.
As changes are introduced, methods are annotated with @deprecated and removed only with the next MAJOR component version.
![Page 14: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/14.jpg)
14
BACKWARDS COMPATIBILITY& PHP annotations
Magento uses reflection to automatically create classes and sets data submitted in JSON or HTTP
array syntax onto an instance of the expected PHP class when calling the service method.
Conversely, if an object is returned from one of these methods, Magento automatically converts
that PHP object into a JSON or SOAP object before sending it over the web API.
![Page 15: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/15.jpg)
15
BACKWARDS COMPATIBILITY& PHP annotations
All methods exposed by the web API must follow these rules
● Parameters must be defined in the doc block as * @param type $paramName
● Return type must be defined in the doc block as * @return type
● Valid object types include a fully qualified class name or a fully qualified interface name.
● Any parameters or return values of type array can be denoted by following any of the previous types by
an empty set of square brackets []
![Page 16: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/16.jpg)
16
cuSTOMIZE AN API:Extension Attributes
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd"> <extension_attributes for="Magento\Catalog\Api\Data\ProductInterface"> <attribute code="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface"> <resources> <resource ref="Magento_CatalogInventory::cataloginventory"/> </resources> </attribute> </extension_attributes></config>
![Page 17: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/17.jpg)
17
CREATE AN API
![Page 18: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/18.jpg)
18
CREATE AN APINever been easier !!!
Bitbull/CustomApi/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Bitbull\CustomApi\Api\MagentoSeminarInterface" type="Bitbull\CustomApi\Model\MagentoSeminar" /></config>
Bitbull/CustomApi/etc/webapi.xml
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> <route url="/V1/magentoseminar/:eventName" method="GET"> <service class="Bitbull\CustomApi\Api\MagentoSeminarInterface" method="getAwesomeEvent"/> <resources> <resource ref="Magento_Catalog::products" /> </resources> </route></routes>
![Page 19: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/19.jpg)
19
CREATE AN APIBitbull/CustomApi/Api/MagentoSeminarInterface.php
namespace Bitbull\CustomApi\Api;
/** * @api */interface MagentoSeminarInterface{ /** * Get info about the conference * @api * @param string $eventName * @return string */ public function getAwesomeEvent($eventName);
}
![Page 20: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/20.jpg)
20
CREATE AN APIBitbull/CustomApi/Model/MagentoSeminar.php
namespace Bitbull\CustomApi\Model;
class MagentoSeminar implements \Bitbull\CustomApi\Api\MagentoSeminarInterface{
/* * @api * @param string $conferenceName * @return string */ public function getAwesomeEvent($eventName) { return $eventName . ' is an awesome event'; }}
![Page 21: Magento 2 Seminar - Andra Lungu - API in Magento 2](https://reader034.vdocuments.site/reader034/viewer/2022042619/5882cce81a28abf8388b4ef7/html5/thumbnails/21.jpg)
21
QUestions
Thank you