Introduction to RESTful API Designs

Post on 14-Jun-2015

383 views 6 download

Tags:

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.

transcript

Teach a dog to REST

… yeah, that one is stolen ;D

Why dogs ?

I

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 :)

Whats an API

II

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 )

I never used one

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

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:

Okay, how does it work ?

/showmesomedogs

/getavailabledogfood

/whereisavet

/storenewdogdata

Bad API

III

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

So what makes a good API

IV

RESTful

Representational state transfer (REST)

RESTful

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

RESTful

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

Okay, how does it work ?

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

POST, GET,PUT, DELETE

CRUD

You may also know this as

CREATEREADUPDATEDELETE

Basic Rules

V

Resources

Work with Resources and identifiers.

/dogs

/dogs/{id}

Parameters

Use Parameters if you want to get very specific

&color=brown

Keep them Optional!!!

Verbs - Nouns

Verbs are BAD

Nouns are GOOD

Dog…s… ?

Singular or Plural ?Stick to it!

How Specific

things ?animals ?people ?

Version

It's always good to version your API

&v=1

- or -

/v1/dogs

Results

Always return a HTTP Number.

ex: 200,301,404

Communication

JSON ?

JSON !

( XML, ATOM, …)

Example

VI

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

Specific

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

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

Specific

Don't nest too deep!

TODOs

VII

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

Extras

VIII

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.

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)

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 :)

Questions ?

IX