diff --git a/auth.go b/auth.go index bd76dad..48f4f35 100644 --- a/auth.go +++ b/auth.go @@ -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 @@ -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 } diff --git a/client.go b/client.go index ba418f0..d39ae00 100644 --- a/client.go +++ b/client.go @@ -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, @@ -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 { diff --git a/config.go b/config.go index b542309..3f47417 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package yandexgpt import "net/http" type YandexGPTClientConfig struct { + OAuthToken string ApiKey string IAMToken string HTTPClient *http.Client @@ -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 } diff --git a/examples/completion/main.go b/examples/completion/main.go index 7b6ff0b..4df2012 100644 --- a/examples/completion/main.go +++ b/examples/completion/main.go @@ -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) } diff --git a/examples/completionAsync/main.go b/examples/completionAsync/main.go index 43f1a51..ac2c392 100644 --- a/examples/completionAsync/main.go +++ b/examples/completionAsync/main.go @@ -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) } diff --git a/request.go b/request.go index b4dd2de..bc53d1e 100644 --- a/request.go +++ b/request.go @@ -13,5 +13,5 @@ type YandexGPTCompletionOptions struct { } type YandexIAMRequest struct { - APIKey string `json:"yandexPassportOauthToken"` + OAuthToken string `json:"yandexPassportOauthToken"` }