tags:

views:

185

answers:

1
+1  Q: 

ruby and HTTParty

Hi,

I'm requesting an internal API with lots of different interface, each is REST :

/users/

/users/username/contacts/

/users/_username/properties/

.....

All this interfaces have a common authentication via a call to

/login

and return an auth_key that need to be used in every others calls

So I made a class for each interface, something like :

class MyApi::Users
  include HTTParty

  def initialize(username, password)
     init = MyApi::Auth.new(username, password)
     @auth_token = init.auth["id"]
  end

  def create(options)
  end

  def show(username)
  end    

  def update(username, options)
  end

  def destroy(username)
  end

end

So after coding all the class, my problem is if I need to call 2 different interfaces one after the other like :

def test
  user = MyApi::Users.new("user", "password")
  user = user.show("toto")
  contacts = MyApi::Contacts.new("user", "password")
  contacts = contacts.list(user.id)
end

As you see I need to authenticate twice and my instances have each a different token, do you have an idea how I could improve that without login twice. Thanks,

+1  A: 

Hi mike why are you authenticating on every call? Without knowing your app it would appear that you should be using sessions,and only authenticating the very first time its needed

There should be no need to pass round the username and password in the you controllers outside the login/auth controller

Something like auth logic will have a before_action making sure you user is authenticated and giving you access to current_user

ADAM
I'm not using rails nether sessions or auth logic,And it's my problem I don't want to authenticate twice,I need a pattern to be able to do it.
Mike
Sorry my assumption
ADAM
Mike can you clarify what you are using to serve the restful resource if you are not using rails. Are you using your own framework, sinatra, etc. Some of these have ways of keeping state and some of these dont. As obviously the answer to this lies in keeping the state on the server side and reusing it on the client side
ADAM
Im just doing a wrapper lib of the API on the client side and cannot make changes to the server side API (and its in java btw)
Mike
Ok. have you seen this -> http://groups.google.com/group/httparty-gem/browse_thread/thread/fda88f9f00ff816 . Unless this has changed it would seem there is no way to do this unless you use a library that can keep track of cookies
ADAM