I recently created a public API for Less Accounting. In addition to requiring the username a password for the user that is logging in, the API also requires a application specific api_key to be provided for each request. This allows me to track every api call and turn off malicious applications. This works great, every request adds the query string parameter “api_key” with the value of their key and Bob’s your uncle. Except if you happen to be writing a rails app and want to create a model using ActiveResource. Then you’re screwed. This is because the create method in AR doesn’t allow you to pass in extra parameters. It takes a hash of attributes and then wraps the hash in a {:model=>attributes} and that is all. So if you add a parameter, like :api_key, then it will be delivered as model[api_key]=key in the params hash. Not nice. And it’s even worse because there has been a patch out there which address this problem. (I just nudged the core team, so hopefully this patch will finally be applied). But in the mean time I wrote a nice little hack:
class ActiveResource::Base
def self.custommethodcollectionurl(methodname, options = {}) prefixoptions, queryoptions = splitoptions(options) if methodname == :create “#{prefix(prefixoptions)}#{collectionname}.#{format.extension}#{querystring(queryoptions)}” else “#{prefix(prefixoptions)}#{collectionname}/#{methodname}.#{format.extension}#{querystring(query_options)}” end end end
And then you can call create using the post method:
post(:create, {:expense=>{:title => expense.title, :notes => expense.note :amount => expense.amount }, :api_key=>‘68e050b6-e879-4f78-adf3-d898387036fc’})
I admit it is not the cleanest solution, and you would have to do something similar for update, but at least it’s done.
Note: Lourens had an idea to concat the password with the api_key like this: “#{password}|{#api_key}” and split it on the server. That is also a nice solution. So take your pick.
If you wanted it to build a product you’d find a way to get time to work on it. If you really wanted to start that new hobby you’d sacrifice something to find the time and money to do it.
I'll define a "Wannabe Entrepreneur" as someone who has never made money from their businesses. Here are the different types of wannabes.
In the past few years I've built go-carts, built a 200+ sq ft workshop, written several eBooks. How do I create a life where I have time to work on side projects?
Receive 5 Software projects mistakes we have made over the years and how to avoid them.