Framework API¶
- Service contracts exist to improve upgrades, decouple modules and formalise customisations
- They exist in the form of interfaces implemented by classes
- They allow an interface to stay the same while changing the implementation
- Developers should rely only on classes' public methods
API Types¶
- Data APIs
- Provice access to modules' entity data through CRUD methods
- Found in the module's Api/Data folder
- Operational APIs
- Provides both entity data and allows for actions and operations
- Found in the module's Api folder
- Repositories are the API equivalent to collections
- Business Logic API provides actual business operations
- The Framework API provides the interfaces and class implementations
(Model)Repository implements \Magento\Catalog\Api\ProductRespositoryInterface
- Respository interfaces may extend
Framework\Api\SearchCriteria
and implement::getList($criteria)
AbstractSimpleObject¶
- Similar to Magento 1's
Varien_Object
- Has a data property, but no public magic getters or setters
- Provides
- An
array $data
constructor parameter and$data
property _get($key)
setData($key, $value)
__toArray()
- An
SimpleBuilderInterface¶
- Defines two methods
create()
getData()
- AbstractSimpleObjectBuilder implements this by instantiating the object with the
$data
AbstractServiceCollection¶
- Service Collections are collections that get items from respositories rather than the ORM
- Allows for converting filters and sorts into a
SearchCriteria
instance - Admin Grids often use Service Collections as a data source
- Provides:
__construct(EntityFactoryInterface, FilterBuilder, SearchCriteriaBuilder, SortOrderBuilder)
addFieldToFilter($field, $condition)
getSearchCriteria()
createFilterData($field, $condition)
Repositories¶
- Extend
Magento\Framework\SearchCriteria
- Exist to provide access to data sources
- Allows the sourcing to be decoupled from data sources
- Are services, so can be relied upon to be stable, where collections can not
- Deal with data objects rather than models
- Use SearchCriteria for filtering
- Have a
getList()
method, expectingMagento\Framework\SearchCriteriaInterface
Magento\Customer\Api\CustomerRepositoryInterface example¶
- Provides
save(\Magento\Customer\Api\Data\CustomerInterface $customer, $passwordHash)
get($email, $websiteId)
getById($customerId)
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
delete(\Magento\Customer\Api\Data\CustomerInterface $customer)
deleteById($customerId)
SearchCriteria¶
- Filters are added using
$this->searchCriteriaBuilder->addFilters()
calls followed by acreate()
call SearchCriteriaInterface
defines:getFilterGroups()
setFilterGroups(array $filterGroups = null)
getSortOrders()
setSortOrders(array $sortOrders = null)
getPageSize()
setPageSize($pageSize)
getCurrentPage()
setCurrentPage($currentPage)
Architecture¶
SearchCriteriaBuilder
- Extends
AbstractSimpleObjectBuilder
create()
createsSearchCriteria
- Extends
AbstractSimpleObject
- Implements
SearchCriteriaInterface
- Uses
Search\FilterGroup
- Uses
Filter
- Uses
- Extends
- Extends
Filter object¶
- Extends
AbstractSimpleObject
- Adds methods to get/set the
$data
fieldget
/setField()
get
/setValue()
get
/setConditionType()
Builder Relationships¶
- Filters
FilterGroup
extendsAbstractSimpleObject
FilterGroupBuilder
extendsAbstractSimpleObjectBuilder
- Filters are set using
FilterGroupBuilder::setFilters()
FilterGroupBuilder::create()
instantiates theFilterGroup
s
- SortOrders
SortOrder
extendsAbstractSimpleObject
SortOrderBuilder
extendsAbstractSimpleObjectBuilder
SortOrderBuilder
creates theSortOrders
based onsetField()
andsetDirection()
SearchResultsInterface¶
- Is the base interface returned by repositories'
getList()
methods - Provides
getItems()
setItems()
getSearchCritera()
setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
getTotalCount()
setTotalCount($totalCount)
- Extends
AbstractSimpleObject
Business Logic API¶
- Is responsible for all logic not contained in Repositories or the Data API
- Is responsible for basically all the actions a module can take
- Example is
Magento\Customer\Api\AccountManagementInterface
- Provides
validate()
,authenticate()
,resendConfirmation()
- Implemented by
Magento\Customer\Model\AccountManagement
- Provides
Data API¶
- Simplifies the SOAP API
- Defines an internal API for accessing the Data Models
- Defines the getters and setters for fields
- Should implement the API interface rather than plain data models
- Can be implemented using one of two methods
- Create specific data objects that only contain data, with getters/setters. This is the preferred approach.
- Use normal models that implement Data API interfaces, but also contain business logic
Extensible Objects and Extension Attributes¶
Extensible Objects¶
- Should extend
Magento\Framework\Api\AbstractExtensibleObject
- Implements
Magento\Framework\Api\CustomAttributesDataInterface
- Defines
const CUSTOM_ATTRIBUTES
getCustomAttribute($attributeCode)
setCustomAttribute($attributeCode, $attributeValue)
getCustomAttributes()
setCustomAttributes(array $attributes)
- Extends
Magento\Framework\Api\ExtensibleDataInterface
- Defines
const EXTENSION_ATTRIBUTES_KEY
- Defines
- Defines
- Implements
- Can call
get-
andsetExtensionAttributes()
- Api Data model interface defines attributes as constants
-
Data model implements the getters and setters
-
AbstractExtensibleObject
accepts a constructor argument ofExtensionAttributesFactory
orAttributeValueFactory
, added in DI - Customer Example
Customer\Api\Data\CustomerInterface
defines attributes as constants, and defines the getters and settersCustomer\Model\Data\Customer
provides the getters and setters
Configuration¶
extension_attributes.xml:
1 2 3 4 5 6 7 |
|
Metadata Objects¶
- Metadata objects are used to obtain a list of attributes
DefaultMetadataService
implementsMetadataServiceInterface
- Implements the
public function getCustomAttributes($dataObjectClassName = null)
- Implements the
AttributeMetadata
implementsMetadataServiceInterface
MetadataObjectInterface
definesget
/setAttributeCode()
Customer Module Example¶
Magento\Customer\Model\Data\AttributeMetadata
implementsAttributeMetadataInterface
- The Interface provides constants fpr the attribute metata keys such as
attribute_code
,frontend_input
etc - The concretion extends
AbstractSimpleObject
and implements the getters for the Interface's constants
Web API¶
- Exposes the module's Service Contract API through the Web API
- Extends the Magento 1 API
- Respositories and APIs can be made accessible through Web APIs
- REST APIs define methods in webapi.xml
- Has Areas for
webapi_rest
andwebapi_soap
, with their own di.xml files - API Calls are processed:
- A URL is called
webapi.xml
maps verbs and URLs to Service Class Interfaces and Methodswebapi.xml
also defines the ACL groupswebapi_rest|soap/di.xml
defines concretions for the Interfaces- The class/method is called
Configuration¶
webapi.xml:
1 2 3 4 5 6 7 8 9 10 |
|
Authentication¶
- ACLs exist as
self
,anonynmous
and a Magento ACL resource - Can be authenticated using OAuth for SOAP, tokens for REST or Sessions
SOAP¶
- Tokens are created in the Admin at System > Integration
- SOAP Wsdls are automatically generated based on Service Interfaces
- Accessible at
/soap?wdsl&services=ModuleInterfaceV1,Module2InterfaceV1
CustomerRespoitoryInterface::getList()
would be specified ascustomerCustomerRepositoryV1getList
- Accessible at
REST¶
- Token requests look like
/index.php/rest/V1/integration/admin/token
, submitting username and password - API calls
index.php/rest/V1/resource/id
with a headerAuthorization:Bearer(_TOKEN_)