Sep 20, 2024
Customize clients
You can customize the Search API client, for example, by using a custom HTTP client, or by changing the user agent information.
To customize individual requests, for example, with headers or query parameters, see Request options.
Custom HTTP requester
You can use your own client for making HTTP requests with a custom configuration.
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import (
"net/http"
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)
type CustomRequester struct {
client *http.Client
}
func NewCustomRequester() *MyCustomRequester {
return &CustomRequester{
client: http.DefaultClient,
}
}
func (r *CustomRequester) Request(req *http.Request, _ time.Duration, _ time.Duration) (*http.Response, error) {
// This gets printed to stdout before the request
println("CustomRequester > Request: ", req.RequestURI)
return r.client.Do(req)
}
func main() {
client, _ := search.NewClientWithConfig(
search.SearchConfiguration{
transport.Configuration{
AppID: "ALGOLIA_APPLICATION_ID",
ApiKey: "ALGOLIA_API_KEY",
Requester: NewCustomRequester(),
},
},
)
}
Custom user agent
The Go Search API client sends user agent information
in the user-agent
header.
To override the default user agent information,
use a custom client configuration and set the UserAgent
field.
Copy
1
2
3
4
5
6
7
8
9
10
// Imports and setup code omitted
client, _ := search.NewClientWithConfig(
search.SearchConfiguration{
transport.Configuration{
AppID: "ALGOLIA_APPLICATION_ID",
ApiKey: "ALGOLIA_API_KEY",
UserAgent: "custom go client (optional version)",
},
},
)
This sets “custom go client (optional version)” as the user-agent
header.
Did you find this page helpful?