mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 17:16:38 +08:00
* Updated relay/channel/gemini package to use gin-gonic/gin for routing * Added timeouts, environment variable for proxy, and error handling for HTTP clients in relay/util/init.go * Improved error handling, URL path cases, and channel selection logic in middleware/distributor.go * Added Content-Type header, closed request bodies, and added context to requests in relay/channel/common.go * Upgraded various dependencies in go.mod * Modified the GetRequestURL method in relay/channel/gemini/adaptor.go to include a "key" parameter in the URL and set a default version of "v1beta" * Added io and net/http packages in relay/channel/gemini/adaptor.go and relay/channel/gemini/main.go * Added a struct for GeminiStreamResp and modified response handling in relay/channel/gemini/main.go * Imported packages for io and net/http added, gin-gonic/gin package added, and error handling improved in relay/channel/gemini/main.go
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package channel
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/Laisky/errors/v2"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/songquanpeng/one-api/relay/util"
|
|
)
|
|
|
|
func SetupCommonRequestHeader(c *gin.Context, req *http.Request, meta *util.RelayMeta) {
|
|
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
|
|
req.Header.Set("Accept", c.Request.Header.Get("Accept"))
|
|
if meta.IsStream && c.Request.Header.Get("Accept") == "" {
|
|
req.Header.Set("Accept", "text/event-stream")
|
|
}
|
|
}
|
|
|
|
func DoRequestHelper(a Adaptor, c *gin.Context, meta *util.RelayMeta, requestBody io.Reader) (*http.Response, error) {
|
|
fullRequestURL, err := a.GetRequestURL(meta)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "get request url failed")
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(c.Request.Context(),
|
|
c.Request.Method, fullRequestURL, requestBody)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "new request failed")
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
err = a.SetupRequestHeader(c, req, meta)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "setup request header failed")
|
|
}
|
|
resp, err := DoRequest(c, req)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "do request failed")
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func DoRequest(c *gin.Context, req *http.Request) (*http.Response, error) {
|
|
resp, err := util.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp == nil {
|
|
return nil, errors.New("resp is nil")
|
|
}
|
|
_ = req.Body.Close()
|
|
_ = c.Request.Body.Close()
|
|
|
|
return resp, nil
|
|
}
|