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, see Request options.
Timeouts
To change the timeouts for all requests,
create a custom configuration and create the client with the NewClientWithConfig
function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package timeouts
import (
"time"
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
"github.com/algolia/algoliasearch-client-go/v4/algolia/transport"
)
func main() {
client, _ := search.NewClientWithConfig(
search.SearchConfiguration{
Configuration: transport.Configuration{
AppID: "ALGOLIA_APPLICATION_ID",
ApiKey: "ALGOLIA_API_KEY",
// Adjust timeouts
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
ConnectTimeout: 10 * time.Second,
}
ConnectTimeout
|
type: time.Duration
default: 2
Maximum number of seconds to wait for the connection to be established. |
ReadTimeout
|
type: time.Duration
default: 5
Maximum number of seconds to wait for a response from the server (for |
WriteTimeout
|
type: time.Duration
default: 30
Maximum number of seconds to wait for a response from the server (for requests other than |
Custom HTTP requesters
You can use your own client for making HTTP requests with a custom configuration.
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
35
36
37
package custom
import (
"net/http"
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
"github.com/algolia/algoliasearch-client-go/v4/algolia/transport"
)
type CustomRequester struct {
client *http.Client
}
func NewCustomRequester() *CustomRequester {
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
fmt.Printf("CustomRequester > Request: %s\n", req.URL.String())
return r.client.Do(req)
}
func main() {
client, _ := search.NewClientWithConfig(
search.SearchConfiguration{
transport.Configuration{
AppID: "ALGOLIA_APPLICATION_ID",
ApiKey: "ALGOLIA_API_KEY",
Requester: NewCustomRequester(),
},
},
)
}
Headers
To add headers to all requests,
use the DefaultHeader
field of the transport.Configuration
struct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package headers
import (
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
"github.com/algolia/algoliasearch-client-go/v4/algolia/transport"
)
func main() {
defaultHeaders := make(map[string]string)
defaultHeaders["extra-header"] = "greetings"
client, _ := search.NewClientWithConfig(
search.SearchConfiguration{
transport.Configuration{
AppID: "ALGOLIA_APPLICATION_ID",
ApiKey: "ALGOLIA_API_KEY",
DefaultHeader: defaultHeaders,
},
},
)
}