Skip to content

Commit

Permalink
feat: add api key auth and setter for iam token
Browse files Browse the repository at this point in the history
  • Loading branch information
Ann Vasileva committed Nov 9, 2024
1 parent 48d14e4 commit 278ebcf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
6 changes: 3 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const getIAMUrl = "https://iam.api.cloud.yandex.net/iam/v1/tokens"
// Always call it before creating a request.
//
// If you will use it when API key is specified, method CreateRequest(...) will always use API key.
func (c *YandexGPTClient) UpdateIAMToken(ctx context.Context) error {
iamRq := YandexIAMRequest{APIKey: c.config.ApiKey}
func (c *YandexGPTClient) GetIAMToken(ctx context.Context) error {
iamRq := YandexIAMRequest{OAuthToken: c.config.OAuthToken}
req, err := c.newRequest(ctx, http.MethodPost, getIAMUrl, iamRq)
if err != nil {
return err
Expand All @@ -26,6 +26,6 @@ func (c *YandexGPTClient) UpdateIAMToken(ctx context.Context) error {
}

//set new IAMToken
c.config.updateIAMToken(resp.IAMToken)
c.config.SetIAMToken(resp.IAMToken)
return nil
}
17 changes: 17 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func NewYandexGPTClientWithAPIKey(
}
}

func NewYandexGPTClientWithOAuthToken(
oauthToken string,
) *YandexGPTClient {
config := NewYandexGPTClientConfigWithOAuthToken(oauthToken)

return &YandexGPTClient{
config: config,
requestBuilder: internal.NewRequestBuilder(),
}
}

func (c *YandexGPTClient) newRequest(
ctx context.Context,
method,
Expand Down Expand Up @@ -100,6 +111,12 @@ func (c *YandexGPTClient) setHeaders(request *http.Request) {
fmt.Sprintf("Bearer %s", c.config.IAMToken),
)
}
if c.config.ApiKey != "" {
request.Header.Set(
"Authorization",
fmt.Sprintf("Bearer %s", c.config.ApiKey),
)
}
}

func (c *YandexGPTClient) handleResponseError(response *http.Response) error {
Expand Down
12 changes: 11 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package yandexgpt
import "net/http"

type YandexGPTClientConfig struct {
OAuthToken string
ApiKey string
IAMToken string
HTTPClient *http.Client
Expand Down Expand Up @@ -32,6 +33,15 @@ func NewYandexGPTClientConfigWithAPIKey(
}
}

func (c *YandexGPTClientConfig) updateIAMToken(iamToken string) {
func NewYandexGPTClientConfigWithOAuthToken(
oauthToken string,
) *YandexGPTClientConfig {
return &YandexGPTClientConfig{
OAuthToken: oauthToken,
HTTPClient: &http.Client{},
}
}

func (c *YandexGPTClientConfig) SetIAMToken(iamToken string) {
c.IAMToken = iamToken
}
4 changes: 2 additions & 2 deletions examples/completion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

func main() {
client := yandexgpt.NewYandexGPTClientWithAPIKey("API_KEY")
client := yandexgpt.NewYandexGPTClientWithOAuthToken("OAUTH_TOKEN")

// get, update and set iam token
ctx := context.Background()
err := client.UpdateIAMToken(ctx)
err := client.GetIAMToken(ctx)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/completionAsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

func main() {
client := yandexgpt.NewYandexGPTClientWithAPIKey("API_KEY")
client := yandexgpt.NewYandexGPTClientWithOAuthToken("OAUTH_TOKEN")

// get, update and set iam token
ctx := context.Background()
err := client.UpdateIAMToken(ctx)
err := client.GetIAMToken(ctx)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ type YandexGPTCompletionOptions struct {
}

type YandexIAMRequest struct {
APIKey string `json:"yandexPassportOauthToken"`
OAuthToken string `json:"yandexPassportOauthToken"`
}

0 comments on commit 278ebcf

Please sign in to comment.