πConnectors
Connectors are classes which define an API integration's properties like its URL and headers. Any behaviour that should be used on every request like authentication should be defined in a connector. You should have a separate connector for each API integration.
Getting Started
You should establish a standard place to keep your API connectors. For example in Laravel, a sensible place would be to place them inside the App/Http/Integrations
folder.
Create a new class and extend the abstract Connector
class. You will then need to define a method resolveBaseUrl
. This is the URL that points to the API.
If you have installed the Laravel plugin, you can use the php artisan saloon:connector command to create a connector.
Headers
Most API integrations will have common headers used by all of its requests. You can extend the defaultHeaders
method to define the headers.
You can also use the headers
method on a connector instance.
Timeout
By default, Saloon will have a connection timeout of 10 seconds and a request timeout of 30 seconds. You can customise this by using the HasTimeout
trait and specifying a connectTimeout
and requestTimeout
property.
Constructor Arguments
Since connectors are just classes, you can define a constructor to populate its default properties. For example, if the URL changes per user of your application you can define this as a constructor argument.
HTTP Client Config
The connector uses a HTTP client to send the request. By default, this client is Guzzle. If you would like to define Guzzle config options then you can extend the defaultConfig
method.
Click here to see a list of the available options Guzzle provide.
You can also use the config
method on a connector instance.
Last updated