Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

DJANGO-REST-FRAMEWORK: AWESOME WEB-BROWSABLE WEB APIS

Palestra ministrada por Filipe Ximenes e Fernando Rocha na PythonBrasil[9]

  • Loggen Sie sich ein, um Kommentare anzuzeigen.

DJANGO-REST-FRAMEWORK: AWESOME WEB-BROWSABLE WEB APIS

  1. 1. DJANGO-REST-FRAMEWORK AWESOME WEB-BROWSABLE WEB APIS por Filipe Ximenes e Fernando Rocha
  2. 2. Developer @ thinkr Apaixonado por Python Entusiasta de Software Livre Interessado em web e infraestrutura.
  3. 3. THINKR
  4. 4. Fundador da Vinta Desenvolvedor Web Apaixonado por aprender Fã de comunidades de Sofware Livre Curioso de empreendedorismo
  5. 5. DJANGO-REST-FRAMEWORK AWESOME WEB-BROWSABLE WEB API'S
  6. 6. API (APPLICATION PROGRAMMING INTERFACE) Uma Interface de Programação de Aplicação especifica como certos componentes de software devem interagir uns com os outros.
  7. 7. DJANGO- REST-FRAMEWORK AWESOME WEB-BROWSABLE WEB API'S
  8. 8. MÉTODOS HTTP GET POST
  9. 9. MÉTODOS HTTP (RFC 2616) GET POST HEAD PUT DELETE OPTIONS TRACE CONNECT
  10. 10. RESTFULL API Utiliza todo o potencial do HTTP para servir recursos através de métodos (verbos)
  11. 11. EXEMPLO /user_list /user_create /user?id=xxx /user_edit?id=xxx /user_delete?id=xxx
  12. 12. EXEMPLO RESTFULL GET POST GET PUT DELETE /user /user /user/id /user/id /user/id
  13. 13. DJANGO-REST-FRAMEWORK AWESOME WEB-BROWSABLE WEB API'S
  14. 14. DJANGO-REST-FRAMEWORK É um conjunto de ferramentas construidas a partir do Django que facilitam a construção de API's REST
  15. 15. NÚMEROS Criador - Tom Christie Estrelas - 1592 Forks - 464 Documentação
  16. 16. UMA VIEW SIMPLES
  17. 17. APIVIEW fo rs_rmwr.iw ipr AIiw rm etfaeokves mot PVe fo rs_rmwr.epneipr Rsos rm etfaeokrsos mot epne fo rs_rmwr.edrr ipr JORnee rm etfaeokrnees mot SNedrr casPlsrneVe(PVe) ls aetatsiwAIiw: rnee_lse =(SNedrr) edrrcass JORnee, dfgtsl,rqet *rs *kag) e e(ef eus, ag, *wrs: dt =['oe:'iie, aa {nm' Flp' 'iae:'eie, cdd' Rcf' 'mrs' 'it', epea: Vna} {nm' 'enno, 'oe: Frad' 'iae:'eie, cdd' Rcf' 'mrs' 'hnr} epea: Tik'] rtr Rsos(aa eun epnedt)
  18. 18. RESPOSTA HT/. 20O TP11 0 K CnetTp:plcto/sn otn-yeapiainjo [ { cdd:"eie, iae Rcf" epea "it" mrs: Vna, nm:"iie oe Flp" } , { cdd:"eie, iae Rcf" epea "hnr, mrs: Tik" nm:"enno oe Frad" } ]
  19. 19. RENDERIZADORES
  20. 20. DA PYTHON PARA TEXTO JORnee SNedrr UioeSNedrr ncdJORnee JOPedrr SNRnee YMRnee ALedrr XLedrr MRnee TmltHMRnee epaeTLedrr SaiHMRnee ttcTLedrr HMFrRnee TLomedrr BosbePRnee rwalAIedrr #aeedrr BsRnee
  21. 21. JSON rnee_lse =(SNedrr) edrrcass JORnee, HT/. 20O TP11 0 K CnetTp:plcto/sn otn-yeapiainjo [ { "oe:"iie, nm" Flp" "iae:"eie, cdd" Rcf" "mrs" "it" epea: Vna } , { "oe:"enno, nm" Frad" "iae:"eie, cdd" Rcf" "mrs" "hnr epea: Tik" } ]
  22. 22. XML rnee_lse =(MRnee, edrrcass XLedrr) HT/. 20O TP11 0 K CnetTp:plcto/m otn-yeapiainxl <xlvrin"."ecdn=uf8? ?m eso=10 noig"t-"> <ot ro> <itie> ls-tm <iaeRcf<cdd> cdd>eie/iae <mrs>it<epea epeaVna/mrs> <oeFlp<nm> nm>iie/oe <ls-tm /itie> <itie> ls-tm <iaeRcf<cdd> cdd>eie/iae <mrs>hnr/mrs> epeaTik<epea <oeFrad<nm> nm>enno/oe <ls-tm /itie> <ro> /ot
  23. 23. BROWSABLE API rnee_lse =(rwalAIedrr) edrrcass BosbePRnee,
  24. 24. PARSERS
  25. 25. DE TEXTO PARA PYTHON JOPre SNasr YMPre ALasr XLasr MPre FrPre omasr Mliatasr utPrPre FlUlaPre iepodasr #aeasr BsPre
  26. 26. AUTENTICAÇÃO
  27. 27. BASTA DECLARAR fo rs_rmwr.iw ipr AIiw rm etfaeokves mot PVe fo rs_rmwr.epneipr Rsos rm etfaeokrsos mot epne fo rs_rmwr.uhniainipr Ssinuhniain rm etfaeokatetcto mot esoAtetcto, Tknuhniain oeAtetcto casPlsrneVe(PVe) ls aetatsiwAIiw: atetcto_lse =(esoAtetcto, uhniaincass Ssinuhniain Tknuhniain) oeAtetcto, priso_lse =(suhniae, emsincass IAtetctd) dfgtsl,rqet *rs *kag) e e(ef eus, ag, *wrs: dt =['oe:'iie, aa {nm' Flp' 'iae:'eie, cdd' Rcf' 'mrs' 'it', epea: Vna} {nm' 'enno, 'oe: Frad' 'iae:'eie, cdd' Rcf' 'mrs' 'hnr} epea: Tik'] rtr Rsos(aa eun epnedt)
  28. 28. FALHA NA AUTENTICAÇÃO HT/. 41O TP10 0 K HTTP_401_UNAUTHORIZED HT/. 43O TP10 0 K HTTP_403_FORBIDDEN
  29. 29. AUTENTICAÇÃO PLUGÁVEL Bscuhniain aiAtetcto Tknuhniain oeAtetcto Ssinuhniain esoAtetcto Ouhuhniain AtAtetcto OuhAtetcto At2uhniain #aeuhniain BsAtetcto
  30. 30. DEFININDO DEFAULT NO SETTINGS RS_RMWR ={ ETFAEOK 'EAL_UHNIAINCASS:( DFUTATETCTO_LSE' 'etfaeokatetcto.esoAtetcto' rs_rmwr.uhniainSsinuhniain, 'etfaeokatetcto.oeAtetcto' rs_rmwr.uhniainTknuhniain ) , }
  31. 31. SIMPLIFICANDO A VIEW
  32. 32. GENERICAPIVIEW models.py fo dag.bipr mdl rm jnod mot oes casSekrmdl.oe) ls pae(oesMdl: nm =mdl.hril('oe,mxlnt=0 ae oesCaFeduNm' a_egh5) ct =mdl.hril('iae,mxlnt=0 iy oesCaFeduCdd' a_egh5) cmay=mdl.hril('mrs' mxlnt=0 opn oesCaFeduEpea, a_egh5) poe=mdl.hril('eeoe,mxlnt=2 bakTu) hn oesCaFeduTlfn' a_egh1, ln=re views.py fo rs_rmwr ipr gnrc rm etfaeok mot eeis fo .oesipr Sekr rm mdl mot pae casSekritgnrc.itraePVe) ls paeLs(eeisLsCetAIiw: '' ' Edon qerpeet alsad plsrne,eprieqenvs npit u ersna it e aetats emt u oo plsrne sjmcdsrds aetats ea aatao. '' ' mdl=Sekr oe pae
  33. 33. GET /SPEAKERS/ HT/. 20O TP10 0 K Alw GT PS,HA,OTOS lo: E, OT ED PIN CnetTp:apiainjo;castuf8 otn-ye plcto/sn hre=t[ { "iy:"eie, ct" Rcf" "opn" "hnr, cmay: Tik" "d:1 i" , "ae:"ennoRca, nm" Frad oh" "hn" "2468 poe: 1357" } , { "iy:"eie, ct" Rcf" "opn" "it" cmay: Vna, "d:2 i" , "ae:"iieXmns, nm" Flp iee" "hn" "2468 poe: 1357" } ]
  34. 34. POST /SPEAKERS/ request { } response HT/. 40BDRQET TP10 0 A EUS Alw GT PS,HA,OTOS lo: E, OT ED PIN CnetTp:apiainjo;castuf8 otn-ye plcto/sn hre=t{ "iy:[ ct" "hsfedi rqie. Ti il s eurd" ] , "opn" [ cmay: "hsfedi rqie. Ti il s eurd" ] , "ae:[ nm" "hsfedi rqie. Ti il s eurd" ] }
  35. 35. POST /SPEAKERS/ request {nm' 'ae' 'opn' 'ND,'hn' '111} 'ae: Dkr, cmay: IT' poe: 333' response HT/. 21CETD TP10 0 RAE Alw GT PS,HA,OTOS lo: E, OT ED PIN CnetTp:apiainjo;castuf8 otn-ye plcto/sn hre=t{ "iy:"eie, ct" Rcf" "opn" "ND, cmay: IT" "d:3 i" , "ae:"ae" nm" Dkr "hn" "111 poe: 333" }
  36. 36. GENERIC VIEWS CetAIiw raePVe LsAIiw itPVe RtivAIiw ereePVe DsryPVe etoAIiw UdtAIiw paePVe LsCetAIiw itraePVe RtivUdtAIiw ereepaePVe RtivDsryPVe ereeetoAIiw RtivUdtDsryPVe ereepaeetoAIiw
  37. 37. CUSTOMIZANDO OS DADOS (SERIALIZADORES)
  38. 38. SERIALIZADORES Converte objetos de Python para listas e dicionários Converte dicionários e listas para objetos de Python Semelhante a API de forms do Django
  39. 39. DEFININDO CAMPOS serializers.py fo rs_rmwr ipr sraies rm etfaeok mot eilzr fo .oesipr Sekr rm mdl mot pae casSekreilzrsraiesMdleilzr: ls paeSraie(eilzr.oeSraie) casMt: ls ea mdl=Sekr oe pae fed =(nm' 'iy,'opn' ils 'ae, ct' cmay)
  40. 40. UTILIZANDO GENERIC VIEWS serializers.py fo rs_rmwr ipr sraies rm etfaeok mot eilzr fo .oesipr Sekr rm mdl mot pae casSekreilzrsraiesMdleilzr: ls paeSraie(eilzr.oeSraie) casMt: ls ea mdl=Sekr oe pae fed =(nm' 'iy,'opn' ils 'ae, ct' cmay) views.py fo rs_rmwr ipr gnrc rm etfaeok mot eeis fo .oesipr Sekr rm mdl mot pae casSekritgnrc.itraePVe) ls paeLs(eeisLsCetAIiw: '' ' Edon qerpeet alsad plsrne,eprieqenvs npit u ersna it e aetats emt u oo plsrne sjmcdsrds aetats ea aatao. '' ' mdl=Sekr oe pae sraie_ls =Sekreilzr eilzrcas paeSraie
  41. 41. GET /SPEAKERS/ HT/. 20O TP10 0 K Alw GT PS,HA,OTOS lo: E, OT ED PIN CnetTp:apiainjo;castuf8 otn-ye plcto/sn hre=t[ { "iy:"eie, ct" Rcf" "opn" "hnr, cmay: tik" "ae:"ennoRca" nm" Frad oh, } , { "iy:"eie, ct" Rcf" "opn" "it" cmay: Vna, "ae:"iieXmns" nm" Flp iee, } , { "iy:"eie, ct" Rcf" "opn" "ND, cmay: IT" "ae:"ae, nm" Dkr" } ]
  42. 42. COMO FUNCIONA? views.py fo rs_rmwr ipr gnrc rm etfaeok mot eeis fo rs_rmwr.epneipr Rsos rm etfaeokrsos mot epne fo .oesipr Sekr rm mdl mot pae casSekritgnrc.PVe) ls paeLs(eeisAIiw: '' ' Edon qerpeet alsad plsrne,eprieqenvs npit u ersna it e aetats emt u oo plsrne sjmcdsrds aetats ea aatao. '' ' dfps(ef rqet: e otsl, eus) sraie =Sekreilzrdt=eus.AA eilzr paeSraie(aarqetDT) i sraie.svld) f eilzri_ai(: sraie.ae) eilzrsv( rtr Rsos(eilzrdt,sau=0) eun epnesraie.aa tts21 rtr Rsos(eilzrero,sau=0) eun epnesraie.rrs tts40
  43. 43. VALIDAÇÃO serializers.py fo rs_rmwr ipr sraies rm etfaeok mot eilzr fo .oesipr Sekr rm mdl mot pae casSekreilzrsraiesMdleilzr: ls paeSraie(eilzr.oeSraie) casMt: ls ea mdl=Sekr oe pae fed =(nm' 'iy,'opn' ils 'ae, ct' cmay) dfvldt_aesl,ats suc) e aiaenm(ef tr, ore: i lnatssuc]slt) <2 f e(tr[ore.pi() : riesraiesVldtoErruIfreonm esbeoe) as eilzr.aiainro('nom oe ornm' rtr ats eun tr
  44. 44. POST /SPEAKERS/ request {nm' '' 'iy:'eie,'opn' 'et} 'ae: a, ct' rcf' cmay: ts' response HT/. 40BDRQET TP10 0 A EUS Alw GT PS,HA,OTOS lo: E, OT ED PIN CnetTp:apiainjo;castuf8 otn-ye plcto/sn hre=t{ "ae:[ nm" "nom onm esbeoe Ifre oe ornm" ] }
  45. 45. GET /SPEAKERS/ HT/. 20O TP10 0 K Alw GT PS,HA,OTOS lo: E, OT ED PIN CnetTp:apiainjo;castuf8 otn-ye plcto/sn hre=t[ { "iy:"eie, ct" Rcf" "opn" "HNR, cmay: TIK" "ae:"ennoRca" nm" Frad oh, } , { "iy:"eie, ct" Rcf" "opn" "IT" cmay: VNA, "ae:"iieXmns" nm" Flp iee, } , { "iy:"eie, ct" Rcf" "opn" "ND, cmay: IT" "ae:"ae, nm" Dkr" } , ]
  46. 46. VIEWSETS
  47. 47. VIEWSET views.py fo rs_rmwr ipr vest rm etfaeok mot iwes fo .oesipr Sekr rm mdl mot pae casSekriwe(iwesMdliwe) ls paeVeStvest.oeVeSt: '' ' Edon pr vsaie eeia isâca d Sekr npit aa iulzr dtr ntnis e pae '' ' sraie_ls =UeSraie eilzrcas sreilzr qeye =Sekrojcsal) urst pae.bet.l(
  48. 48. FILIPE XIMENES github.com/filipeximenes twitter.com/xima FERNANDO ROCHA livingintheshell.com github.com/fernandogrd twitter.com/fernandogrd

×