Add testable request builder (#162)

* Add testable request builder

* improve tests
This commit is contained in:
sashabaranov
2023-03-15 13:16:33 +04:00
committed by GitHub
parent 53d195cf5a
commit c34bc77f1a
13 changed files with 205 additions and 152 deletions

20
api.go
View File

@@ -1,7 +1,6 @@
package openai
import (
"bytes"
"context"
"encoding/json"
"fmt"
@@ -12,7 +11,7 @@ import (
type Client struct {
config ClientConfig
marshaller marshaller
requestBuilder requestBuilder
}
// NewClient creates new OpenAI API client.
@@ -24,8 +23,8 @@ func NewClient(authToken string) *Client {
// NewClientWithConfig creates new OpenAI API client for specified config.
func NewClientWithConfig(config ClientConfig) *Client {
return &Client{
config: config,
marshaller: &jsonMarshaller{},
config: config,
requestBuilder: newRequestBuilder(),
}
}
@@ -91,17 +90,8 @@ func (c *Client) newStreamRequest(
ctx context.Context,
method string,
urlSuffix string,
body interface{}) (*http.Request, error) {
var reqBody []byte
if body != nil {
var err error
reqBody, err = c.marshaller.marshal(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequestWithContext(ctx, method, c.fullURL(urlSuffix), bytes.NewBuffer(reqBody))
body any) (*http.Request, error) {
req, err := c.requestBuilder.build(ctx, method, c.fullURL(urlSuffix), body)
if err != nil {
return nil, err
}