-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
137 lines (115 loc) · 2.85 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package yandexgpt
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/sheeiavellie/go-yandexgpt/internal"
)
// YandexGPT Client
type YandexGPTClient struct {
requestBuilder internal.RequestBuilder
config *YandexGPTClientConfig
}
// Creates new YandexGPT Client.
//
// If you're using this option, keep in mind that you will need to generate IAM token yourself.
func NewYandexGPTClientWithIAMToken(
iamToken string,
) *YandexGPTClient {
config := NewYandexGPTClientConfigWithIAMToken(iamToken)
return &YandexGPTClient{
config: config,
requestBuilder: internal.NewRequestBuilder(),
}
}
// Creates new YandexGPT Client.
func NewYandexGPTClient() *YandexGPTClient {
config := NewYandexGPTClientConfig()
return &YandexGPTClient{
config: config,
requestBuilder: internal.NewRequestBuilder(),
}
}
// Creates new YandexGPT Client.
//
// You will need to specify your own API key.
func NewYandexGPTClientWithAPIKey(
apiKey string,
) *YandexGPTClient {
config := NewYandexGPTClientConfigWithAPIKey(apiKey)
return &YandexGPTClient{
config: config,
requestBuilder: internal.NewRequestBuilder(),
}
}
// Creates new YandexGPT Client.
//
// You will need to specify your own OAuth key.
func NewYandexGPTClientWithOAuthToken(
oauthToken string,
) *YandexGPTClient {
config := NewYandexGPTClientConfigWithOAuthToken(oauthToken)
return &YandexGPTClient{
config: config,
requestBuilder: internal.NewRequestBuilder(),
}
}
func (c *YandexGPTClient) newRequest(
ctx context.Context,
method,
url string,
req any,
) (*http.Request, error) {
request, err := c.requestBuilder.Build(ctx, method, url, req)
if err != nil {
return nil, err
}
c.setHeaders(request)
return request, nil
}
func (c *YandexGPTClient) sendRequest(
request *http.Request,
v Response,
) error {
response, err := c.config.HTTPClient.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
if internal.IsBadStatusCode(response.StatusCode) {
return c.handleResponseError(response)
}
if v != nil {
v.SetHeader(response.Header)
}
return json.NewDecoder(response.Body).Decode(v)
}
func (c *YandexGPTClient) setHeaders(request *http.Request) {
request.Header.Set("Content-Type", "application/json")
if c.config.IAMToken != "" {
request.Header.Set(
"Authorization",
fmt.Sprintf("Bearer %s", c.config.IAMToken),
)
}
if c.config.ApiKey != "" {
request.Header.Set(
"Authorization",
fmt.Sprintf("Api-Key %s", c.config.ApiKey),
)
}
}
func (c *YandexGPTClient) handleResponseError(response *http.Response) error {
var errResponse YandexGPTResponseBad
err := json.NewDecoder(response.Body).Decode(&errResponse)
if err != nil {
return err
}
return fmt.Errorf(
"bad response. Http Status %d %s message %s",
errResponse.Error.HTTPCode,
errResponse.Error.HTTPStatus,
errResponse.Error.Message,
)
}