Skip to content

Commit

Permalink
Merge pull request #207 from tonkeeper/with-tonapi-key
Browse files Browse the repository at this point in the history
Add WithTonApiKey function to client package
  • Loading branch information
mr-tron authored Oct 4, 2023
2 parents ce3697e + 2a37510 commit 02c3626
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions client/client_options.go
Original file line number Diff line number Diff line change
@@ -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)})
}

0 comments on commit 02c3626

Please sign in to comment.