63
An API your parents would be proud of!!! @chepeftw [email protected] https://www.linkedin.com/pub/jose-alfredo-alvarez-aldana/25/47b/b62 FIT Universidad Galileo, Guatemala 10/9/2014 https://db.tt/DscxrOCk 1

An API Your Parents Would Be Proud Of

Embed Size (px)

Citation preview

Page 1: An API Your Parents Would Be Proud Of

An API your parents would be proud of!!!

@[email protected]

https://www.linkedin.com/pub/jose-alfredo-alvarez-aldana/25/47b/b62FIT Universidad Galileo, Guatemala

10/9/2014 https://db.tt/DscxrOCk 1

Page 2: An API Your Parents Would Be Proud Of

Agenda

• API super summary

• Vagrant– Puppet

• API

• Symfony (PHP Framework)– FOSRestBundle

• BeerCount (act surprised)

• Angular

• Recommendations

10/9/2014 https://db.tt/DscxrOCk 2

Page 3: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 3

Page 4: An API Your Parents Would Be Proud Of

API

• “In computer programming, an application programming interface (API) specifies a software component in terms of its operations, their inputs and outputs and underlying types.” --Wikipedia

10/9/2014 https://db.tt/DscxrOCk 4

Page 5: An API Your Parents Would Be Proud Of

API

• It’s not easy … for realz :S

• Lots of theory and rules

– But we like to break the rules :P

• Open to interpretation (sometimes)

10/9/2014 https://db.tt/DscxrOCk 5

Page 6: An API Your Parents Would Be Proud Of

But first …

• Let me take a selfie?

10/9/2014 https://db.tt/DscxrOCk 6

Page 7: An API Your Parents Would Be Proud Of

But first …

• Let me take a selfie?

• Cool technologies from the block:

10/9/2014 https://db.tt/DscxrOCk 7

Page 8: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 8

Page 9: An API Your Parents Would Be Proud Of

• “Create and configure lightweight, reproducible, and portable development environments.”

• http://www.vagrantup.com/

10/9/2014 https://db.tt/DscxrOCk 9

Page 10: An API Your Parents Would Be Proud Of

• “Puppet Labs is the leader in IT automation. Our software helps sysadmins

automate configuration and management of machines and the software running on them. With our

software, businesses can make rapid, repeatable changes and automatically enforce the consistency of systems and devices, across physical and virtual machines, on prem or in the cloud. We help tens of thousands of the world’s leading companies manage millions of machines and devices. Companies like Bank of America, Cisco, Citrix, eBay, NYSE, PayPal, and salesforce.com rely on our software to deploy their own software faster, be more productive, and gain insight into infrastructure configurations and operation. Based in Portland, Oregon, Puppet Labs employs more than 300 people. We’ve raised $86 million from our investors: Cisco, Google Ventures, Kleiner Perkins Caufield & Byers, Radar Partners, Triangle Peak Partners, True Ventures and VMware.”

• http://puppetlabs.com/

10/9/2014 https://db.tt/DscxrOCk 10

Page 11: An API Your Parents Would Be Proud Of

Uses for us?

• Local environment setup

– Consistent

– Rapid

– Automate installation

– Example … today, right now, FIT

• Team Collaboration

10/9/2014 https://db.tt/DscxrOCk 11

Page 12: An API Your Parents Would Be Proud Of

Vagrant DEMO

• git clone https://github.com/chepeftw/VagrantSimpleEnvSymfony.git

– Need help with that “git” sorcery?

https://www.codeschool.com/paths/git

10/9/2014 https://db.tt/DscxrOCk 12

Page 13: An API Your Parents Would Be Proud Of

Vagrant DEMO

$ git clone https://github.com/chepeftw/VagrantSimpleEnvSymfony.git

$ cd VagrantSimpleEnvSymfony

$ vagrant up

$ vagrant ssh

$ vagrant destroy -f

10/9/2014 https://db.tt/DscxrOCk 13

Page 14: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 14

Page 15: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 15

Page 16: An API Your Parents Would Be Proud Of

API

• URL structure?

• Json?

• Errors?

• Testing?

• HTTP methods?

• Status codes?

• Documentation?

10/9/2014 https://db.tt/DscxrOCk 16

Page 17: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 17

Page 18: An API Your Parents Would Be Proud Of

API

• HTTP

– GET /drinkBeer

– HTTP/1.1 200 OK

<h1>Cheers!</h1>

http://tools.ietf.org/html/rfc2616

10/9/2014 https://db.tt/DscxrOCk 18

Page 19: An API Your Parents Would Be Proud Of

HTTP

• HTTP Request

GET /beer.html HTTP/1.1

Host: www.party.com

Accept: application/json

– /beer.html => Uniform Resource Identifier (URI)

– GET => HTTP Method

– HTTP Headers

10/9/2014 https://db.tt/DscxrOCk 19

Page 20: An API Your Parents Would Be Proud Of

