Cliente API REST con LungoJS

72
CLIENTE API REST CON LUNGOJS ADOLFO SANZ DE DIEGO OCTUBRE 2013

description

Creando un cliente API RESTful con lungojs.

Transcript of Cliente API REST con LungoJS

Page 1: Cliente API REST con LungoJS

CLIENTEAPIRESTCONLUNGOJS

ADOLFOSANZDEDIEGOOCTUBRE2013

Page 2: Cliente API REST con LungoJS

1ACERCADE

Page 3: Cliente API REST con LungoJS

1.1ELGULElGULeselGrupodeUsuariosdeLinuxdelaUC3M.Grupodepersonasconinquietudesentornoalainformática.ConlaideacomúndelautilizaciónypromocióndelSoftwareLibre.Quedamosdevezencuandoyorganizamosactividadessobretodoesto.Elpuntodeunióneslalistadecorreoqueestáabiertaatodoelmundo.

Page 4: Cliente API REST con LungoJS

1.2¿DÓNDEENCONTRARNOS?Twitter:Lista:Ftp:Web:Podcast:Blog:Linkedin:

http://twitter.com/[email protected]

ftp://ftp.gul.uc3m.eshttp://www.gul.uc3m.es

http://holamundo.gul.es/http://planeta.gul.uc3m.es/

http://www.linkedin.com/groups?gid=3451836

Page 5: Cliente API REST con LungoJS

1.3ADOLFOSANZDEDIEGOAntiguoprogramadorwebJEEHoyendía:ProfesordeFPdeinformática:Hardware,SistemasOperativosRedes,Programación

FormadorFreelance:Java,AndroidJavaScript,jQueryJSF,Spring,HibernateGroovy&Grails

Megustaprogramar

Page 6: Cliente API REST con LungoJS

1.4HACKALOVER

ParalosamantesdeloshackathonesMeetup:

Twitter:Blog:LinkedIn:

YouTube:

http://www.meetup.com/Hackathon-Lovers/

http://twitter.com/HackathonLovershttp://hackathonlovers.tumblr.com/

http://www.linkedin.com/groups/Hackathon-Lovers-6510465

http://www.youtube.com/channel/UCRwSe7jK-y62BMvIiNBV1qw

Page 7: Cliente API REST con LungoJS

1.5TWEETSSENTIMENT

Esunanalizadordetweetsqueextraeinformaciónsemánticaparaconocersielsentimientogeneraldelostweetsdeundeterminadotemaespositivoonegativo.Web:Twitter:

http://tweetssentiment.com/http://twitter.com/TweetsSentiment

Page 8: Cliente API REST con LungoJS

1.6¿DONDEENCONTRARME?Minick:asanzdiegoAboutMe:GitHub:Twitter:Blog:LinkedIn:Google+:

http://about.me/asanzdiegohttp://github.com/asanzdiegohttp://twitter.com/asanzdiego

http://asanzdiego.blogspot.com.eshttp://www.linkedin.com/in/asanzdiego

http://plus.google.com/+AdolfoSanzDeDiego

Page 9: Cliente API REST con LungoJS

1.7CRÉDITOSEstastransparenciasestánhechascon:https://github.com/asanzdiego/markdownslides

Page 10: Cliente API REST con LungoJS

1.8LICENCIAEstastransparenciasestánbajounalicencia:

Elcódigodelosprogramasestánbajounalicencia:

CreativeCommonsReconocimiento-CompartirIgual3.0

GPL3.0

Page 11: Cliente API REST con LungoJS

1.9FUENTESTransparencias

Código

SlideShareDeckSlidesRevealSlidesPlainHTML

https://github.com/asanzdiego/curso-api-restful-lungojs-client-2013/tree/master/src

Page 12: Cliente API REST con LungoJS

2APIS¿PARAQUÉ?

Page 13: Cliente API REST con LungoJS

2.1APLICACIÓNESTÁNDAR

Page 14: Cliente API REST con LungoJS

