lancejpollard / authlogic-connect

Instant Oauth and OpenID support for your Rails and Sinatra Apps
MIT License
148 stars 27 forks source link

How to set the user-agent? #19

Closed aac closed 14 years ago

aac commented 14 years ago

Foursquare does rate-limiting based on the user-agent.

Right now, the main way to set the user-agent is to pass user-agent in to the options hash of the AccessToken's get and post methods. I kind of like the idea of adding user-agent as an option in the config files (as is done for scope, and for api_version in my fork). Any thoughts?

lancejpollard commented 14 years ago

This comes down to how much we should add to the token class relating to customizing default headers, params, and paths. If you wanted to add custom header attributes ("MyApp-Version" => "1.2" for example), then there's more to include. I like the idea of including the api_versions in the settings hash, and then using regexp's to pass it in:

"http://api.foursquare.com/:api_version"
:api_versions => {1 => "v1", 2 => "v2"},

...

token.get("/users.json", :api_version => 2) #=> "/v2/users.json"

But there's too much variability in the headers/params hash to setup a mechanism in the core for customizing defaults. A compromise would be to build a primitive solution, just adding a ":headers" tag in the AuthlogicConnect.config hash, and we could pass it in there:

:headers {
  "User-Agent" => "Safari",
  "MyApp-Version" => "1.2"
}

Then the resulting settings hash would have all of this:

:twitter => {
  :key => "my_key",
  :secret => "my_secret",
  :headers => {
    "User-Agent" => "Safari",
    "MyApp-Version" => "1.2"
  },
  :api_version => 1
}

Would something like that work? If you wanted to get anything more complicated, say set the User-Agent based on the user, but behind the scenes so you didn't have to pass it in via the headers argument in get, I would write a module for that specific to your app. If it evolved into something generic/reusable, we could move it in here.

Does that work?

lancejpollard commented 14 years ago
class FoursquareToken < OauthToken

  key do |access_token|
    body = JSON.parse(access_token.get("/user.json").body)
    user_id = body['user']['id'].to_s
  end

  settings "http://api.foursquare.com/:api_version",
    :request_token_url  => "http://foursquare.com/oauth/request_token",
    :access_token_url   => "http://foursquare.com/oauth/access_token",
    :authorize_url      => "http://foursquare.com/oauth/authorize",
    :api_versions       => {1 => "v1", 2 => "v2"},
    :api_version        => 1

end
lancejpollard commented 14 years ago

For now, i suggest writing a module that overrides some of the core OauthToken methods to add your specific helpers. It requires a bit of code to make the above be simple, I think handling it in the app will help keep the gem thinner.

Best, Lance