RESTFUL webservicesConstruindo Apis
http://bit.ly/ZhgkiB
Agenda
Eu
API's
Frapi
Demo
Mercantólogo por formação, desenvolvedor por paixão!
Ativista por consciência!
Alex Piaz (@zaip)
alex@piaz.com.br
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!
ReST
Representational State Transfer
http://alganet.github.io/rest-client-side/#/rest-roy
Estilo Arquitetural
HTTP
RFC 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 substantivos
Prefira o plural ao singular
2 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
HAL
Hypertext 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" },
} ,
}
WRML
Web resource modeling language
http://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.git
git 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 admin@api.frapi
# 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 admin@api.frapi
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_PATH
chmod 775 FRAPI_PATH/src/frapi/custom/Action
chmod 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 API
Principais casos da Ufologia Mundial REST!
CRUD
UFO API
Modelo
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/text
http://frapi-admin/docs/generate/format/html
http://frapi-admin/docs/generate/format/mdown
http://frapi-admin/docs/generate/format/pdf
Utilidades
implementing-rest
Exploring the implementation aspects of the REST architectural style.
https://code.google.com/p/implementing-rest/
PostMan - Rest Client para o Chrome
https://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 Model
http://martinfowler.com/articles/richardsonMaturityModel.html
Swagger (Documentação para sua API)
https://developers.helloreverb.com/swagger/
API Best Pratices
http://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
ReSTFul Api's com FRAPI

ReSTFul Api's com FRAPI