|
1 | 1 | package openai |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "context" |
6 | 4 | "encoding/json" |
7 | | - "fmt" |
8 | | - "io" |
9 | 5 | "net/http" |
10 | 6 | "os" |
11 | 7 | ) |
@@ -72,62 +68,6 @@ func (c *Client) WithHTTPClient(httpClient *http.Client) *Client { |
72 | 68 | return c |
73 | 69 | } |
74 | 70 |
|
75 | | -// ReqOption is http requestion functional option. |
76 | | -type ReqOption func(*http.Request) |
77 | | - |
78 | | -// WithSetHeader sets the header key to value val. |
79 | | -func WithSetHeader(key, val string) ReqOption { |
80 | | - return func(req *http.Request) { |
81 | | - if req.Header == nil { |
82 | | - req.Header = make(http.Header) |
83 | | - } |
84 | | - req.Header.Set(key, val) |
85 | | - } |
86 | | -} |
87 | | - |
88 | | -// WithAddHeader adds the val to key header. |
89 | | -func WithAddHeader(key, val string) ReqOption { |
90 | | - return func(req *http.Request) { |
91 | | - if req.Header == nil { |
92 | | - req.Header = make(http.Header) |
93 | | - } |
94 | | - req.Header.Add(key, val) |
95 | | - } |
96 | | -} |
97 | | - |
98 | | -func (c *Client) newRequest(ctx context.Context, method, uri string, body io.Reader, opts ...ReqOption) (*http.Request, error) { |
99 | | - if ctx == nil { |
100 | | - ctx = context.Background() |
101 | | - } |
102 | | - if body == nil { |
103 | | - body = &bytes.Reader{} |
104 | | - } |
105 | | - |
106 | | - req, err := http.NewRequestWithContext(ctx, method, uri, body) |
107 | | - if err != nil { |
108 | | - return nil, err |
109 | | - } |
110 | | - |
111 | | - for _, setOption := range opts { |
112 | | - setOption(req) |
113 | | - } |
114 | | - |
115 | | - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey)) |
116 | | - if len(c.orgID) != 0 { |
117 | | - req.Header.Set("OpenAI-Organization", c.orgID) |
118 | | - } |
119 | | - |
120 | | - req.Header.Set("Accept", "application/json; charset=utf-8") |
121 | | - if body != nil { |
122 | | - // if no content-type is specified we default to json |
123 | | - if ct := req.Header.Get("Content-Type"); len(ct) == 0 { |
124 | | - req.Header.Set("Content-Type", "application/json; charset=utf-8") |
125 | | - } |
126 | | - } |
127 | | - |
128 | | - return req, nil |
129 | | -} |
130 | | - |
131 | 71 | func (c *Client) doRequest(req *http.Request) (*http.Response, error) { |
132 | 72 | resp, err := c.hc.Do(req) |
133 | 73 | if err != nil { |
|
0 commit comments