mirror of
https://github.com/icereed/paperless-gpt.git
synced 2025-03-12 21:08:00 -05:00
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
34 lines
944 B
Go
34 lines
944 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// HttpTransportWithBearer wraps the default RoundTripper to add the Authorization header.
|
|
type HttpTransportWithBearer struct {
|
|
BaseTransport http.RoundTripper
|
|
Token string
|
|
}
|
|
|
|
// RoundTrip implements the RoundTripper interface to modify the request.
|
|
func (t *HttpTransportWithBearer) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
// Clone the request to avoid side effects
|
|
reqClone := req.Clone(req.Context())
|
|
|
|
// Add the Authorization header
|
|
reqClone.Header.Set("Authorization", fmt.Sprintf("Bearer %s", t.Token))
|
|
|
|
// Use the base RoundTripper to perform the request
|
|
return t.BaseTransport.RoundTrip(reqClone)
|
|
}
|
|
|
|
func NewHttpClientWithBearerTransport(token string) *http.Client {
|
|
// Create a new HTTP client with the custom transport
|
|
return &http.Client{
|
|
Transport: &HttpTransportWithBearer{
|
|
BaseTransport: http.DefaultTransport,
|
|
Token: token,
|
|
},
|
|
}
|
|
}
|