68
Mobile API Design & Techniques. Fred Brunel CTO Wednesday, 29 February, 12

Mobile API Design Techniques

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Mobile API Design Techniques

Mobile APIDesign & Techniques.

Fred BrunelCTO

Wednesday, 29 February, 12

Page 2: Mobile API Design Techniques

Wednesday, 29 February, 12

Page 3: Mobile API Design Techniques

Wednesday, 29 February, 12

Page 4: Mobile API Design Techniques

Wednesday, 29 February, 12

Page 5: Mobile API Design Techniques

Why?

Wednesday, 29 February, 12

Page 6: Mobile API Design Techniques

Though for CPU powerThough for bandwidthLazy designed.Too close to database.

Wednesday, 29 February, 12

Page 7: Mobile API Design Techniques

A mobile device isLow poweredLow bandwidthRuns on battery!

Wednesday, 29 February, 12

Page 8: Mobile API Design Techniques

A the network is the weak link.

Wednesday, 29 February, 12

Page 9: Mobile API Design Techniques

Network conditions change in real-time.

Wednesday, 29 February, 12

Page 10: Mobile API Design Techniques

We want to keep the best user experience at all time.Nobody wants an unresponsive app.

Wednesday, 29 February, 12

Page 11: Mobile API Design Techniques

The features of an API has a huge impact on performances.

Wednesday, 29 February, 12

Page 12: Mobile API Design Techniques

An API is a contract that dictates what can or cannot be done (directly).

Wednesday, 29 February, 12

Page 13: Mobile API Design Techniques

When the API is too lazy, or incomplete; the burden is put on the mobile app.

Wednesday, 29 February, 12

Page 14: Mobile API Design Techniques

Any workaround put a stress on the mobile app to use too much network.

Wednesday, 29 February, 12

Page 15: Mobile API Design Techniques

API = User Interface.

Should be simple and get the job done. Fast.

Wednesday, 29 February, 12

Page 16: Mobile API Design Techniques

Landlord Report.

Wednesday, 29 February, 12

Page 17: Mobile API Design Techniques

Simple

Standard CompleteWednesday, 29 February, 12

Page 18: Mobile API Design Techniques

Simple

Standard CompleteSOAP

XML-RPC

WS-*

Pure RESTWednesday, 29 February, 12

Page 19: Mobile API Design Techniques

Simple

CompleteWednesday, 29 February, 12

Page 20: Mobile API Design Techniques

Trust the OSI model.Works everywhere.And it’s plenty enough.

http://en.wikipedia.org/wiki/OSI_model

Wednesday, 29 February, 12

Page 21: Mobile API Design Techniques

REST-ish API + JSON

Pure REST is a nice to have but not a goal.

Wednesday, 29 February, 12

Page 22: Mobile API Design Techniques

GET/POST + Action + Params is fine.

PUT/DELETE are nice to have.

Wednesday, 29 February, 12

Page 23: Mobile API Design Techniques

Twitter is also REST-ish

POST statuses/createPOST statuses/destroy/:idPOST statuses/update

Wednesday, 29 February, 12

Page 24: Mobile API Design Techniques

REST put an emphasis on actions applied to resources; but the issue is the representation.

Wednesday, 29 February, 12

Page 25: Mobile API Design Techniques

Simplify the life of the implementor.Be pragmatic.

Wednesday, 29 February, 12

Page 26: Mobile API Design Techniques

When designing your API payloads, pay attention to consistency and completeness.

Wednesday, 29 February, 12

Page 27: Mobile API Design Techniques

Consistency means developer know what to expect.Principle of least astonishment.

Wednesday, 29 February, 12

Page 28: Mobile API Design Techniques

Completeness means less roundtrips.

Wednesday, 29 February, 12

Page 29: Mobile API Design Techniques

HTTP latency on 3G

~ 1 second.

Every request count.

Wednesday, 29 February, 12

Page 30: Mobile API Design Techniques

API is NOT a CRUD interface to your SQL database.

It’s a facade.

Wednesday, 29 February, 12

Page 31: Mobile API Design Techniques

DatabaseFacadeAPIApp

DataRepresentation Raw DataDisplay

Wednesday, 29 February, 12

Page 32: Mobile API Design Techniques

The facade answer to high-level questions.

Think services, not objects and methods.

Wednesday, 29 February, 12

Page 33: Mobile API Design Techniques

So, how do we start from here?

Wednesday, 29 February, 12

Page 34: Mobile API Design Techniques

Most of the time, a mobile API will be use to get information to be displayed on a screen.

Wednesday, 29 February, 12

Page 35: Mobile API Design Techniques

Reverse Design.

Start from the UINot the data

Wednesday, 29 February, 12

Page 36: Mobile API Design Techniques

1. Think screens2.Entities to display3.Design entity models4.Design services

Wednesday, 29 February, 12

Page 37: Mobile API Design Techniques

Wednesday, 29 February, 12

Page 38: Mobile API Design Techniques

IDTitleTownCountryRatingThumbnail URLGeocodeWebsiteEmailDescription

Wednesday, 29 February, 12

Page 39: Mobile API Design Techniques

Then, format the representation to be as efficient as possible.

Wednesday, 29 February, 12

