feat: correctly implement bitbucket & add OpenAIAdapter

This commit is contained in:
u80864958
2026-02-13 13:54:57 +01:00
parent b67125024c
commit 2cb64194b9
13 changed files with 408 additions and 91 deletions

View File

@@ -1,6 +1,9 @@
package baseadapter
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
@@ -12,17 +15,45 @@ type Rest struct {
bearerToken string
}
func (b *Rest) createRequest(method string, body io.Reader, path ...string) (r *http.Request, err error) {
func NewRest(baseURL string, bearerToken string) Rest {
return Rest{
baseURL: baseURL,
bearerToken: bearerToken,
}
}
const defaultBodyBufferSize = 100
func (b *Rest) CreateRequest(ctx context.Context, method string, body any, path ...string) (r *http.Request, err error) {
target, err := url.JoinPath(b.baseURL, path...)
if err != nil {
err = fmt.Errorf("can not parse path: %w", err)
return
}
req, err := http.NewRequest(method, target, body)
var bodyReader io.Reader
if body != nil {
bodyBuff := bytes.NewBuffer(make([]byte, 0, defaultBodyBufferSize))
err = json.NewEncoder(bodyBuff).Encode(body)
if err != nil {
return
}
bodyReader = bodyBuff
}
req, err := http.NewRequest(method, target, bodyReader)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if b.bearerToken != "" {
req.Header.Set("Authorization", "Bearer "+b.bearerToken)
}