Per-request Authentication
Some API integrations require you to make a request to create an access token that a subsequent request will use. When building this, it can add a level of complexity to your application because you have to make two requests for every single resource. Let's say you have two requests for an API. You have:
GetAccessTokenRequest
GetSongsByArtistRequest
But in order to make an API call GetSongsByArtistRequest
you must obtain an access token by calling GetAccessTokenRequest
first. Let's also assume that the GetAccessTokenRequest
requires a username and password. Let's see how this may be implemented currently in your application.
This is pretty cumbersome. Every time you have to make a request, you have to write all these lines of code. One way you can get around this is by utilizing the boot
method on your connector. Let's try refactoring this.
In our connector, we'll extend the boot()
method which is a method that runs before every request that is sent through that connector, and you get access to the special PendingRequest
class which is a class that is used to store all the information of a request to be sent in a temporary way. We'll move our logic into here.
Now when we send any request using this connector, the authentication will be handled every time! We do have to make sure to pass in our authentication requirements (username, password) into the connector, but this is way better than repeating the same code. Saloon is all about standardisation!
You may also write additional logic inside of the boot method, like caching access tokens and reusing them.
Last updated