Dynamic Authentication #154
Replies: 2 comments
-
Oh, I also wonder if having to rely on class level attributes is going to make code written with Scorpio prone to race conditions? |
Beta Was this translation helpful? Give feedback.
-
There isn't currently any way to pass this through per-request with ResourceBase, unfortunately. I've added this to the todo list. Class-level attributes do have the downside of being thread-unsafe. This can be worked around with Thread.current variables. I think that without a way currently to pass this to each request, the best solution would look something like this: module ConnectWise
class Resource < Scorpio::ResourceBase
def self.faraday_builder
lambda { |builder|
username, password = Thread.current[:connectwise_username], Thread.current[:connectwise_password]
builder.use Faraday::Request::BasicAuthentication, username, password
}
end
end
end You could also do this using Scorpio without ResourceBase, using the OpenAPI document more directly, as described in the readme under Pet Store (using Scorpio::OpenAPI classes). It's kind of a lower-level API that gives you more flexibility but lacks some of the nice, more ORM-ish abstractions that ResourceBase offers. You can pass |
Beta Was this translation helpful? Give feedback.
-
My use case is that I need to export data to an API on behalf of my users. As such, the API credentials are different for each user, and I'm trying to work out the best way to set the credentials at runtime.
While prototyping, I set credentials via
faraday_builder
like this:Now, I need to pass in credentials at runtime, so I defined class-level attributes that have to be set before methods can be called. I'm not sure this is the best way to do this though:
This seems like a pretty common thing people are going to need to do while using Scorpio, so wanted to ask! Thanks!
Beta Was this translation helpful? Give feedback.
All reactions