apigility & restfull apis
TRANSCRIPT
ApigilityAgility: the power of moving the limbs quickly
and easily; A faculty of being agile in body, mind, or figuratively
Get up!wakey wakey
Who am I
FatherPHP DeveloperBehalf.com
This presentation
Very quick on basic stuffA short high speed videoAdvanced Apigility stuffI like my voice
(ask me about “ask me”)
Why APIs?
● Architecture● Future proof● Product enhancement● The unexpected ability
Eat your own dogfood
RESTful APIs: Glossary
When I say:
URL or path (nouns - accounts, banks...)Actions (verbs - GET, POST, PUT...)Output indicators (response codes - 200, 500..)Payload (response body)
What is Apigility
“The world's easiest way to create high-quality APIs”● Generate API wrappers for existing code● Generate direct-db APIs (video in a second)● Rest or RPC● ZF2 based. Service Managers● Super easy to install. No. Really.
Apigility UI basics
Apigility basics
When I say:
Resource - action respondersEntity - single display unit, ArraySerializableCollection - list of Entities for display
(Ask me about ArraySerializable)
The Fine art of manual configuration
● The UI may sometimes limit you● Some features are not exposed
o Advanced routingo Custom validatorso Custom authenticationo HAL Entity configurationo And so much more!
Apigility Resources
● Have own Factories● Methods return an entity object
● fetchAll() returns a collection entities
o Other group methods
● DELETE action returns boolean,becomes 204 No Content (wth?)
Apigility Resources
● Not Controllers● $data has already gone through inputFilter● fetch($id) does not run inputFilter
(Ask me about plugins)
Apigility input validation
● Zend\InputFilter speco validatorso filters
Services galore:'zf-content-validation' => array( 'Application\Controller\HelloWorld' => array( 'input_filter' => 'Application\Controller\HelloWorld\Validator', 'POST' => 'Application\Controller\HelloWorld\CreationValidator', ),),
Id or Me - filter spec example...1 => array( 'name' => 'vendorId', 'filters' => array( ... 1 => array( 'name' => 'MeToRequesterId', ),… ),),// somewhere else:$serviceManager->get('FilterManager')->setInvokableClass(
'MeToRequesterId', 'BehalfApi\Filter\MeToRequesterId');
● Manual Validation (GET vs POST)public function fetch($id) { $this->inputFilter->setData(array('linkId'=>$id)); if(!$this->inputFilter->isValid()){ return new ApiProblem(
ZendHttpResponse::STATUS_CODE_422,'Validation Error', null, null,$this->inputFilter->getMessages()); }…}(Ask me about ApiProblem vs exceptions)
Collections and paginators
Default Collection: extends PaginatorYour Resource::fetchAll() should:$collection = new MyCollection($adapter);return $collection;
Zend\Paginator accepts a $adapterYour adapter should:class BackendApi implements \Zend\Paginator\Adapter\AdapterInterface { public function getItems($offset, $itemCountPerPage) ; public function count();}
(Ask me about optimization)
Customise DB wrapped Apigility
Resource extends <Db>ConnectedListenerYou may override responder methods(Don’t forget to call the parent)public function create($data){ … logic … logic … logic … return parent::create($data);}
Next:Apigility Authentication
Authentication - config basics
zf-mvc-auth config basics:'zf-mvc-auth' => array( 'authentication' => array( 'adapters' => array('api' | 'user' | 'client' => array(...)), 'types' => array('basic', 'key', ...),)
Specify multiple adapters as a chain of responsibility. Each adapter supports “types” of authentication.Can specify “active” types for specific adapters (e.g. Http auth adapter basic, digest)
Authentication - custom adapter
Extend ZF\MvcAuth\Authentication\AdapterInterface
provide(), matches($type),getTypeFromRequest($request)
preAuth($request, $response)authenticate($request, $response, $mvcAuthEvent)
Authentication - Identity
Authentication result objectsGuest and Authenticated IdentitiesgetAuthenticationIdentity()
(Ask me about roles)
/apigility/thank?who=you
[email protected]/in/yonmantwitter.com/NamedLambda
Fun with HAL
Adding more linksconfigurationthrough hal links manager