43
API DRIVEN DESIGN How to build things you won’t hate

API Driven Design

Embed Size (px)

Citation preview

Page 1: API Driven Design

API DRIVEN DESIGNHow to build things you won’t hate

Page 2: API Driven Design

6 THINGS I WISH I HAD

KNOWN1. MVC frameworks aren’t designed for APIs

2. Modern tools are awesome; use them

3. Write testable code

4. Make deployment easy & scalable

5. Make your infrastructure work for you

6. Break down app into smaller services

Page 3: API Driven Design
Page 4: API Driven Design

MVC FRAMEWORKS

• Popularized by Ruby on Rails

• Opinionated design optimized for building

quickly

• Convention over configuration

Page 5: API Driven Design

✅ Fast prototyping

✅ Reasonably sane architecture

❌ Inflexible structure

❌ Tightly coupled

❌ Unsuitable for non-web tasks

💣 Active Record pattern

Page 6: API Driven Design

3rd Party API

Page 7: API Driven Design

–Benjamin Franklin*

“Always code as if the guy who ends up

maintaining your code will be a violent

psychopath who knows where you live.”

* Martin Golding

Page 8: API Driven Design

3rd Party API

JSON View

Page 9: API Driven Design

3rd Party API

JSON View

CDN

Page 10: API Driven Design

MVC

Page 11: API Driven Design

CODE IS

(FINALLY)

CHEAPAdd libraries: require

Update: update

Deploy: install

Page 12: API Driven Design

Most common problems are already solved

Focus on using libraries

…but build them independently when

necessary

Contribute fixes back upstream if possible

Page 13: API Driven Design

WHAT DOES EVERY API

NEED?

Authentication

Request & Response formats

Documentation

Page 14: API Driven Design

HOW ABOUT…

SDK

Sandbox

Versioning

Simulator tools

Testing

Logging

Page 15: API Driven Design

THINK IN LIBRARIES

Put SDKs and interfaces in

own repositories

Import as needed - avoid

huge frameworks

Develop independently

Page 16: API Driven Design
Page 17: API Driven Design

TESTS…you have some, right?

…please?

Page 18: API Driven Design
Page 19: API Driven Design
Page 20: API Driven Design

GOALS

• Inputs and outputs match spec

• Data calculated, modified and returned

correctly

• Adheres to interfaces

• Interface makes sense

Page 21: API Driven Design

DO YOU…

• Connect to DB?

• Make API calls?

• Use internal services?

Page 22: API Driven Design

TESTABLE CODE

Support DI; hint interfaces, not classes

Red flags: “new”, “and”, “::”, “instance”, “->->->”

More small classes & methods

Make tests reusable

Write tests first! (TDD)

Page 23: API Driven Design
Page 24: API Driven Design

TESTING SERVICES

• Mock response from service w/ different

results

• Test output formats

• Trust required

Page 25: API Driven Design

DEPLOYMENT 101

Page 26: API Driven Design
Page 27: API Driven Design

Get deployments working!

Rollbacks too…

Doesn’t matter how, but

make it one-click

DeployBot

Page 28: API Driven Design
Page 29: API Driven Design

1. Install

dependencies

2. Compile &

compress

3. Run tests

4. Push the build

5. Flip it live

composer install

less, jsx, jsmin

phpunit

tar -c, scp

tar -x; ln -s; mv -T

Page 30: API Driven Design
Page 31: API Driven Design

WHEN TO SHIP?

• Announcement-based

• Scheduled (monthly, weekly, etc)

• As-needed

• Continuously

Page 32: API Driven Design
Page 33: API Driven Design
Page 34: API Driven Design
Page 35: API Driven Design

INPUT VALIDATION

Page 36: API Driven Design

ACTIONS AROUND

DATA

Page 37: API Driven Design
Page 38: API Driven Design

YOUR FIRST SERVICE

Page 39: API Driven Design

GOOD FIRST CANDIDATES

• Simple tasks (notifications)

• Slow processes (analysis, reporting)

• High-security operations (involving hashed or

encrypted data)

Page 40: API Driven Design

WHY SECURITY

OPERATIONS?

• Network segregation

• Least-privilege principle

• Improve access controls

• Keep sensitive info off front-end servers

Page 41: API Driven Design
Page 42: API Driven Design

6 THINGS I WISH I HAD

KNOWN1. MVC frameworks aren’t designed for APIs

2. Modern tools are awesome; use them

3. Write testable code

4. Make deployment easy & scalable

5. Make your infrastructure work for you

6. Break down app into smaller services

Page 43: API Driven Design

[email protected]

github.com/firehed

linkedin.com/in/firehed

http://joind.in/14740