Mike
Mike

Reputation: 5193

ruby and HTTParty

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,

Upvotes: 1

Views: 1009

Answers (1)

ADAM
ADAM

Reputation: 3903

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

Upvotes: 1

Related Questions