HTTP

• HTTP ResponseHTTP/1.1 200 OK

Date: Mon, 23 May 2005 22:38:34 GMT

Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)

Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT

<html><body> Hello Beer </body></html>

– 200 OK => HTTP Status Code

– HTTP Headers

– Body

10/9/2014 https://db.tt/DscxrOCk 20

Page 21: An API Your Parents Would Be Proud Of

REST

• http://www.ics.uci.edu/~taylor/documents/2002-REST-TOIT.pdf

• /beers

– Collection of resources of beers.

• /beers/cabro

– Single beer resource.

10/9/2014 https://db.tt/DscxrOCk 21

Page 22: An API Your Parents Would Be Proud Of

Representation

GET /beers/cabro

{

“id” : 1,

“name” : “cabro”

}

10/9/2014 https://db.tt/DscxrOCk 22

Page 23: An API Your Parents Would Be Proud Of

Representation

GET /beers/cabro

{

“id” : 1,

“name” : “cabro”

}

10/9/2014 https://db.tt/DscxrOCk 23

Json representationOf a beer Resoruce

Page 24: An API Your Parents Would Be Proud Of

Representation

• Machine-readable

• Current state of a resource

10/9/2014 https://db.tt/DscxrOCk 24

Page 25: An API Your Parents Would Be Proud Of

REST

• Rules on how two machines can transfer the state of a resource by using representations.

10/9/2014 https://db.tt/DscxrOCk 25

Page 26: An API Your Parents Would Be Proud Of

Richardson Maturity Model

• http://martinfowler.com/articles/richardsonMaturityModel.html

10/9/2014 https://db.tt/DscxrOCk 26

Page 27: An API Your Parents Would Be Proud Of

POST

• Creating resources*.

• HTTP Rule: After creating a resource, return a 201 status code.

• HTTP Rule: After creating a resource, return a Location header.

• Response should be a representation of the resource created.

• http://tools.ietf.org/html/rfc2616• * not exactly true but more information later

10/9/2014 https://db.tt/DscxrOCk 27

Page 28: An API Your Parents Would Be Proud Of

POST - request

POST /api/beers

{

“id” : 1,

“name” : “cabro”

“type” : “lager”

}

10/9/2014 https://db.tt/DscxrOCk 28

Page 29: An API Your Parents Would Be Proud Of

POST - response

HTTP/1.1 201 Created

Content-Type: application-json

Location: /api/beers/cabro

{

“id” : 1,

“name” : “cabro”

“type” : “lager”

}

10/9/2014 https://db.tt/DscxrOCk 29

Page 30: An API Your Parents Would Be Proud Of

GET

• “Getting information”.

– Show a resource.

– List all resources.

• Retrieving a representation.

• 200 OK

10/9/2014 https://db.tt/DscxrOCk 30

Page 31: An API Your Parents Would Be Proud Of

GET- request

GET /api/beers

10/9/2014 https://db.tt/DscxrOCk 31

Page 32: An API Your Parents Would Be Proud Of

GET- response

HTTP/1.1 200 OK

Content-Type: application-json

{ “beers”: [{

“id” : 1,

“name” : “cabro”

“type” : “lager”

}

]}

10/9/2014 https://db.tt/DscxrOCk 32

Page 33: An API Your Parents Would Be Proud Of

PUT

• “Updating/Editing” resources*.

• Meh!

• 200 OK

• No need for a Location header.

10/9/2014 https://db.tt/DscxrOCk 33

Page 34: An API Your Parents Would Be Proud Of

PUT - request

PUT /api/beers/cabro

{

“id” : 1,

“name” : “cabro reserva”

“type” : “lager”

}

10/9/2014 https://db.tt/DscxrOCk 34

Page 35: An API Your Parents Would Be Proud Of

PUT - response

HTTP/1.1 200 OK

Content-Type: application-json

{

“id” : 1,

“name” : “cabro reserva”

“type” : “lager”

}

10/9/2014 https://db.tt/DscxrOCk 35

Page 36: An API Your Parents Would Be Proud Of

POST vs PUT

• Both unsafe HTTP methods (might change data).

• PUT is idempotent.

• POST is not idempotent.

10/9/2014 https://db.tt/DscxrOCk 36

Page 37: An API Your Parents Would Be Proud Of

DELETE

• Guess what it does? … Deletes a resource.

• 204 Status Code.

10/9/2014 https://db.tt/DscxrOCk 37

Page 38: An API Your Parents Would Be Proud Of

DELETE - request

DELETE /api/beers/cabro

10/9/2014 https://db.tt/DscxrOCk 38

Page 39: An API Your Parents Would Be Proud Of

GET- response

HTTP/1.1 204 No Content

10/9/2014 https://db.tt/DscxrOCk 39

Page 40: An API Your Parents Would Be Proud Of

QUICK NOTE

• HTTP Methods

– GET, POST, DELETE …

– Avoid:

• /api/beers/new