2.2INTRODUCIMOSAPI

Page 15: Cliente API REST con LungoJS

2.3SEPARACIÓNROLES

Page 16: Cliente API REST con LungoJS

2.4¿YAHORAQUÉ?

Page 17: Cliente API REST con LungoJS

2.5SERVICIOSEXTERNOS

Page 18: Cliente API REST con LungoJS

2.6APPSCLIENTES

Page 19: Cliente API REST con LungoJS

2.7APPSDESERVICIOS

Page 20: Cliente API REST con LungoJS

2.8APPSMIXTAS

Page 21: Cliente API REST con LungoJS

2.9PLATAFORMA

Page 22: Cliente API REST con LungoJS

2.10¿QUIENEXPONEAPIS?

Page 23: Cliente API REST con LungoJS

2.11¿QUIENEXPONEAPIS?

Page 24: Cliente API REST con LungoJS

2.12¿QUIENEXPONEAPIS?

Page 25: Cliente API REST con LungoJS

2.13EXPONLASTÚ

Page 26: Cliente API REST con LungoJS

2.14EXPONLASTÚ

Page 27: Cliente API REST con LungoJS

2.15EXPONLASTÚ

Page 28: Cliente API REST con LungoJS

3APISRESTFUL

Page 29: Cliente API REST con LungoJS

3.1¿QUÉESREST?REST(RepresentationalStateTransfer)esunatécnicadearquitecturadesoftwareparasistemashipermediadistribuidoscomolaWorldWideWeb.EnRESTunaURL(UniformResourceLocator)representaunrecurso.SepuedeaccederalrecursoomodificarlomediantelosmétodosdelprotocoloHTTP:

GET,POST,PUT,DELETE

Page 30: Cliente API REST con LungoJS

3.2EJEMPLOAPIhttp://myhost.com/talkGET>Devuelvetodaslascharlas.POST>Crearunanuevacharla.

http://myhost.com/talk/123GET>Devuelvelacharlaconid=123PUT>Actualizalacharlaconid=123DELETE>Borralacharlaconid=123

Page 31: Cliente API REST con LungoJS

3.3MANEJODEERRORESSepuedenutilizarloserroresdelprotocoloHTTP:200Successful201Created202Accepted301MovedPermanently400BadRequest401Unauthorised402PaymentRequired403Forbidden404NotFound405MethodNotAllowed500InternalServerError501NotImplemented

Page 32: Cliente API REST con LungoJS

3.4¿PORQUÉREST?Esmássencillo(tantolaAPIcomolaimplementación).Esmásrápido(peticionesmáslijerasquesepuedencachear).Esmultiformato(HTML,XML,JSON,etc.).SecomplementamuybienconAJAX.

Page 33: Cliente API REST con LungoJS

3.5RESTVSRESTFULRESTserefiereauntipodearquitecturadesoftwareSeutilizacomonombreSeutilizacomoporejemplo:success=éxito.

SiunserviciowebesRESTfulindicaqueimplementadichaarquitectura.SeutilizacomoadjetivoSeutilizacomoporejemplo:successful=éxitoso).

Page 34: Cliente API REST con LungoJS

3.6RESTVSRESTFULAveceselfulseconfundeconfull=completo.YserefierealosservicioswebRESTfull

YserefierealosservicioswebREST(sinelfull)

AquellosqueimplementanunaAPIcontodoslosmétodosdelprotócoloHTTP.

AquellosqueNOimplementanunaAPIcontodoslosmétodosdelprotócoloHTTP.

Page 35: Cliente API REST con LungoJS

4CLIENTESMULTIDISPOSITIVO

Page 36: Cliente API REST con LungoJS

4.1DISTINTOSDISPOSITIVOS

Page 37: Cliente API REST con LungoJS

4.2DISTINTOSOS

Page 38: Cliente API REST con LungoJS

4.3DISTINTOSOS

Page 39: Cliente API REST con LungoJS

4.4DISTINTOSNAVEGADORES

