2019-04-09 17:08:32 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-05-07 14:15:49 +03:00
|
|
|
"encoding/json"
|
2024-05-15 11:59:54 +03:00
|
|
|
"gitea.24example.ru/spavelit/dadata/client/transport"
|
2023-05-07 14:15:49 +03:00
|
|
|
"io"
|
2019-04-09 17:08:32 +03:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Option applies some option to clientOptions.
|
|
|
|
Option func(opts *clientOptions)
|
|
|
|
)
|
|
|
|
|
|
|
|
// WithHttpClient sets custom http.Client.
|
|
|
|
func WithHttpClient(c *http.Client) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.httpClient = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithCredentialProvider sets credential provider.
|
|
|
|
func WithCredentialProvider(c CredentialProvider) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.credentialProvider = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-07 14:15:49 +03:00
|
|
|
func WithEncoderFactory(f transport.EncoderFactory) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.encoderFactory = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDecoderFactory(f transport.DecoderFactory) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.decoderFactory = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:08:32 +03:00
|
|
|
func applyOptions(options *clientOptions, opts ...Option) {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(options)
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 14:15:49 +03:00
|
|
|
|
|
|
|
func defaultJsonEncoderFactory() transport.EncoderFactory {
|
|
|
|
return func(w io.Writer) transport.Encoder {
|
|
|
|
d := json.NewEncoder(w)
|
|
|
|
return func(v interface{}) error {
|
|
|
|
return d.Encode(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultJsonDecoderFactory() transport.DecoderFactory {
|
|
|
|
return func(r io.Reader) transport.Decoder {
|
|
|
|
d := json.NewDecoder(r)
|
|
|
|
return func(v interface{}) error {
|
|
|
|
return d.Decode(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|