Page 40: Mobile API Design Techniques

Each JSON entity should have the same consistent representation.

Wednesday, 29 February, 12

Page 41: Mobile API Design Techniques

"person": { "id": 1234, "name": "Fred", "lastname": "Brunel", "company": "WhereCloud"}

Wednesday, 29 February, 12

Page 42: Mobile API Design Techniques

"book": { "name": "Steve Jobs", "author": "Walter Isaacson", "lenders" = [{ "person_id": 1234, "person_name": "Fred", "person_lastname": "Brunel" }]}

BADWednesday, 29 February, 12

Page 43: Mobile API Design Techniques

"book": { "name": "Steve Jobs", "author": "Walter Isaacson", "lenders" = [{ "id": 1234, "name": "Fred", "lastname": "Brunel" }]}

GOODWednesday, 29 February, 12

Page 44: Mobile API Design Techniques

..."user_mentions": [{ "id": 22548447, "id_str": "22548447", "screen_name": "rno", "name": "Arnaud Meunier", "indices": [0, 4]]}...

Wednesday, 29 February, 12

Page 45: Mobile API Design Techniques

Pick the right granularity.

Denormalize!

Wednesday, 29 February, 12

Page 46: Mobile API Design Techniques

"result": { ... "categories" = [{ "id": 2 }], "images": [{ "id": 317171 }], "tags": [{ "id": 555 }] ...}

Wednesday, 29 February, 12

Page 47: Mobile API Design Techniques

"result": { ... "categories": [{ "id": 2 "name" = "food" }], "images" = [{ "id": 317171 "url": "http://image.com/a.png" }], ...}

Wednesday, 29 February, 12

Page 48: Mobile API Design Techniques

Denormalize the most common fields.Avoid unnecessary roundtrips.

Wednesday, 29 February, 12

Page 49: Mobile API Design Techniques

Don’t make the app connects to 10 3rd-party systems.Aggregate on the backend.

Wednesday, 29 February, 12

Page 50: Mobile API Design Techniques

The backend has the power, bandwidth and knowledge.Use it!

Wednesday, 29 February, 12

Page 51: Mobile API Design Techniques

Make it fast!

Some good techniques to be aware of.

Wednesday, 29 February, 12

Page 52: Mobile API Design Techniques

JSON is fast to parse, but still, compress everything.

Wednesday, 29 February, 12

Page 53: Mobile API Design Techniques

Use Cache-Control on every response that can be cached.

Wednesday, 29 February, 12

Page 54: Mobile API Design Techniques

Partial Responses & Partial Updates

Let the client decides what to get/update.

Wednesday, 29 February, 12

Page 55: Mobile API Design Techniques

GEThttp://www.google.com/calendar/feeds/[email protected]/private/full?fields=entry(title,gd:when)

Wednesday, 29 February, 12

Page 56: Mobile API Design Techniques

PATCH /myfeed/1/1/Content-Type: application/xml

<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google...' gd:fields='description'> <title>New title</title></entry>

Wednesday, 29 February, 12

Page 57: Mobile API Design Techniques

Batch Requests

Send multiple operations, get one answer.

Wednesday, 29 February, 12

Page 58: Mobile API Design Techniques

Persistent Connections.

Keep a connection nailed up.

Wednesday, 29 February, 12

Page 59: Mobile API Design Techniques

“If you’re serious about network, you should make your own protocol.”—Fake Alan Kay.

Wednesday, 29 February, 12

Page 60: Mobile API Design Techniques

The fabric of the Internet is TCP/IP, not HTTP.

Wednesday, 29 February, 12

Page 61: Mobile API Design Techniques

Make your own Binary Protocol.

Lot faster than text + compression. Sorry!

Wednesday, 29 February, 12

Page 62: Mobile API Design Techniques

Message-based API

Custom TLVMessagePackProtocolBuffers

Wednesday, 29 February, 12

Page 63: Mobile API Design Techniques

TAG LENGTH VALUE

32 bits16 bits n bits

TLV TLV TLV TLV TLV

TLV TLV TLV TLV TLV

messages streaming

a message

Wednesday, 29 February, 12

Page 64: Mobile API Design Techniques

message Person { required string name = 1; required int32 id = 2; optional string email = 3;

enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; }

message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; }

repeated PhoneNumber phone = 4;}

Wednesday, 29 February, 12

Page 65: Mobile API Design Techniques

Person person;person.set_name("John Doe");person.set_id(1234);person.set_email("[email protected]");fstream output("myfile", ios::out | ios::binary);person.SerializeToOstream(&output);

fstream input("myfile", ios::in | ios::binary);Person person;person.ParseFromIstream(&input);cout << "Name: " << person.name() << endl;cout << "E-mail: " << person.email() << endl;

Wednesday, 29 February, 12

Page 66: Mobile API Design Techniques

So.

They are tons of efficient solutions and techniques.

Wednesday, 29 February, 12

Page 67: Mobile API Design Techniques

Remember.Be pragmatic.Be consistentBe complete.Be fast.

Wednesday, 29 February, 12

Page 68: Mobile API Design Techniques

Thank you.

twitter.com/[email protected]

Wednesday, 29 February, 12