36
Teach a dog to REST … yeah, that one is stolen ;D

Introduction to RESTful API Designs

Embed Size (px)

DESCRIPTION

This Presentation makes use of the well know "Teach a dog to REST" ideology. It's an easy and convenient way to learn the basic patterns and ideas behind a good API.

Citation preview

Page 1: Introduction to RESTful API Designs

Teach a dog to REST

… yeah, that one is stolen ;D

Page 2: Introduction to RESTful API Designs

Why dogs ?

I

Page 3: Introduction to RESTful API Designs

The world is hugeA puppies World is huge.

You have other dogs, cats, people, places, vets, colours and much more.

Easy to connect with and understand basic concepts in an fluffy way :)

Page 4: Introduction to RESTful API Designs

Whats an API

II

Page 5: Introduction to RESTful API Designs

Application programming interface

It's an convenient way to interact with an Application from the outside. ( Earlier we had special Protocols like SOAP and co )

Page 6: Introduction to RESTful API Designs

I never used one

The chance you already used an API without knowing is pretty darn high. ( Apps! Facebook, Twitter, you-name-it)

Page 7: Introduction to RESTful API Designs

Okay, how does it work ?

It's a predefined way to POST or GET Content from an special resource. Let's look at some examples:

Page 8: Introduction to RESTful API Designs

Okay, how does it work ?

/showmesomedogs

/getavailabledogfood

/whereisavet

/storenewdogdata

Page 9: Introduction to RESTful API Designs

Bad API

III

Page 10: Introduction to RESTful API Designs

/store-new-dog/get-all-dogs/show-me-dog/{id}/get-all-dogs-with-color/{color} /save-new-owner /locations /vet/{id}

Page 11: Introduction to RESTful API Designs

So what makes a good API

IV

Page 12: Introduction to RESTful API Designs

RESTful

Representational state transfer (REST)

Page 13: Introduction to RESTful API Designs

RESTful

This means that there is a strict separation between Client and Server and the communication itself should be stateless.

Page 14: Introduction to RESTful API Designs

RESTful

This also means every request should work regardless of what you did last.

Page 15: Introduction to RESTful API Designs

Okay, how does it work ?

So basically there are URLs you talk to with a HTTP Verb

POST, GET,PUT, DELETE

Page 16: Introduction to RESTful API Designs

CRUD

You may also know this as

CREATEREADUPDATEDELETE

Page 17: Introduction to RESTful API Designs

Basic Rules

V

Page 18: Introduction to RESTful API Designs

Resources

Work with Resources and identifiers.

/dogs

/dogs/{id}

Page 19: Introduction to RESTful API Designs

Parameters

Use Parameters if you want to get very specific

&color=brown

Keep them Optional!!!

Page 20: Introduction to RESTful API Designs

Verbs - Nouns

Verbs are BAD

Nouns are GOOD

Page 21: Introduction to RESTful API Designs

Dog…s… ?

Singular or Plural ?Stick to it!

Page 22: Introduction to RESTful API Designs

How Specific

things ?animals ?people ?

Page 23: Introduction to RESTful API Designs

Version

It's always good to version your API

&v=1

- or -

/v1/dogs

Page 24: Introduction to RESTful API Designs

Results

Always return a HTTP Number.

ex: 200,301,404

Page 25: Introduction to RESTful API Designs

Communication

JSON ?

JSON !

( XML, ATOM, …)

Page 26: Introduction to RESTful API Designs

Example

VI

Page 27: Introduction to RESTful API Designs

CRUD

GET /dogs — return ALL dogs

POST /dogs — create a NEW dog and return ID

GET /dogs/{id} — return dog with this ID

PUT /dogs/{id} — update this dog

DELETE /dogs/{id} — delete this dog

Page 28: Introduction to RESTful API Designs

Specific

GET /dogs/{id}/owners — return the owners of the dog

GET /owners/{id}/dogs — return the dog(s) of the owners

Page 29: Introduction to RESTful API Designs

Specific

Don't nest too deep!

Page 30: Introduction to RESTful API Designs

TODOs

VII

Page 31: Introduction to RESTful API Designs

It's TODO time againGet into Groups of four (!?)

Describe your Project briefly and the goal of the API

Design your Projects API ( Parameters and stuff )

60 min time

10 min Presentation each group

5 min Feedback

Page 32: Introduction to RESTful API Designs

Extras

VIII

Page 33: Introduction to RESTful API Designs

Dos

Use existing Frameworks and Tools to create your APIs.

Define some Ground-rules for your API Design as early as possible to stay consistent from the beginning.

Having APIs in Mind for Backend-development also helps in using Frameworks like AngularJS and co.

Page 34: Introduction to RESTful API Designs

Donts

Don't reinvent the Wheel.

Orient on best practice examples because a lot of people put a lot of thought in those already.

Don't reinvent the Wheel. (even if its tempting)

Page 35: Introduction to RESTful API Designs

REST in Peace

REST-API's and JSON are the closest thing we have to a common language.

Use this tools to create awsm stuff and make a developers life as easy as possible :)

Page 36: Introduction to RESTful API Designs

Questions ?

IX