Page 40: Cliente API REST con LungoJS

4.5¡¡¡SOCORRO!!!

Page 41: Cliente API REST con LungoJS

4.6ALRESCATE

Page 42: Cliente API REST con LungoJS

4.7GRACEFULDEGRADATION

Page 43: Cliente API REST con LungoJS

4.8GRACEFULDEGRADATION

Page 44: Cliente API REST con LungoJS

4.9PROGRESSIVEENHANCEMENT

Page 45: Cliente API REST con LungoJS

4.10PHONEGAP

Page 46: Cliente API REST con LungoJS

5LUNGO.JS

Page 47: Cliente API REST con LungoJS

5.1COMPETIDORESjQTouch:SenchaTouch:

jQueryMobile:hammer.js:

http://jqtjs.com/

http://www.sencha.com/products/touch/http://jquerymobile.com/

http://eightmedia.github.io/hammer.js/

Page 48: Cliente API REST con LungoJS

5.2¿PORQUÉLUNGO?

Page 49: Cliente API REST con LungoJS

5.3¿LICENCIAGPL?

Page 50: Cliente API REST con LungoJS

5.4¿ESPAÑOL?

Page 51: Cliente API REST con LungoJS

5.5PUESNO

Page 52: Cliente API REST con LungoJS

5.6MEGUSTA

Page 53: Cliente API REST con LungoJS

5.7ABURRIDO

Page 54: Cliente API REST con LungoJS

6CÓDIGO

Page 55: Cliente API REST con LungoJS

6.1APLAUSOS

Page 56: Cliente API REST con LungoJS

6.2DIRECTORIOSPuedesusarlaestructuradedirectoriosquequieras.Yoheusadoesta:html:losHTMLconunacarpetaparacadaentidaddeldominojs:losJSconunacarpetaparacadaentidaddeldominolib:carpetaconlasdependenciasaotrosproyectosLungodependedeQuoqueesunalibreríadelosmismosauotresde'tipo'jQueryenfocadaamóviles

Page 57: Cliente API REST con LungoJS

6.3JS/UTIL.JS//Searh$$('document').ready(function(){Lungo.dom('input[type=search]').on('keyup',...);});

//ServerURLvarutil_server_url="http://localhost:3000";

//ErrorNotificationvarutil_errorNotification=function(message,error){Lungo.Notification.error(message,"","warning-sign",2);};

//MétodosRESTvarutil_ajaxGet=function(url,data,callback){$$.get(util_server_url+url,data,...,'json');};varutil_ajaxPost=function(url,data,callback){...};varutil_ajaxPut=function(url,data,callback){...};varutil_ajaxDelete=function(url,data,callback){...};

Page 58: Cliente API REST con LungoJS

6.4HTML/TALK/TALK-ADD.HTML<bodyclass="app"><sectionid="main"data-transition=""><headerdata-title="AddTalk"class="extended"></header><footer><nav><ahref="talk-list.html"data-icon="list"></a><ahref="talk-add.html"data-icon="plus"class="active"></a></nav></footer><articleid="main-article"class="activelist"><divclass="form"><fieldset><label>NAME:</label><inputtype="text"id="talkName"/></fieldset>...</div><div><ahref="#addTalk"id="addTalk"class="button">Add</a><ahref="talk-list.html"class="buttoncancel">Cancel</a></div></article></section></body>

Page 59: Cliente API REST con LungoJS

6.5HTML/TALK/TALK-ADD.HTML

Page 60: Cliente API REST con LungoJS

