Linkography
- Here is a link to a screencast of using Amazon EC2 to run a render session using blender in a linux environment
- Here is a pretty good series on REST
N.B. These are based on Rails 1, so look at the last one ('Meets Rails 2') for details on coding
- And some more good REST links
- Here is chapter 5 of Fieldings dissertation on REST which explains its design goals
A great description of the subtleties of REST semantics: Put or Post - The Rest of the story
Lecture Notes
Summary
- What is a Web Service?
- Discover and explore a web service for weather
- What is REST?
- What is RPC?
- REST for Reading data
- REST for true RPC. Call a method to do something on the server!
- Relationship to Cloud Computing
What is a Web Service
- A web server can provide functionality to another server, not just to a person in a browser. This is a web service
- You get information by doing an HTTP GET and receiving XML back
- You provide information to others by responding (in a controller) to an HTTP GET and delivering XML
- RPC - Remote procedure call
- XML_RPC, SOAP, and REST are formalisms that are related
- XML_RPC is simple but not used a super amount
- SOAP is super complicated but adopted in the enterprise
- REST is very popular among open source projects.
- Real world use
- Accessing a Web API
- Providing a Web API from your server
- Need to decide on the url (REST!)
- Often nice to provide sample code and a 'binding' to a language
Pause to look at the big picture
- Servers on the internet, anywhere, can be called as objects and methods
- Resources of all kinds can be offered to clients with no coordination
- The internet becomes a huge, amazing Operating Systems
Retrieving information
Many servers provide info and functionality. Check out Programmable Web for see what's available.
- Many times you need to get an 'API Key' to be granted access. Many are free
- Let's work with weather.
Look at Gems to access Weather.com
Here's where you request a Weather.com API key
Here's the weather in Arlington, MA, in xml: The Weather in Arlington
- Now, let's try to do it through code, to access a different api, weather.com
$ sudo gem install xml-simple $ sudo gem sources -a http://gems.github.com $ sudo gem install jdpace-weatherman
require 'rubygems'
require 'weather_man'
WeatherMan.partner_id = '1191575384'
WeatherMan.license_key = 'c3df3b7f29fe164e'
# Search for a location
# Returns an array of WeatherMan objects
locations = WeatherMan.search('Arlington, MA')
# I happen to know the code for Arlington MA
arlington = WeatherMan.new('USMA0011')
# Fetch only current conditions in metric units
weather = arlington.fetch(:days => 0, :unit => 'm')
# Fetch a 3 day forecast only
weather = arlington.fetch(:days => 3, :current_conditions => false)
# Fetch the current conditions and 5 day forecast in 'standard' units
weather = arlington.fetch
# Print a weather report
puts "Current temperature is: #{weather.current_conditions.temperature}"
puts "But it feels like: #{weather.current_conditions.feels_like}"
puts "Wind speed is: #{weather.current_conditions.wind.speed}"
puts "And the direction is #{weather.current_conditions.wind.direction}"
- Also, let's take a look at the gem code
$ gem which weather_man /opt/local/lib/ruby/gems/1.8/gems/jdpace-weatherman-0.1.2/lib/weather_man.rb $ mate /opt/local/lib/ruby/gems/1.8/gems/jdpace-weatherman-0.1.2/lib/weather_man.rb
RPC - Remote Procedure Calls
- Imagine a procedure (method) : return_fortune_cookie.
- What would it mean to call it between two computers?
- What would it mean to call it between two computers over the internet?
- How would you approach it?
REST - A different way to think about RPC
- HTTP Verbs: GET (HEAD), PUT, POST, DELETE.
- Think of everything in terms of a 'resource' that is being manipulated
- For example, GET means get a representation of the resource marked, e.g.
GET http://www.facebook.com/user/pitosalas GET http://www.facebok.com/users GET localhost:3000/cards/1.xml
- Some things are harder to fit with the model
- What might a fortune cookie service look like as REST?
- The 'resource' here is a single fortune
http://cookieserver.org/fortunes/1 http://cookieserver.org/fortunes http://cookieserver.org/fortunes/random
- Note fortunes/random, random is not exactly identifying a resource; but close enough.
- What if caching was done strictly by url?
- Two advantages:
- some rhyme or reason on how to build urls and
- make logical use of url space
- Different 'representations' possible: html and xml, but others too, say csv or video
- Big one: Standards allow caching in the cloud
Extending the API
- What if we now want to actually do 'RPC' with REST?
- For example, create a new Card in this server?
i = PhonebookService("localhost:3000")
i.create_new_card(:name => "Pito Salas", :business => "6173886367", :home => "7816462540")
- Danger: now anyone on the planet can start hitting my database. So I add an API key
i = PhonebookService("localhost:3000")
i.api_key = "F123FD123"
i.create_new_card(:name => "Pito Salas", :business => "6173886367", :home => "7816462540")
- So let's try to implement something like that. We use a new gem, httparty to simplify accessing an http service
$ sudo gem install httparty $ sudo gem install crack
- First see if httparty command line test harness works
$ httparty -f xml http://localhost:3000/cards/1.xml
- Now lets write a simple utility: addcard.rb
require 'rubygems'
require 'httparty'
# trivial front end to be able to send a rest URL to the server. Much is hardwired.
# Note: I had a problem getting a PUT to work, so I added a special action. Debugging required.
class CardUtil
include HTTParty
base_uri 'localhost:3000'
def update_card(cardnum, text)
options = {:query => {:card => {:name => text}} }
self.class.get("/cards/#{cardnum}/store.xml", options)
end
end
cu = CardUtil.new
cu.update_card(ARGV[0], ARGV[1])
- Here's what the special action looks like:
def store
begin
@card = Card.find(params[:id])
rescue
@card = Card.new
end
respond_to do |format|
if @card.update_attributes(params[:card])
flash[:notice] = 'Card was successfully updated.'
format.html { redirect_to(@card) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @card.errors, :status => :unprocessable_entity }
end
end
end
Run Addcard (NB: COULD BE ON A DIFFERENT SERVER ON A DIFFERENT CONTINENT
$ ruby addcard.rb 3 "john"
- Watch the server:
Processing CardsController#store to xml (for 127.0.0.1 at 2010-06-15 20:34:14) [GET]
Parameters: {"id"=>"3", "card"=>{"name"=>"john"}}
Card Load (0.4ms) SELECT * FROM "cards" WHERE ("cards"."id" = 3)
Card Update (0.7ms) UPDATE "cards" SET "updated_at" = '2010-06-16 00:34:14', "name" = 'john' WHERE "id" = 3
Completed in 36ms (View: 1, DB: 1) | 200 OK [http://localhost/cards/3/store.xml?card[name]=john]
- Check the result:
~/brandeis-cosci/restlecture/assoc (master) $ script/console Loading development environment (Rails 2.3.5) installed irb goodies from ~/.irbrc >> Card.all Card Load (1.4ms) SELECT * FROM "cards" +----+------------+------------+--------------+-------------------------+------------------+ | id | name | home_phone | office_phone | created_at | updated_at | +----+------------+------------+--------------+-------------------------+------------------+ | 1 | hello | | | 2010-06-14 19:45:50 UTC | 2010-06-16 00:17:26 UTC | | 2 | pito salas | | | 2010-06-14 19:51:12 UTC | 2010-06-16 00:23:37 UTC | | 3 | john | | | 2010-06-16 00:27:11 UTC | 2010-06-16 00:34:14 UTC | +----+------------+------------+--------------+-------------------------+------------------+ 3 rows in set >>
- So we have essentially done a remote procedure call -- add_card(cardnum, name) -- across servers
Cloud Computing
- There's a spectrum. One would argue that the above calls are or are not cloud computing
- Cloud Computing: resources offered in small bites, over the network
- What kind of resources?
- Always will include Web Service APIs of one kind or other
- How about Amazon, as a major Cloud Computing provider?
- S3 - Storage Service
- EC2 - Processing
- Anything else?
- There is no strict definition of what cloud computing is
- What about the weather.com service, is that cloud computing?
- DISCUSSION: What's the resource in that case?
