Files
LocalAGI/pkg/client/client.go
Ettore Di Giacinto e32a569796 try to fixup tests, enable e2e (#53)
* try to fixup tests, enable e2e

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Generate JSON character data with tools

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Rework generation of character

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Simplify text

Signed-off-by: mudler <mudler@localai.io>

* Relax some test constraints

Signed-off-by: mudler <mudler@localai.io>

* Fixups

* Properly fit schema generation

* Swap default model

* ci fixups

---------

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: mudler <mudler@localai.io>
2025-03-18 23:28:02 +01:00

74 lines
1.6 KiB
Go

package localagent
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// Client represents a client for the LocalAgent API
type Client struct {
BaseURL string
APIKey string
HTTPClient *http.Client
}
// NewClient creates a new LocalAgent client
func NewClient(baseURL string, apiKey string) *Client {
return &Client{
BaseURL: baseURL,
APIKey: apiKey,
HTTPClient: &http.Client{
Timeout: time.Second * 30,
},
}
}
// SetTimeout sets the HTTP client timeout
func (c *Client) SetTimeout(timeout time.Duration) {
c.HTTPClient.Timeout = timeout
}
// doRequest performs an HTTP request and returns the response
func (c *Client) doRequest(method, path string, body interface{}) (*http.Response, error) {
var reqBody io.Reader
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("error marshaling request body: %w", err)
}
reqBody = bytes.NewBuffer(jsonData)
}
url := fmt.Sprintf("%s%s", c.BaseURL, path)
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
if c.APIKey != "" {
req.Header.Set("Authorization", "Bearer "+c.APIKey)
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
if resp.StatusCode >= 400 {
// Read the error response
defer resp.Body.Close()
errorData, _ := io.ReadAll(resp.Body)
return resp, fmt.Errorf("api error (status %d): %s", resp.StatusCode, string(errorData))
}
return resp, nil
}