Upload
eric-stern
View
564
Download
0
Embed Size (px)
Citation preview
API DRIVEN DESIGNHow to build things you won’t hate
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
MVC FRAMEWORKS
• Popularized by Ruby on Rails
• Opinionated design optimized for building
quickly
• Convention over configuration
✅ Fast prototyping
✅ Reasonably sane architecture
❌ Inflexible structure
❌ Tightly coupled
❌ Unsuitable for non-web tasks
💣 Active Record pattern
3rd Party API
–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
3rd Party API
JSON View
3rd Party API
JSON View
CDN
MVC
CODE IS
(FINALLY)
CHEAPAdd libraries: require
Update: update
Deploy: install
Most common problems are already solved
Focus on using libraries
…but build them independently when
necessary
Contribute fixes back upstream if possible
WHAT DOES EVERY API
NEED?
Authentication
Request & Response formats
Documentation
HOW ABOUT…
SDK
Sandbox
Versioning
Simulator tools
Testing
Logging
THINK IN LIBRARIES
Put SDKs and interfaces in
own repositories
Import as needed - avoid
huge frameworks
Develop independently
TESTS…you have some, right?
…please?
GOALS
• Inputs and outputs match spec
• Data calculated, modified and returned
correctly
• Adheres to interfaces
• Interface makes sense
DO YOU…
• Connect to DB?
• Make API calls?
• Use internal services?
TESTABLE CODE
Support DI; hint interfaces, not classes
Red flags: “new”, “and”, “::”, “instance”, “->->->”
More small classes & methods
Make tests reusable
Write tests first! (TDD)
TESTING SERVICES
• Mock response from service w/ different
results
• Test output formats
• Trust required
DEPLOYMENT 101
Get deployments working!
Rollbacks too…
Doesn’t matter how, but
make it one-click
DeployBot
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
WHEN TO SHIP?
• Announcement-based
• Scheduled (monthly, weekly, etc)
• As-needed
• Continuously
INPUT VALIDATION
ACTIONS AROUND
DATA
YOUR FIRST SERVICE
GOOD FIRST CANDIDATES
• Simple tasks (notifications)
• Slow processes (analysis, reporting)
• High-security operations (involving hashed or
encrypted data)
WHY SECURITY
OPERATIONS?
• Network segregation
• Least-privilege principle
• Improve access controls
• Keep sensitive info off front-end servers
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
github.com/firehed
linkedin.com/in/firehed
http://joind.in/14740