restful api's com frapi
DESCRIPTION
Crie suas APITRANSCRIPT
Agenda
Eu
API's
Frapi
Demo
Mercantólogo por formação, desenvolvedor por paixão!
Ativista por consciência!
Alex Piaz (@zaip)[email protected]
http://www.zaip.net
Há 15+ anos nas interwebs...
Há 13 anos...
1998
Há 7 anos...
1998
www.socioambiental.org
Gleilson Miranda/Secretaria de Comunicação do Estado do Acre/FUNAI
Chefe Almir Suruí
1998
É só fazer um "XML" que resolve....
É só fazer um "XML" que resolve....
Ao vivo é muito pior!
Ao vivo é muito pior!http://www.youtube.com/watch?v=9C-IDT__1HE
APIhttp://en.wikipedia.org/wiki/Application_programming_interface
UMA API É UM CONTRATO !
Controle de acesso
IDENTIFICACAO
http://blogs.estadao.com.br/radar-pop/homem-e-preso-em-recife-por-apresentar-rg-com-foto-de-jack-nicholson/
Autorização
Uso
Documentação
Formatos de saída
versões
Mensagens!
ReSTRepresentational State Transfer
http://alganet.github.io/rest-client-side/#/rest-roy
Estilo Arquitetural
HTTPRFC 2616
HTTP PARA DESIGNERS
Cliente / Servidor
Interface Uniforme
Camadas
Stateless
Cache
ReSTafarianos x Pragmaresticos
ReST Constraints● Cliente/Servidor
● Interface Uniforme
● Cache
● Stateless
● Camadas
● Código sob demanda *
Recursos :: URLs + Verbos HTTP
Interface Uniforme
GET http://api.piaz.com.br/ufoapi/v1/casos/1
POST http://api.piaz.com.br/ufoapi/v1/casos/1
PUT http://api.piaz.com.br/ufoapi/v1/casos/1
DELETE http://api.piaz.com.br/ufoapi/v1/casos/1
HEAD http://api.piaz.com.br/ufoapi/v1/casos/1
Recursos são substantivosPrefira o plural ao singular2 URLs por recurso
Manipulação dos recursos através de suas representações
Interface Uniforme
GET http://api.piaz.com.br/ufoapi/v1/casos/1
Mensagens auto-descritivas
Interface Uniforme
Cabeçalhos e Status Codes do HTTP
1xx - Informativo
2xx - Sucesso
3xx - Redirecionamento
4xx - Erro do cliente
5xx - Erro do servidor
http://stateless.co/hal_specification.html
HALHypertext Application Language
HATEOAS - Hypermedia as the Engine of Application State
Interface Uniforme
{ "_links": { "self": { "href": "/ufoapi/v1/casos/2" }, "next": { "href": "/ufoapi/v1/casos/3" }, "prev": { "href": "/ufoapi/v1/casos/1" }, } ,}
WRMLWeb resource modeling languagehttp://www.wrml.org/modelingLanguage
Richardson Maturity Model
E o FRAPI entra em cena!
http://getfrapi.com
FRAMEWORK API
INSTALAÇÃO
INSTALAÇÃO / Requisitos mínimos
○ Webserver (apache, nginx, iis)
○ PHP >= 5.2.4○ gettext○ XMLWriter○ PEAR
○ APC ** Pode ser instalado sem o APC ou com outro mecanismo de caching mas não é recomendado http://frapi.github.com/installing/caching.html
http://frapi.github.com/installing/index.html
git clone git://github.com/frapi/frapi.gitgit clone git://github.com/zaip/frapi.git
ou
wget https://github.com/frapi/frapi/tarball/master
tar -xzvf frapi-frapi-VERSION.tar.gz
FRAPI_PATH = local/onde/voce/desempacotou/o/frapi
INSTALAÇÃO / Obtendo o software
Interface administrativa API Pública
Camadas de interação
● Zend Framework● Actions e Errors● Autenticação● Documentação● Ambiente de testes● Produção
● PHP● Cache● Request● Response
Interface CLI
● Admin via terminal
<VirtualHost *:80> ServerName admin.frapi DirectoryIndex index.php ServerAdmin [email protected]
# This should be omitted in the production environment SetEnv APPLICATION_ENV development
DocumentRoot FRAPI_PATH/src/ frapi/admin/public <Directory FRAPI_PATH/src/frapi/admin/public> AllowOverride All Order deny,allow Allow from All </Directory></VirtualHost>
Interface Administrativa
<VirtualHost *:80> ServerName api.frapi ServerAdmin [email protected] DocumentRoot FRAPI_PATH/src/ frapi/public
# This should be omitted in the production environment SetEnv APPLICATION_ENV development <Directory FRAPI_PATH/src/frapi/public> AllowOverride All Order deny,allow Allow from All </Directory></VirtualHost>
API Pública
INSTALAÇÃO / Configurando
2 virtual host
Permissões de acesso
chown -R username.web-user FRAPI_PATHchmod 775 FRAPI_PATH/src/frapi/custom/Actionchmod 775 FRAPI_PATH/src/frapi/custom/Config/chmod 664 FRAPI_PATH/src/frapi/custom/Config/*.xml
Dá pra instalar sem 2 vhosts?
http://www.felipemarques.com.br/tag/alex-piaz/
SIM
INSTALAÇÃO / Finalizando
UFO APIPrincipais casos da Ufologia Mundial REST!
CRUD
UFO APIModelo
Mensagens de erro (Not found [404] / NO PUT/DELETE/POST [405] )
2 URLs === 2 actions
//api.piaz.com.br/ufoapi/v1/casos (GET POST)
//api.piaz.com.br/ufoapi/v1/casos/{id} (GET PUT DELETE)
Complexidade vai na Query String
//api.piaz.com.br/ufoapi/v1/casos?pais=Brasil
Principais casos da Ufologia Mundial em ReSTFul!
UFO APIPrincipais casos da Ufologia Mundial REST!
Custom_Model_Caso()
getCasos()
getCaso($id)
getCasosPorPais($pais)
insertCaso($data)
updateCaso($id,$data)
deleteCaso($id)
FRAPI_PATH/src/frapi/custom/Action
UFO APIPrincipais casos da Ufologia Mundial em ReSTFul!
Métodos disponibilizados pelo FRAPI
executeGet()
executePost()
executePut()
executeDelete()
executeHead()http://frapi.github.com/developing/coding-actions.html
UFO APIPrincipais casos da Ufologia Mundial em ReSTFul!
Mensagens de erro
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
UFO APIAction Casos - http://ufo-api.piaz.com.br/casos (GET POST HEAD)
UFO APIAction Casos - http://api.piaz.com.br/ufoapi/v1/casos (GET)
UFO APIAction Casos - http://api.piaz.com.br/ufoapi/v1/casos (POST)
UFO APIAction Casos - http://api.piaz.com.br/ufoapi/v1/casos (HEAD)
UFO APIPrincipais casos da Ufologia Mundial ReST!
http://api.piaz.com.br/ufoapi/v1/casos/{id} (POST PUT DELETE)
UFO APIAction Caso - http://api.piaz.com.br/ufoapi/v1/casos/{id} (GET)
UFO APIAction Caso - http://api.piaz.com.br/ufoapi/v1/casos/{id} (PUT)
UFO APIAction Caso - http://api.piaz.com.br/ufoapi/v1/casos/{id} (DELETE)
Gerando documentação...
http://frapi-admin/docs/generate/format/texthttp://frapi-admin/docs/generate/format/htmlhttp://frapi-admin/docs/generate/format/mdownhttp://frapi-admin/docs/generate/format/pdf
Utilidadesimplementing-restExploring the implementation aspects of the REST architectural style.
https://code.google.com/p/implementing-rest/
PostMan - Rest Client para o Chromehttps://chrome.google.com/webstore/detail/fdmmgilgnpjigdojojpjoooidkmcomcm
Tese do Roy Fieldling (nascimento do ReST)http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
Richardson Maturity Modelhttp://martinfowler.com/articles/richardsonMaturityModel.html
Swagger (Documentação para sua API)https://developers.helloreverb.com/swagger/
API Best Praticeshttp://apigee.com/about/api-best-practices
HTTP (RFC 2616)http://tools.ietf.org/html/rfc2616
Arquivos desta palestra
https://www.dropbox.com/sh/u0qqmm49ursrtpg/mofogHmsIi