whichdigital / active-rest-client

ActiveRestClient API Client
https://rubygems.org/gems/active_rest_client
MIT License
386 stars 44 forks source link

Multiple clients for different APIs #97

Closed bilby91 closed 9 years ago

bilby91 commented 9 years ago

Is it possible to have multiple clients that use different APIs with different credentials ? I'm concerned about this because all the configuration are stored in class variables in ActiveRestClient::Base.

Thanks!

andyjeffries commented 9 years ago

They're stored in class instance variables (@variable within a class method). So you can do this and indeed the username/password is intended to be called within the specific API class, not a parent class. For example, this works fine with both classes using different API credentials (obviously if you had more than one class using a given username/password I would definitely extract it to a constant):

# API Endpoint defined at https://httpbin.org/

class Person < ActiveRestClient::Base
  base_url "https://httpbin.org/basic-auth"
  username "user"
  password "passwd"

  get :all, "/user/passwd"
end

class Car < ActiveRestClient::Base
  base_url "https://httpbin.org/basic-auth"
  username "someone"
  password "else"

  get :all, "/someone/else"
end

Person.all
Car.all