6.6JS/TALK/TALK-ADD.JS$$('#addTalk').tap(function(){

vardata={talkName:$$("#talkName").val(),talkDate:util_stringToDate($$("#talkDate").val()),talkSpeaker:$$("#talkSpeaker").val(),talkSpeakerMail:$$("#talkSpeakerMail").val(),talkPoints:$$("#talkPoints").val()};

//senddatatoserverutil_ajaxPost('/talk',data,function(json){if(!json||json.error){util_errorNotification('ERRORaddingtalk',json.error);}else{util_successNotification('Talksaved',function(){window.location.replace('talk-list.html');});}});});

Page 61: Cliente API REST con LungoJS

6.7HTML/TALK/TALK-EDIT.HTML<bodyclass="app"><sectionid="main"data-transition=""><headerdata-title="EditTalk"class="extended"></header><navdata-control="groupbar"><ahref="#editTalkArticle"...>Edit</a><ahref="#deleteTalkArticle"...>Delete</a></nav><footer>...</footer><articleid="editTalkArticle"class="listindentedscroll">...</article><articleid="deleteTalkArticle"class="listindentedscroll">...</article></section></body>

Page 62: Cliente API REST con LungoJS

6.8HTML/TALK/TALK-EDIT.HTML

Page 63: Cliente API REST con LungoJS

6.9HTML/TALK/TALK-EDIT.HTML

Page 64: Cliente API REST con LungoJS

6.10JS/TALK/TALK-GET.JS$$('document').ready(function(){

vartalkId=util_urlParams["talkId"];

//getdatafromserverutil_ajaxGet('/talk/'+talkId,{},function(json){if(!json||json.error){util_errorNotification('ERRORretrievingtalk',json.error);}else{vartalk=json;console.log('Talkretrieved');drawTalk(talk);}});

//drawdatavardrawTalk=function(talk){$$("#talkName").val(talk.talkName);...};});

Page 65: Cliente API REST con LungoJS

6.11JS/TALK/TALK-UPDATE.JS$$('#saveTalk').tap(function(){

vartalkId=util_urlParams["talkId"];

vardata={talkName:$$("#talkName").val(),...};

//senddatatoserverutil_ajaxPut('/talk/'+talkId,data,function(json){if(!json||json.error){util_errorNotification('ERRORsavingtalk',json.error);}else{util_successNotification('Talksaved',function(){window.location.replace('talk-list.html');});}});});

Page 66: Cliente API REST con LungoJS

6.12JS/TALK/TALK-DELETE.JS$$('#deleteTalk').tap(function(){

vartalkId=util_urlParams["talkId"];

//senddatatoserverutil_ajaxDelete('/talk/'+talkId,{},function(json){if(!json||json.error){util_errorNotification('ERRORdeletingtalk',json.error);}else{util_successNotification('Talkdeleted',function(){window.location.replace('talk-list.html');});}});});

Page 67: Cliente API REST con LungoJS

6.13HTML/TALK/TALK-LIST.HTML<bodyclass="app"><sectionid="main"data-transition=""><headerdata-title="TalksList"class="extended"></header><footer><nav><ahref="talk-list.html"data-icon="list"class="active"></a><ahref="talk-add.html"data-icon="plus"></a></nav></footer><articleid="main-article"class="activelistindentedscroll"><divclass="form"><fieldsetdata-icon="search"><inputtype="search"placeholder="Search..."></fieldset></div><ulid="talks"></ul></article></section></body>

Page 68: Cliente API REST con LungoJS

6.14HTML/TALK/TALK-LIST.HTML

Page 69: Cliente API REST con LungoJS

6.15HTML/TALK/TALK-LIST.HTML

Page 70: Cliente API REST con LungoJS

6.16JS/TALK/TALK-LIST.JS$$('document').ready(function(){

//getdatafromserverutil_ajaxGet('/talk',{},function(json){if(!json||json.error){util_errorNotification('ERRORretrievingtalks',json.error);}else{drawTalks(json);}});

//drawdatavardrawTalks=function(talks){for(vari=0;i<talks.length;i++){vartalk=talks[i];$$("#talks").append('<lidata-action="search"class="selectable">'+'<ahref="talk-edit.html?talkId='+talk._id+'">'+'<strong>'+talk.talkName+'</strong></a></li>');}};});

Page 71: Cliente API REST con LungoJS

7DEMO

Page 72: Cliente API REST con LungoJS

8¿ALGUNAPREGUNTA?