Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Ā
Building Apis That Rock
1. Building APIs That Rock
Congratulations: If youāre here, youāre a nerd!
Wednesday, March 4, 2009
2. hi. iām eaton.
ā¢
ā¢ Voting API
eaton@lullabot.com
ā¢
ā¢ Token
eaton in #drupal
ā¢
ā¢ Form API
twitter.com/eaton
ā¢ Render API
ā¢ ā¢
@eaton is rambling in the Amazon API
#drupalapi presentation.
ā¢
no puppets yet! Twitter API
Wednesday, March 4, 2009
16. An
interface
to reusable
features
http://www.ļ¬ickr.com/photos/splorp/126523012/
Wednesday, March 4, 2009
17. An
Features! Ponies!
interface
to reusable
features
http://www.ļ¬ickr.com/photos/splorp/126523012/
Wednesday, March 4, 2009
18. Your API
An
Features! Ponies!
interface
to reusable
features
http://www.ļ¬ickr.com/photos/splorp/126523012/
Wednesday, March 4, 2009
19. Your UI
Your API
An
Features! Ponies!
interface
to reusable
features
http://www.ļ¬ickr.com/photos/splorp/126523012/
Wednesday, March 4, 2009
20. Your UI Code!
Your API
An
Features! Ponies!
interface
to reusable
features
http://www.ļ¬ickr.com/photos/splorp/126523012/
Wednesday, March 4, 2009
27. Gentlemen! A Recap!
ā¢ Drupal is complicated
ā¢ APIs let other code
use your features
Wednesday, March 4, 2009
28. Gentlemen! A Recap!
ā¢ Drupal is complicated
ā¢ APIs let other code
use your features
ā¢ APIs hide complexity
Wednesday, March 4, 2009
29. Gentlemen! A Recap!
ā¢ Drupal is complicated
ā¢ APIs let other code
use your features
ā¢ APIs hide complexity
ā¢ APIs get everyone on
the same page
Wednesday, March 4, 2009
30. Screw this.
Letās make an API.
http://www.ļ¬ickr.com/photos/djou/2908514697/
Wednesday, March 4, 2009
31. When two modules love
each other very much
The real story of how one API came to be
Wednesday, March 4, 2009
52. What happens when someone adds a new type?
http://www.ļ¬ickr.com/photos/jayd/3296037643
Wednesday, March 4, 2009
53. What happens when someone adds a new type?
What happens when another person does?
http://www.ļ¬ickr.com/photos/jayd/3296037643
Wednesday, March 4, 2009
54. What happens when someone adds a new type?
What happens when another person does?
Use enums for lists that wonāt change.
http://www.ļ¬ickr.com/photos/jayd/3296037643
Wednesday, March 4, 2009
55. What happens when someone adds a new type?
What happens when another person does?
Use enums for lists that wonāt change.
Use strings for stuff you canāt predict
http://www.ļ¬ickr.com/photos/jayd/3296037643
Wednesday, March 4, 2009
59. Remember the verbs?
Are you doing things other modules care about?
Wednesday, March 4, 2009
60. Remember the verbs?
Are you doing things other modules care about?
Wednesday, March 4, 2009
61. Remember the verbs?
Are you doing things other modules care about?
Wednesday, March 4, 2009
62. Remember the verbs?
Are you doing things other modules care about?
module_invoke_all()
Itās like party invitations for code
Wednesday, March 4, 2009
64. Remember the nouns?
Are you building data other modules want to change?
Wednesday, March 4, 2009
65. Remember the nouns?
Are you building data other modules want to change?
Wednesday, March 4, 2009
66. Remember the nouns?
Are you building data other modules want to change?
Wednesday, March 4, 2009
67. Remember the nouns?
Are you building data other modules want to change?
drupal_alter()
Itās like passing around the doobie of data
Wednesday, March 4, 2009
77. Stop! Hammertime!
ā¢ Identify verbs and nouns
Wednesday, March 4, 2009
78. Stop! Hammertime!
ā¢ Identify verbs and nouns
ā¢ Layer your verbs to hide
complexity
Wednesday, March 4, 2009
79. Stop! Hammertime!
ā¢ Identify verbs and nouns
ā¢ Layer your verbs to hide
complexity
ā¢ Account for new uses
Wednesday, March 4, 2009
80. Stop! Hammertime!
ā¢ Identify verbs and nouns
ā¢ Layer your verbs to hide
complexity
ā¢ Account for new uses
ā¢ Broadcast events via
hooks
Wednesday, March 4, 2009
81. Stop! Hammertime!
ā¢ Identify verbs and nouns
ā¢ Layer your verbs to hide
complexity
ā¢ Account for new uses
ā¢ Broadcast events via
hooks
ā¢ Let modules alter data
Wednesday, March 4, 2009
85. The Lone Ranger
ā¢ Reinventing the wheel
Wednesday, March 4, 2009
86. The Lone Ranger
ā¢ Reinventing the wheel
ā¢ Ignoring conventions
Wednesday, March 4, 2009
87. The Lone Ranger
ā¢ Reinventing the wheel
ā¢ Ignoring conventions
ā¢ Ignoring related APIs!
Wednesday, March 4, 2009
88. The Lone Ranger
ā¢ Reinventing the wheel
ā¢ Ignoring conventions
ā¢ Ignoring related APIs!
ā¢ Common for newcomers, or obsessives
Wednesday, March 4, 2009
91. Invisible Assumptions
ā¢ Assuming the global $user
ā¢ Checking arg(0)ā¦
Wednesday, March 4, 2009
92. Invisible Assumptions
ā¢ Assuming the global $user
ā¢ Checking arg(0)ā¦
ā¢ Assuming logged in usersā¦
Wednesday, March 4, 2009
93. Invisible Assumptions
ā¢ Assuming the global $user
ā¢ Checking arg(0)ā¦
ā¢ Assuming logged in usersā¦
ā¢ Common with UI-bound APIs
Wednesday, March 4, 2009
95. Helping them to death
ā¢ āIāll set the breadcrumb while Iām at it...ā
Wednesday, March 4, 2009
96. Helping them to death
ā¢ āIāll set the breadcrumb while Iām at it...ā
ā¢ āAnd Iāll send an email, too!ā
Wednesday, March 4, 2009
97. Helping them to death
ā¢ āIāll set the breadcrumb while Iām at it...ā
ā¢ āAnd Iāll send an email, too!ā
ā¢ āAnd add these links, andā¦ā
Wednesday, March 4, 2009
98. Helping them to death
ā¢ āIāll set the breadcrumb while Iām at it...ā
ā¢ āAnd Iāll send an email, too!ā
ā¢ āAnd add these links, andā¦ā
ā¢ Common when an API grows out of a
speciļ¬c use case, or an existing module
Wednesday, March 4, 2009
100. Leaky Abstraction
ā¢ āJust use this functionā¦ā
Wednesday, March 4, 2009
101. Leaky Abstraction
ā¢ āJust use this functionā¦ā
ā¢ ...And learn everything underneath it.
Wednesday, March 4, 2009
102. Leaky Abstraction
ā¢ āJust use this functionā¦ā
ā¢ ...And learn everything underneath it.
ā¢ Common in complex problem domains
Wednesday, March 4, 2009
103. Leaky Abstraction
ā¢ āJust use this functionā¦ā
ā¢ ...And learn everything underneath it.
ā¢ Common in complex problem domains
ā¢ Simplify, or get out of the way
Wednesday, March 4, 2009
104. Leaky Abstraction
ā¢ āJust use this functionā¦ā
ā¢ ...And learn everything underneath it.
ā¢ Common in complex problem domains
ā¢ Simplify, or get out of the way
ā¢ Sometimes, problems are just complicated.
Wednesday, March 4, 2009
106. Fake Flexibility
ā¢ āOh, you can save any dataā¦ā (votingapi)
Wednesday, March 4, 2009
107. Fake Flexibility
ā¢ āOh, you can save any dataā¦ā (votingapi)
ā¢ āOh, thereās a hook for thatā¦ā (blogapi)
Wednesday, March 4, 2009
108. Fake Flexibility
ā¢ āOh, you can save any dataā¦ā (votingapi)
ā¢ āOh, thereās a hook for thatā¦ā (blogapi)
ā¢ Build 3 test implementations
Wednesday, March 4, 2009
109. Fake Flexibility
ā¢ āOh, you can save any dataā¦ā (votingapi)
ā¢ āOh, thereās a hook for thatā¦ā (blogapi)
ā¢ Build 3 test implementations
ā¢ Step through different use cases
Wednesday, March 4, 2009
111. Mission Creep
ā¢ Even worse than feature creep
Wednesday, March 4, 2009
112. Mission Creep
ā¢ Even worse than feature creep
ā¢ Common with āsolve-a-big-problemā APIs
Wednesday, March 4, 2009
113. Mission Creep
ā¢ Even worse than feature creep
ā¢ Common with āsolve-a-big-problemā APIs
ā¢ Focus, focus, focus.
Wednesday, March 4, 2009
114. Mission Creep
ā¢ Even worse than feature creep
ā¢ Common with āsolve-a-big-problemā APIs
ā¢ Focus, focus, focus.
ā¢ Split into complementary APIs
Wednesday, March 4, 2009
116. Dependency Soup
ā¢ The ļ¬ip side of mission creep
Wednesday, March 4, 2009
117. Dependency Soup
ā¢ The ļ¬ip side of mission creep
ā¢ Focus, focus, focus!
Wednesday, March 4, 2009
118. Dependency Soup
ā¢ The ļ¬ip side of mission creep
ā¢ Focus, focus, focus!
ā¢ Make the dependencies optional
Wednesday, March 4, 2009
119. Dependency Soup
ā¢ The ļ¬ip side of mission creep
ā¢ Focus, focus, focus!
ā¢ Make the dependencies optional
ā¢ Pray for a package manager
Wednesday, March 4, 2009
122. Oh, the Recappery!
ā¢ Cooperate
ā¢ Donāt assume too much
Wednesday, March 4, 2009
123. Oh, the Recappery!
ā¢ Cooperate
ā¢ Donāt assume too much
ā¢ Donāt be pushy
Wednesday, March 4, 2009
124. Oh, the Recappery!
ā¢ Cooperate
ā¢ Donāt assume too much
ā¢ Donāt be pushy
ā¢ Simplify or step aside
Wednesday, March 4, 2009
125. Oh, the Recappery!
ā¢ Cooperate
ā¢ Donāt assume too much
ā¢ Donāt be pushy
ā¢ Simplify or step aside
ā¢ Test your extensions
Wednesday, March 4, 2009
126. Oh, the Recappery!
ā¢ Cooperate
ā¢ Donāt assume too much
ā¢ Donāt be pushy
ā¢ Simplify or step aside
ā¢ Test your extensions
ā¢ Stay focused
Wednesday, March 4, 2009
You were promised puppets. Also, magic.
Unfortunately, you’ve been deceived. There are no puppets.
The ends justify the means: APIs are Drupal’s lifeblood.
When I started using Drupal, it was a little goofy, but fundamentally simple.
Core. Some modules (gallery? event? done!)
Maybe a custom module and a theme if you were REALLY crazy.
When I started using Drupal, it was a little goofy, but fundamentally simple.
Core. Some modules (gallery? event? done!)
Maybe a custom module and a theme if you were REALLY crazy.
Today, lots and lots of smaller modules
Image == Views, CCK, FileField, ImageField, ImageCache, ImageAPI, Custom Breadcrumbs.
Flexibility vs. Complexity
All the pieces have to work with each other! And that’s where APIs come in.
Today, lots and lots of smaller modules
Image == Views, CCK, FileField, ImageField, ImageCache, ImageAPI, Custom Breadcrumbs.
Flexibility vs. Complexity
All the pieces have to work with each other! And that’s where APIs come in.
The pull between functionality and flexibility
Building code that does stuff, APIs that let other code control it, and UIs that let users configure/control
The pull between functionality and flexibility
Building code that does stuff, APIs that let other code control it, and UIs that let users configure/control
Imagine a Drupal module as a giant pile of money.
Mmmmmmmmmmm.
Each of those awesome features is like a crisp $20.
Imagine “Code” -- other Drupal modules -- as users who can’t click on anything. All they can do is call other functions.
Can they use your module?
Could an install profile set up your module automatically?
Can someone turn off your UI entirely, and make it automatic?
Can someone build a new UI for your module?
Imagine a Drupal module as a giant pile of money.
Mmmmmmmmmmm.
Each of those awesome features is like a crisp $20.
Imagine “Code” -- other Drupal modules -- as users who can’t click on anything. All they can do is call other functions.
Can they use your module?
Could an install profile set up your module automatically?
Can someone turn off your UI entirely, and make it automatic?
Can someone build a new UI for your module?
Imagine a Drupal module as a giant pile of money.
Mmmmmmmmmmm.
Each of those awesome features is like a crisp $20.
Imagine “Code” -- other Drupal modules -- as users who can’t click on anything. All they can do is call other functions.
Can they use your module?
Could an install profile set up your module automatically?
Can someone turn off your UI entirely, and make it automatic?
Can someone build a new UI for your module?
The ATM is a metaphor: it’s a basic way of controlling things that can be used externally.
Flag
Organic Groups
node_save()
The ATM is a metaphor: it’s a basic way of controlling things that can be used externally.
Flag
Organic Groups
node_save()
The ATM is a metaphor: it’s a basic way of controlling things that can be used externally.
Flag
Organic Groups
node_save()
The ATM is a metaphor: it’s a basic way of controlling things that can be used externally.
Flag
Organic Groups
node_save()
The ATM is a metaphor: it’s a basic way of controlling things that can be used externally.
Flag
Organic Groups
node_save()
The ATM is a metaphor: it’s a basic way of controlling things that can be used externally.
Flag
Organic Groups
node_save()
Something complicated can be simplified by putting a buffer between it, and you.
ImageAPI
Messaging
Visitor
cache_get() and cache_set()
Something complicated can be simplified by putting a buffer between it, and you.
ImageAPI
Messaging
Visitor
cache_get() and cache_set()
The value in credit cards isn’t that they’re thin and plastic
It’s that everyone uses them, so the problem of “IOUs” and “Store credit” and “Pay you back later” are all handled with this.
Everyone’s on the same page, so they can get on to solving other problems
The value in credit cards isn’t that they’re thin and plastic
It’s that everyone uses them, so the problem of “IOUs” and “Store credit” and “Pay you back later” are all handled with this.
Everyone’s on the same page, so they can get on to solving other problems
History of VotingAPI - 2005
Was making a community site that needed to rate comments AND nodes, with a nice flash widget AND a nice
Picked the one that seemed best and shamelessly ripped its guts out
History of VotingAPI - 2005
Was making a community site that needed to rate comments AND nodes, with a nice flash widget AND a nice
Picked the one that seemed best and shamelessly ripped its guts out
History of VotingAPI - 2005
Was making a community site that needed to rate comments AND nodes, with a nice flash widget AND a nice
Picked the one that seemed best and shamelessly ripped its guts out
Voila! VotingAPI 1.0 Beta! Simple, but effective.
How to start thinking about it like an API?
Find the nouns
Find the verbs
This is similar to building good classes in OOP languages like Java. Those guys think about APIs all freakin’ day.
Voila! VotingAPI 1.0 Beta! Simple, but effective.
How to start thinking about it like an API?
Find the nouns
Find the verbs
This is similar to building good classes in OOP languages like Java. Those guys think about APIs all freakin’ day.
Voila! VotingAPI 1.0 Beta! Simple, but effective.
How to start thinking about it like an API?
Find the nouns
Find the verbs
This is similar to building good classes in OOP languages like Java. Those guys think about APIs all freakin’ day.
Voila! VotingAPI 1.0 Beta! Simple, but effective.
How to start thinking about it like an API?
Find the nouns
Find the verbs
This is similar to building good classes in OOP languages like Java. Those guys think about APIs all freakin’ day.
Voila! VotingAPI 1.0 Beta! Simple, but effective.
How to start thinking about it like an API?
Find the nouns
Find the verbs
This is similar to building good classes in OOP languages like Java. Those guys think about APIs all freakin’ day.
Voila! VotingAPI 1.0 Beta! Simple, but effective.
How to start thinking about it like an API?
Find the nouns
Find the verbs
This is similar to building good classes in OOP languages like Java. Those guys think about APIs all freakin’ day.
Discussions on drupal.org, working with other developers to gather use cases.
Not flexible enough! My my noun weren’t precise enough…
Additional data: “Tag” for multiple criteria voting
My verbs were too broad!
Sub-functions.
Building in layers is important
Discussions on drupal.org, working with other developers to gather use cases.
Not flexible enough! My my noun weren’t precise enough…
Additional data: “Tag” for multiple criteria voting
My verbs were too broad!
Sub-functions.
Building in layers is important
Discussions on drupal.org, working with other developers to gather use cases.
Not flexible enough! My my noun weren’t precise enough…
Additional data: “Tag” for multiple criteria voting
My verbs were too broad!
Sub-functions.
Building in layers is important
Discussions on drupal.org, working with other developers to gather use cases.
Not flexible enough! My my noun weren’t precise enough…
Additional data: “Tag” for multiple criteria voting
My verbs were too broad!
Sub-functions.
Building in layers is important
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Feedback: Vote on a 1-100 scale! Or vote with “points” like Digg! And getting results is too slow -- sorting and filtering with them takes complex SQL!
The “vote” noun has a ‘value type’ now. What does the number MEAN?
New noun: “Results”, new verb “Recalculate”
Now have votes that can be set, and retrieved. Results that can be recalculated and retrieved. “Value type” lets modules use it in unanticipated ways, too!
Holy crap, someone hates me.
Quick show of hands -- what’s wrong with this?
Holy crap, someone hates me.
Quick show of hands -- what’s wrong with this?
Holy crap, someone hates me.
Quick show of hands -- what’s wrong with this?
Holy crap, someone hates me.
Quick show of hands -- what’s wrong with this?
Holy crap, someone hates me.
Quick show of hands -- what’s wrong with this?
Holy crap, someone hates me.
Quick show of hands -- what’s wrong with this?
We’ve covered the basics of letting other modules “steer” your code.
The next step - giving your module points of expansion
Several common patterns in Drupal