-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from tonkeeper/with-tonapi-key
Add WithTonApiKey function to client package
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}) | ||
} |