-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
83 lines (67 loc) · 1.65 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
package chatgpt
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/ayush6624/go-chatgpt/utils"
)
const (
apiURL = "https://api.openai.com/v1"
)
type Client struct {
// HTTP client used to communicate with the API.
client *http.Client
// Config
config *Config
}
type Config struct {
// Base URL for API requests.
BaseURL string
// API Key (Required)
APIKey string
// Organization ID (Optional)
OrganizationID string
}
func NewClient(apikey string) (*Client, error) {
if apikey == "" {
return nil, chatgpt_errors.ErrAPIKeyRequired
}
return &Client{
client: &http.Client{},
config: &Config{
BaseURL: apiURL,
APIKey: apikey,
},
}, nil
}
func NewClientWithConfig(config *Config) (*Client, error) {
if config.APIKey == "" {
return nil, chatgpt_errors.ErrAPIKeyRequired
}
return &Client{
client: &http.Client{},
config: config,
}, nil
}
func (c *Client) sendRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.APIKey))
if c.config.OrganizationID != "" {
req.Header.Set("OpenAI-Organization", c.config.OrganizationID)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
// Parse body
var errMessage interface{}
if err := json.NewDecoder(res.Body).Decode(&errMessage); err != nil {
return nil, err
}
return nil, fmt.Errorf("api request failed: status Code: %d %s %s Message: %+v", res.StatusCode, res.Status, res.Request.URL, errMessage)
}
return res, nil
}