Skip to content

Commit

Permalink
Add WithTonApiKey function to client package
Browse files Browse the repository at this point in the history
  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))
  }
  • Loading branch information
aleksej-paschenko committed Oct 3, 2023
1 parent 7c95fac commit 2a37510
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 2a37510

Please sign in to comment.