Skip to content

Commit

Permalink
SDK regeneration (#99)
Browse files Browse the repository at this point in the history
Co-authored-by: fern-api <115122769+fern-api[bot]@users.noreply.github.com>
  • Loading branch information
fern-api[bot] authored Jan 6, 2025
1 parent dfae687 commit 2adb6cd
Show file tree
Hide file tree
Showing 37 changed files with 14,249 additions and 10,976 deletions.
1,942 changes: 821 additions & 1,121 deletions client/client.go

Large diffs are not rendered by default.

700 changes: 700 additions & 0 deletions connectors.go

Large diffs are not rendered by default.

1,085 changes: 463 additions & 622 deletions connectors/client.go

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions core/api_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package core

import "fmt"

// APIError is a lightweight wrapper around the standard error
// interface that preserves the status code from the RPC, if any.
type APIError struct {
err error

StatusCode int `json:"-"`
}

// NewAPIError constructs a new API error.
func NewAPIError(statusCode int, err error) *APIError {
return &APIError{
err: err,
StatusCode: statusCode,
}
}

// Unwrap returns the underlying error. This also makes the error compatible
// with errors.As and errors.Is.
func (a *APIError) Unwrap() error {
if a == nil {
return nil
}
return a.err
}

// Error returns the API error's message.
func (a *APIError) Error() string {
if a == nil || (a.err == nil && a.StatusCode == 0) {
return ""
}
if a.err == nil {
return fmt.Sprintf("%d", a.StatusCode)
}
if a.StatusCode == 0 {
return a.err.Error()
}
return fmt.Sprintf("%d: %s", a.StatusCode, a.err.Error())
}
8 changes: 8 additions & 0 deletions core/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core

import "net/http"

// HTTPClient is an interface for a subset of the *http.Client.
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
3 changes: 2 additions & 1 deletion core/request_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func (r *RequestOptions) cloneHeader() http.Header {
headers := r.HTTPHeader.Clone()
headers.Set("X-Fern-Language", "Go")
headers.Set("X-Fern-SDK-Name", "github.com/cohere-ai/cohere-go/v2")
headers.Set("X-Fern-SDK-Version", "v2.12.3")
headers.Set("X-Fern-SDK-Version", "v2.12.4")
headers.Set("User-Agent", "github.com/cohere-ai/cohere-go/2.12.4")
return headers
}

Expand Down
128 changes: 12 additions & 116 deletions core/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,14 @@ package core

import (
"bufio"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
)

const (
// DefaultDataPrefix is the default prefix used for SSE streaming.
DefaultSSEDataPrefix = "data: "

// DefaultTerminator is the default terminator used for SSE streaming.
DefaultSSETerminator = "[DONE]"

// The default stream delimiter used to split messages.
defaultStreamDelimiter = '\n'
)

// Streamer calls APIs and streams responses using a *Stream.
type Streamer[T any] struct {
client HTTPClient
retrier *Retrier
}

// NewStreamer returns a new *Streamer backed by the given caller's HTTP client.
func NewStreamer[T any](caller *Caller) *Streamer[T] {
return &Streamer[T]{
client: caller.client,
retrier: caller.retrier,
}
}

// StreamParams represents the parameters used to issue an API streaming call.
type StreamParams struct {
URL string
Method string
Prefix string
Delimiter string
Terminator string
MaxAttempts uint
Headers http.Header
BodyProperties map[string]interface{}
QueryParameters url.Values
Client HTTPClient
Request interface{}
ErrorDecoder ErrorDecoder
}

// Stream issues an API streaming call according to the given stream parameters.
func (s *Streamer[T]) Stream(ctx context.Context, params *StreamParams) (*Stream[T], error) {
url := buildURL(params.URL, params.QueryParameters)
req, err := newRequest(
ctx,
url,
params.Method,
params.Headers,
params.Request,
params.BodyProperties,
)
if err != nil {
return nil, err
}

// If the call has been cancelled, don't issue the request.
if err := ctx.Err(); err != nil {
return nil, err
}

client := s.client
if params.Client != nil {
// Use the HTTP client scoped to the request.
client = params.Client
}

var retryOptions []RetryOption
if params.MaxAttempts > 0 {
retryOptions = append(retryOptions, WithMaxAttempts(params.MaxAttempts))
}

resp, err := s.retrier.Run(
client.Do,
req,
params.ErrorDecoder,
retryOptions...,
)
if err != nil {
return nil, err
}

// Check if the call was cancelled before we return the error
// associated with the call and/or unmarshal the response data.
if err := ctx.Err(); err != nil {
defer resp.Body.Close()
return nil, err
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
defer resp.Body.Close()
return nil, decodeError(resp, params.ErrorDecoder)
}

var opts []StreamOption
if params.Delimiter != "" {
opts = append(opts, WithDelimiter(params.Delimiter))
}
if params.Prefix != "" {
opts = append(opts, WithPrefix(params.Prefix))
}
if params.Terminator != "" {
opts = append(opts, WithTerminator(params.Terminator))
}

return NewStream[T](resp, opts...), nil
}
// defaultStreamDelimiter is the default stream delimiter used to split messages.
const defaultStreamDelimiter = '\n'

// Stream represents a stream of messages sent from a server.
type Stream[T any] struct {
Expand Down Expand Up @@ -262,18 +155,21 @@ func (s *scannerStreamReader) ReadFromStream() ([]byte, error) {
}

func (s *scannerStreamReader) parse(bytes []byte) (int, []byte, error) {
var start int
var startIndex int
if s.options != nil && s.options.prefix != "" {
if i := strings.Index(string(bytes), s.options.prefix); i >= 0 {
start = i + len(s.options.prefix)
startIndex = i + len(s.options.prefix)
}
}
data := bytes[start:]
if i := strings.Index(string(data), s.options.delimiter); i >= 0 {
data = data[:i+len(s.options.delimiter)]
data := bytes[startIndex:]
delimIndex := strings.Index(string(data), s.options.delimiter)
if delimIndex < 0 {
return startIndex + len(data), data, nil
}
n := start + len(data) + len(s.options.delimiter)
return n, data, nil
endIndex := delimIndex + len(s.options.delimiter)
parsedData := data[:endIndex]
n := startIndex + endIndex
return n, parsedData, nil
}

func (s *scannerStreamReader) isTerminated(bytes []byte) bool {
Expand Down
Loading

0 comments on commit 2adb6cd

Please sign in to comment.