mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-14 12:23:41 +08:00
✨ add transcriptions api
This commit is contained in:
@@ -27,13 +27,13 @@ func init() {
|
||||
|
||||
type Client struct {
|
||||
requestBuilder RequestBuilder
|
||||
createFormBuilder func(io.Writer) FormBuilder
|
||||
CreateFormBuilder func(io.Writer) FormBuilder
|
||||
}
|
||||
|
||||
func NewClient() *Client {
|
||||
return &Client{
|
||||
requestBuilder: NewRequestBuilder(),
|
||||
createFormBuilder: func(body io.Writer) FormBuilder {
|
||||
CreateFormBuilder: func(body io.Writer) FormBuilder {
|
||||
return NewFormBuilder(body)
|
||||
},
|
||||
}
|
||||
@@ -46,6 +46,10 @@ type requestOptions struct {
|
||||
|
||||
type requestOption func(*requestOptions)
|
||||
|
||||
type Stringer interface {
|
||||
GetString() *string
|
||||
}
|
||||
|
||||
func WithBody(body any) requestOption {
|
||||
return func(args *requestOptions) {
|
||||
args.body = body
|
||||
@@ -60,6 +64,12 @@ func WithHeader(header map[string]string) requestOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithContentType(contentType string) requestOption {
|
||||
return func(args *requestOptions) {
|
||||
args.header.Set("Content-Type", contentType)
|
||||
}
|
||||
}
|
||||
|
||||
type RequestError struct {
|
||||
HTTPStatusCode int
|
||||
Err error
|
||||
@@ -173,6 +183,11 @@ func DecodeResponse(body io.Reader, v any) error {
|
||||
if result, ok := v.(*string); ok {
|
||||
return DecodeString(body, result)
|
||||
}
|
||||
|
||||
if stringer, ok := v.(Stringer); ok {
|
||||
return DecodeString(body, stringer.GetString())
|
||||
}
|
||||
|
||||
return json.NewDecoder(body).Decode(v)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,11 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type FormBuilder interface {
|
||||
CreateFormFile(fieldname string, file *os.File) error
|
||||
CreateFormFile(fieldname string, fileHeader *multipart.FileHeader) error
|
||||
CreateFormFileReader(fieldname string, r io.Reader, filename string) error
|
||||
WriteField(fieldname, value string) error
|
||||
Close() error
|
||||
@@ -26,8 +25,15 @@ func NewFormBuilder(body io.Writer) *DefaultFormBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
func (fb *DefaultFormBuilder) CreateFormFile(fieldname string, file *os.File) error {
|
||||
return fb.createFormFile(fieldname, file, file.Name())
|
||||
func (fb *DefaultFormBuilder) CreateFormFile(fieldname string, fileHeader *multipart.FileHeader) error {
|
||||
file, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
return fb.createFormFile(fieldname, file, fileHeader.Filename)
|
||||
}
|
||||
|
||||
func (fb *DefaultFormBuilder) CreateFormFileReader(fieldname string, r io.Reader, filename string) error {
|
||||
|
||||
@@ -3,9 +3,10 @@ package common
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func UnmarshalBodyReusable(c *gin.Context, v any) error {
|
||||
@@ -20,9 +21,9 @@ func UnmarshalBodyReusable(c *gin.Context, v any) error {
|
||||
contentType := c.Request.Header.Get("Content-Type")
|
||||
if strings.HasPrefix(contentType, "application/json") {
|
||||
err = json.Unmarshal(requestBody, &v)
|
||||
} else {
|
||||
// skip for now
|
||||
// TODO: someday non json request have variant model, we will need to implementation this
|
||||
} else if strings.HasPrefix(contentType, "multipart/form-data") {
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
|
||||
err = c.ShouldBind(v)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user