apigility & restfull apis

22
Apigility Agility: the power of moving the limbs quickly and easily; A faculty of being agile in body, mind, or figuratively

Upload: yonni-mendes

Post on 02-Aug-2015

217 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Apigility & Restfull APIs

ApigilityAgility: the power of moving the limbs quickly

and easily; A faculty of being agile in body, mind, or figuratively

Page 2: Apigility & Restfull APIs

Get up!wakey wakey

Page 3: Apigility & Restfull APIs

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”)

Page 4: Apigility & Restfull APIs

Why APIs?

● Architecture● Future proof● Product enhancement● The unexpected ability

Eat your own dogfood

Page 5: Apigility & Restfull APIs

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)

Page 6: Apigility & Restfull APIs

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.

Page 7: Apigility & Restfull APIs

Apigility UI basics

Page 8: Apigility & Restfull APIs

Apigility basics

When I say:

Resource - action respondersEntity - single display unit, ArraySerializableCollection - list of Entities for display

(Ask me about ArraySerializable)

Page 9: Apigility & Restfull APIs

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!

Page 10: Apigility & Restfull APIs

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?)

Page 11: Apigility & Restfull APIs

Apigility Resources

● Not Controllers● $data has already gone through inputFilter● fetch($id) does not run inputFilter

(Ask me about plugins)

Page 12: Apigility & Restfull APIs

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', ),),

Page 13: Apigility & Restfull APIs

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');

Page 14: Apigility & Restfull APIs

● 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)

Page 15: Apigility & Restfull APIs

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)

Page 16: Apigility & Restfull APIs

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);}

Page 17: Apigility & Restfull APIs

Next:Apigility Authentication

Page 18: Apigility & Restfull APIs

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)

Page 19: Apigility & Restfull APIs

Authentication - custom adapter

Extend ZF\MvcAuth\Authentication\AdapterInterface

provide(), matches($type),getTypeFromRequest($request)

preAuth($request, $response)authenticate($request, $response, $mvcAuthEvent)

Page 20: Apigility & Restfull APIs

Authentication - Identity

Authentication result objectsGuest and Authenticated IdentitiesgetAuthenticationIdentity()

(Ask me about roles)

Page 21: Apigility & Restfull APIs

/apigility/thank?who=you

[email protected]/in/yonmantwitter.com/NamedLambda

Page 22: Apigility & Restfull APIs

Fun with HAL

Adding more linksconfigurationthrough hal links manager