From 2a375104f6b72b09533e8b89e400ea9447bbb474 Mon Sep 17 00:00:00 2001 From: "aleksej.paschenko" Date: Tue, 3 Oct 2023 19:00:18 +0300 Subject: [PATCH] Add WithTonApiKey function to client package The idea is to use WithTonApiKey in the following way: import ( tonapiClient "github.com/tonkeeper/opentonapi/client" ) func main() { cli, _ := tonapiClient.NewClient("https://tonapi.io", tonapiClient.WithTonApiKey(tonapiKey)) } --- client/client_options.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 client/client_options.go diff --git a/client/client_options.go b/client/client_options.go new file mode 100644 index 00000000..50f918c8 --- /dev/null +++ b/client/client_options.go @@ -0,0 +1,37 @@ +package client + +import ( + "fmt" + "net/http" + + ht "github.com/ogen-go/ogen/http" +) + +type clientWithApiKey struct { + header string +} + +func (c clientWithApiKey) Do(r *http.Request) (*http.Response, error) { + r.Header.Set("Authorization", c.header) + return http.DefaultClient.Do(r) +} + +var _ ht.Client = &clientWithApiKey{} + +// WithTonApiKey configures client to use tonApiKey for authorization. +// When working with tonapi.io, you should consider getting an API key at https://tonconsole.com/. +// +// Example: +// +// import ( +// +// tonapiClient "github.com/tonkeeper/opentonapi/client" +// +// ) +// +// func main() { +// cli, _ := tonapiClient.NewClient("https://tonapi.io", tonapiClient.WithTonApiKey(tonapiKey)) +// } +func WithTonApiKey(tonApiKey string) ClientOption { + return WithClient(&clientWithApiKey{header: fmt.Sprintf("Bearer %s", tonApiKey)}) +}