Build REST APIs like a Jedi with Symfony2

31
Build REST APIs like a Jedi @AlmogBaku

Transcript of Build REST APIs like a Jedi with Symfony2

Build REST APIs like a Jedi@AlmogBaku

Who are you?

@AlmogBaku nice to meet ya`

1. Entrepreneur

2. Co-Founder & CTO @ Rimoto

3. Developer for 12 years

4. GitHub addicted.

5. Blog about entrepreneurship and development:

www.AlmogBaku.com

What are we going to talk about?

● What tha’ heck is REST?

● Differences between SOAP and REST

● Authentication methods

● Symfony2 and REST

Disclaimer

You wanna know more? Google it!

What tha’ heck is REST?

Representational State Transfer

Watttt??

What is API?

API is a layer that connects two applications.

Application Programing Interface

What is API?

API is actually a common language, that

both of the parties knows.

REST

REST is a Client-Server API

Let’s talk about history

The old days: web services

A Web service is a method of communication between two electronic devices over a network.

Created by and for enterprises.

Makes data available as services (verb + noun), for example “getAuthor” or “PayInvoice”:

GetAuthor(183);

The old days: web services

Request:

POST /InStock HTTP/1.1Host: www.example.orgContent-Type: application/soap+xml; charset=utf-8Content-Length: nnn

<?xml version="1.0"?><soap:Envelopexmlns:soap="http://www.w3.org/2001/12/soap-envelope"soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/author"> <m:GetAuthor> <m:Id>183</m:Id> </m:GetAuthor></soap:Body>

</soap:Envelope>

The old days: web services

Response:

HTTP/1.1 200 OKContent-Type: application/soap+xml; charset=utf-8Content-Length: nnn

<?xml version="1.0"?><soap:Envelopexmlns:soap="http://www.w3.org/2001/12/soap-envelope"soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/author"> <m:GetAuthor> <m:Id>183</m:Id> <m:Name>J.R.R. Tolkien</m:Name> <m:Birthday>1-3-1892</m:Birthday> </m:GetAuthor></soap:Body>

</soap:Envelope>

The thing is…It just sucks

REST

REST is just the regular way the internet works!

GET http://google.com/

REST

REST is just the regular way the internet works!

GET http://google.com/RESPONSE 200 OK

REST protocol features

1. Follows the philosophy of Open Web

2. Easy to use (common standard)

3. Based on the HTTP protocol

4. Uses http status codes

5. Resource oriented

6. Response can be describe in JSON too!

7. Cacheable

8. Stateless

REST

REST is about resources, not about functions.

Book store api:

1. /api/authors/2. /api/authors/:authorId/3. /api/authors/:authorId/books/4. /api/authors/:authorId/books/:bookId5. /api/authors/:authorId/books/:bookId/reviews6. /api/authors/:authorId/books/:bookId/reviews/:reviewId

REST

REST is about resources, not about functions.

Book store api:

1. /api/authors/2. /api/authors/:authorId/3. /api/authors/:authorId/books/4. /api/authors/:authorId/books/:bookId5. /api/authors/:authorId/books/:bookId/reviews6. /api/authors/:authorId/books/:bookId/reviews/:reviewId

REST

GET /api/authors/

[ { "id": 7, "name": "Douglas Adams", "birthday": "3-11-1952" }, { "id": 183, "name": "J.R.R. Tolkien", "birthday": "1-3-1892" }]

REST

REST is about resources, not about functions.

Book store api:

1. /api/authors/2. /api/authors/:authorId/3. /api/authors/:authorId/books/4. /api/authors/:authorId/books/:bookId5. /api/authors/:authorId/books/:bookId/reviews6. /api/authors/:authorId/books/:bookId/reviews/:reviewId

REST

GET /api/authors/187/

{ "id": 183, "name": "J.R.R. Tolkien", "full_name": "John Ronald Reuel Tolkien", "birthday": "1-3-1892", "genre": "Fantasy"}

REST

The same URIs can do many different actions...We can request web pages in one of the following methods:

1. GET - request information about resource

2. POST - create new resource

3. PUT - update resource

4. DELETE - delete resource

5. HEAD - get information only with headers (eg. if resource exists)

6. OPTIONS - list of available methods to the resource (like --help)

REST

Errors are simple http errors

200 - OK

201 - Resource created

404 - Not found

401 - Unauthorized

500 - Server Error

Etc.

REST

REST is Stateless

- You can’t use cookies

- You need to pass your identification in every request- Basic access authentication (username/password)

- Tokenized authentication (like OAuth, JWT, etc)

GET /users/me?access_token=ftjhi89uh5982hbrvt92vgt9qvhg2r0219

REST API+

Symfony

1. Popular PHP framework

2. Very mature (since 2004)

3. Very Object Oriented architecture

4. Designed for high performance

5. Leading the PHP world, any used in many

other popular projects

Demo time

Be creative,and create your own API

Questions?

Thanks.