tdc2016poa | trilha web - json api: não reinvente a roda

19
Globalcode – Open4education JSON API: não reinvente a roda Pedro Cavalheiro Trilha Web

Upload: tdc-globalcode

Post on 10-Jan-2017

53 views

Category:

Education


2 download

TRANSCRIPT

Page 1: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

JSON API: não reinvente a rodaPedro Cavalheiro

Trilha Web

Page 2: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

Pedro CavalheiroDesenvolvedor web há 6 anos, em sua maioria trabalhando com Ruby e PHP. Entusiasta por tecnologia como um todo, adora estudar novas ferramentas, linguagens e metodologias.

Atualmente no Me Salva!.

Infos em pecavalheiro.com

Page 3: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

Did I hear API?

Page 4: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

API• Application Programming Interface

• Web Services (HTTP)• SOAP• REST

• JSON

Page 5: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

REST API• O que é importante?

• Segurança/Autenticação• Documentação• Requisitos/Complexidade• Design de requisições/respostas• +

Page 6: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

Page 7: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

bikeshed (noun)• (literally) An enclosed structure where bicycles

may be stored.

• (figuratively, technical jargon) A topic that is

attracting debate ad nauseam, out of all proportion

to its actual importance. Also bikeshedding, the

process of futile investment of time and energy in

discussion of marginal technical issues.

Page 8: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

Page 9: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

Did I hear API?

Page 10: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

Did I hear API?

Page 11: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

JSON API• O que ele define?

Just kidding!

Page 12: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

• Como um cliente deve consultar, criar e/ou modificar recursos.

• Verbos• Formato da URL/Filtros• Paginação• +

• Como o servidor deve responder a estas requisições.• Estrutura de resposta• Status Code• +

JSON API• O que ele define?

Page 13: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

HTTP/1.1 200 OKContent-Type: application/vnd.api+json

{ "data": [{ "type": "articles", "id": "1", "attributes": { "title": "JSON API paints my bikeshed!", "body": "The shortest article. Ever.", "created": "2015-05-22T14:56:29.000Z", "updated": "2015-05-22T14:56:28.000Z" }, "relationships": { "author": { "data": {"id": "42", "type": "people"} } } }], "included": [ { "type": "people", "id": "42", "attributes": { "name": "John", "age": 80, "gender": "male" } } ]}

GET /articles?include=author HTTP/1.1

Page 14: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

HTTP/1.1 200 OKContent-Type: application/vnd.api+json

{ "meta": { "total-pages": 13 }, "data": [ { "type": "articles", "id": "3", "attributes": { "title": "JSON API paints my bikeshed!", "body": "The shortest article. Ever.", "created": "2015-05-22T14:56:29.000Z", "updated": "2015-05-22T14:56:28.000Z" } } ], "links": { "self": "http://example.com/articles?page[number]=3&page[size]=1", "first": "http://example.com/articles?page[number]=1&page[size]=1", "prev": "http://example.com/articles?page[number]=2&page[size]=1", "next": "http://example.com/articles?page[number]=4&page[size]=1", "last": "http://example.com/articles?page[number]=13&page[size]=1" }}

GET /articles?page[number]=3&page[size]=1 HTTP/1.1

Page 15: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

HTTP/1.1 422 Unprocessable EntityContent-Type: application/vnd.api+json

{ "errors": [ { "status": "422", "source": { "pointer": "/data/attributes/first-name" }, "title": "Invalid Attribute", "detail": "First name must contain at least three characters." } ]}

Page 16: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

JSON API• Implementação

Page 17: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

• Client libraries• JavaScript, iOS, Ruby, PHP, Dart, Perl, Java, Android, R, Elm, .NET

• Server libraries• PHP, Node.js, Ruby, Python, Go, .NET, Java, Scala, Elixir, Haskell,

Perl, Vala

JSON API• Implementação - jsonapi.org/implementations

Page 18: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

Resumão• Calma! Ajuste uma parte de cada vez

• JSON API não é “lei”, existem outros padrões:

HAL, Siren, JSON-LD, Collection+JSON, etc

• Adote padrões, eles facilitam nossa vida :)

Page 19: TDC2016POA | Trilha Web - JSON API: não reinvente a roda

Globalcode – Open4education

Dúvidas?

jsonapi.org

pecavalheiro.com

OBRIGADO!