This document discusses best practices for building applications that integrate with external APIs and services. It recommends reducing the number of HTTP calls, using caching, and being prepared for APIs to change or fail. Specific techniques mentioned include using API wrappers to isolate changes, implementing timeouts and exponential backoff for retries, and leveraging HTTP caching and webhooks to further reduce calls. The document provides rules of thumb for building applications that depend on external services in a resilient manner.
42. Intranet
GET /user/23 + ETag
HR service
Monday, 18 April 2011
43. Intranet
304 Not Modified
HR service
Monday, 18 April 2011
44. #users_controller
def show
@user = User.find(params[:id])
expires_in 24.hours
if stale? (:etag => @user,
:last_modified => @user.updated_at)
respond_to do |wants|
wants.html
wants.xml { render :xml => @user }
end
end
end
Monday, 18 April 2011
45. #client code
def get_user
u = User.find(23)
return u if u.max_age < Time.now.utc
headers = {
'If-None-Match' => u.etag,
'If-Modifed-Since' => u.last_modifed
}
...
data = request.get(path, headers)
...
return u if response.code == 304
return u.update_attributes(data)
end
Monday, 18 April 2011
46. Webhooks
(have your people call my people)
Monday, 18 April 2011
52. Rules of thumb
i.Reduce number of HTTP calls
ii.Be prepared for APIs to change
iii.Expect services to fail
iv.Reduce time and cost of HTTP calls
Monday, 18 April 2011