• /api/beers/delete

• /api/beers/list

• /api/beers/show

10/9/2014 https://db.tt/DscxrOCk 40

Page 41: An API Your Parents Would Be Proud Of

PATCH

• What about PUT?

– In theory, PUT should send ALL data to the server.

– It REPLACES ALL data (Bad PUT ).

• Eg. If you send just one property to “update”, in theory, it should put null in all the other properties.

• http://tools.ietf.org/html/rfc5789

• PATCH only updates the properties that are sent (Good PATCH ).

10/9/2014 https://db.tt/DscxrOCk 41

Page 42: An API Your Parents Would Be Proud Of

PUT - request

PUT /api/beers/cabro

{

“name” : “cabro reserva”

}

10/9/2014 https://db.tt/DscxrOCk 42

FAIL DARKSSS!!!!!

Page 43: An API Your Parents Would Be Proud Of

PATCH - request

PATCH /api/beers/cabro

{

“name” : “cabro reserva”

}

10/9/2014 https://db.tt/DscxrOCk 43

WIN!!!!

Page 44: An API Your Parents Would Be Proud Of

PATCH

• Ok … I lied :S

• PATCH is a set of instructions.

10/9/2014 https://db.tt/DscxrOCk 44

Page 45: An API Your Parents Would Be Proud Of

PATCH - request

PATCH /api/beers/cabro

{

{“op”: “replace”, ”path": ”name", "value": “cabro reserva”}

{“op”: “remove”, ”path": ”otherProperty”}

}

10/9/2014 https://db.tt/DscxrOCk 45

Page 46: An API Your Parents Would Be Proud Of

PATCH

• https://tools.ietf.org/html/rfc6902

• http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/

10/9/2014 https://db.tt/DscxrOCk 46

Page 47: An API Your Parents Would Be Proud Of

ERRORS

• 400.

• 422: Unprocessable Entity?

• https://tools.ietf.org/html/draft-nottingham-http-problem-07

• Content-Type: application/problem+json

10/9/2014 https://db.tt/DscxrOCk 47

Page 48: An API Your Parents Would Be Proud Of

IANA Media Types

• http://www.iana.org/assignments/media-types/media-types.xhtml

10/9/2014 https://db.tt/DscxrOCk 48

Page 49: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 49

Page 50: An API Your Parents Would Be Proud Of

• So what? Implementation?

10/9/2014 https://db.tt/DscxrOCk 50

Page 51: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 51

Page 52: An API Your Parents Would Be Proud Of

• PHP Framework– MVC*

– CRUD

– Routes

– Controllers

– Templating

– Doctrine

– Testing

– Bla bla bla bla … http://symfony.com

10/9/2014 https://db.tt/DscxrOCk 52

Page 53: An API Your Parents Would Be Proud Of

• Tutorials? Help? Something?

– http://symfony.com/doc/current/book/index.html

– http://tutorial.symblog.co.uk/

– http://code.tutsplus.com/tutorials/diving-into-symfony-2--net-32923

10/9/2014 https://db.tt/DscxrOCk 53

Page 54: An API Your Parents Would Be Proud Of

$ curl -s https://getcomposer.org/installer | php

$ php composer.phar create-project symfony/framework-standard-edition demo/ "2.5.*”

$ cd demo

$ php app/console generate:bundle

$ php app/console doctrine:generate:entity

$ php app/console doctrine:database:create

$ php app/console doctrine:schema:create

$ php app/console doctrine:generate:crud

10/9/2014 https://db.tt/DscxrOCk 54

Page 55: An API Your Parents Would Be Proud Of

• API?

– FOSRestBundle

10/9/2014 https://db.tt/DscxrOCk 55

Page 56: An API Your Parents Would Be Proud Of

DEMO!!!!

10/9/2014 https://db.tt/DscxrOCk 56

Page 57: An API Your Parents Would Be Proud Of

• Have you ever counted your friend’s beers? Or even yours? :O

10/9/2014 https://db.tt/DscxrOCk 57

Page 58: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 58

Beer Count

The next generation super awesome cool web app, with this shagadelic state of the

art API ready for Angular and mobile Apps, that will help you keep count of how many

beers your friends have at a bar.

Page 59: An API Your Parents Would Be Proud Of

$ git clone [email protected]:chepeftw/BeerCountBeta.git

10/9/2014 https://db.tt/DscxrOCk 59

Page 60: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 60

Page 61: An API Your Parents Would Be Proud Of

Angular

• MVC frontend

• Modules

• Controllers

• Services

• Data Binding

• http://campus.codeschool.com/courses/shaping-up-with-angular-js/intro

10/9/2014 https://db.tt/DscxrOCk 61

Page 62: An API Your Parents Would Be Proud Of

Recommendations

10/9/2014 https://db.tt/DscxrOCk 62

Page 63: An API Your Parents Would Be Proud Of

10/9/2014 https://db.tt/DscxrOCk 63