* move error_accumulator into internal pkg (#304) * move error_accumulator into internal pkg (#304) * add a test for ErrTooManyEmptyStreamMessages in stream_reader (#304)
This commit is contained in:
committed by
GitHub
parent
fa694c61c2
commit
1394329e44
44
internal/error_accumulator.go
Normal file
44
internal/error_accumulator.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type ErrorAccumulator interface {
|
||||
Write(p []byte) error
|
||||
Bytes() []byte
|
||||
}
|
||||
|
||||
type errorBuffer interface {
|
||||
io.Writer
|
||||
Len() int
|
||||
Bytes() []byte
|
||||
}
|
||||
|
||||
type DefaultErrorAccumulator struct {
|
||||
Buffer errorBuffer
|
||||
}
|
||||
|
||||
func NewErrorAccumulator() ErrorAccumulator {
|
||||
return &DefaultErrorAccumulator{
|
||||
Buffer: &bytes.Buffer{},
|
||||
}
|
||||
}
|
||||
|
||||
func (e *DefaultErrorAccumulator) Write(p []byte) error {
|
||||
_, err := e.Buffer.Write(p)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error accumulator write error, %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte) {
|
||||
if e.Buffer.Len() == 0 {
|
||||
return
|
||||
}
|
||||
errBytes = e.Buffer.Bytes()
|
||||
return
|
||||
}
|
||||
41
internal/error_accumulator_test.go
Normal file
41
internal/error_accumulator_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package openai_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
utils "github.com/sashabaranov/go-openai/internal"
|
||||
"github.com/sashabaranov/go-openai/internal/test"
|
||||
)
|
||||
|
||||
func TestErrorAccumulatorBytes(t *testing.T) {
|
||||
accumulator := &utils.DefaultErrorAccumulator{
|
||||
Buffer: &bytes.Buffer{},
|
||||
}
|
||||
|
||||
errBytes := accumulator.Bytes()
|
||||
if len(errBytes) != 0 {
|
||||
t.Fatalf("Did not return nil with empty bytes: %s", string(errBytes))
|
||||
}
|
||||
|
||||
err := accumulator.Write([]byte("{}"))
|
||||
if err != nil {
|
||||
t.Fatalf("%+v", err)
|
||||
}
|
||||
|
||||
errBytes = accumulator.Bytes()
|
||||
if len(errBytes) == 0 {
|
||||
t.Fatalf("Did not return error bytes when has error: %s", string(errBytes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorByteWriteErrors(t *testing.T) {
|
||||
accumulator := &utils.DefaultErrorAccumulator{
|
||||
Buffer: &test.FailingErrorBuffer{},
|
||||
}
|
||||
err := accumulator.Write([]byte("{"))
|
||||
if !errors.Is(err, test.ErrTestErrorAccumulatorWriteFailed) {
|
||||
t.Fatalf("Did not return error when write failed: %v", err)
|
||||
}
|
||||
}
|
||||
21
internal/test/failer.go
Normal file
21
internal/test/failer.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package test
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrTestErrorAccumulatorWriteFailed = errors.New("test error accumulator failed")
|
||||
)
|
||||
|
||||
type FailingErrorBuffer struct{}
|
||||
|
||||
func (b *FailingErrorBuffer) Write(_ []byte) (n int, err error) {
|
||||
return 0, ErrTestErrorAccumulatorWriteFailed
|
||||
}
|
||||
|
||||
func (b *FailingErrorBuffer) Len() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (b *FailingErrorBuffer) Bytes() []byte {
|
||||
return []byte{}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package test
|
||||
import (
|
||||
"github.com/sashabaranov/go-openai/internal/test/checks"
|
||||
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
@@ -27,3 +28,26 @@ func CreateTestDirectory(t *testing.T) (path string, cleanup func()) {
|
||||
|
||||
return path, func() { os.RemoveAll(path) }
|
||||
}
|
||||
|
||||
// TokenRoundTripper is a struct that implements the RoundTripper
|
||||
// interface, specifically to handle the authentication token by adding a token
|
||||
// to the request header. We need this because the API requires that each
|
||||
// request include a valid API token in the headers for authentication and
|
||||
// authorization.
|
||||
type TokenRoundTripper struct {
|
||||
Token string
|
||||
Fallback http.RoundTripper
|
||||
}
|
||||
|
||||
// RoundTrip takes an *http.Request as input and returns an
|
||||
// *http.Response and an error.
|
||||
//
|
||||
// It is expected to use the provided request to create a connection to an HTTP
|
||||
// server and return the response, or an error if one occurred. The returned
|
||||
// Response should have its Body closed. If the RoundTrip method returns an
|
||||
// error, the Client's Get, Head, Post, and PostForm methods return the same
|
||||
// error.
|
||||
func (t *TokenRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("Authorization", "Bearer "+t.Token)
|
||||
return t.Fallback.RoundTrip(req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user