feat: add remote NodeService client backed by the HTTP API
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
122
service/api_client.go
Normal file
122
service/api_client.go
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"axolotl/models"
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
type apiClient struct {
|
||||||
|
base string
|
||||||
|
user string
|
||||||
|
http *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *apiClient) User() string { return c.user }
|
||||||
|
|
||||||
|
func (c *apiClient) do(method, path string, body any) (*http.Response, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if body != nil {
|
||||||
|
if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest(method, c.base+path, &buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("X-Ax-User", c.user)
|
||||||
|
if body != nil {
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
return c.http.Do(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiDecode[T any](resp *http.Response) (T, error) {
|
||||||
|
var v T
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode >= 400 {
|
||||||
|
var e struct{ Error string }
|
||||||
|
json.NewDecoder(resp.Body).Decode(&e)
|
||||||
|
return v, fmt.Errorf("%s", e.Error)
|
||||||
|
}
|
||||||
|
return v, json.NewDecoder(resp.Body).Decode(&v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *apiClient) GetByID(id string) (*models.Node, error) {
|
||||||
|
resp, err := c.do("GET", "/nodes/"+id, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return apiDecode[*models.Node](resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *apiClient) List(filter ListFilter) ([]*models.Node, error) {
|
||||||
|
q := url.Values{}
|
||||||
|
for _, r := range filter.Rels {
|
||||||
|
if r.Target == "" {
|
||||||
|
q.Add("rel", string(r.Type))
|
||||||
|
} else {
|
||||||
|
q.Add("rel", string(r.Type)+":"+r.Target)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
path := "/nodes"
|
||||||
|
if len(q) > 0 {
|
||||||
|
path += "?" + q.Encode()
|
||||||
|
}
|
||||||
|
resp, err := c.do("GET", path, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return apiDecode[[]*models.Node](resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *apiClient) Add(input AddInput) (*models.Node, error) {
|
||||||
|
resp, err := c.do("POST", "/nodes", input)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return apiDecode[*models.Node](resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *apiClient) Update(id string, input UpdateInput) (*models.Node, error) {
|
||||||
|
resp, err := c.do("PATCH", "/nodes/"+id, input)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return apiDecode[*models.Node](resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *apiClient) Delete(id string) error {
|
||||||
|
resp, err := c.do("DELETE", "/nodes/"+id, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode >= 400 {
|
||||||
|
var e struct{ Error string }
|
||||||
|
json.NewDecoder(resp.Body).Decode(&e)
|
||||||
|
return fmt.Errorf("%s", e.Error)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *apiClient) ListUsers() ([]*models.Node, error) {
|
||||||
|
resp, err := c.do("GET", "/users", nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return apiDecode[[]*models.Node](resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *apiClient) AddUser(name string) (*models.Node, error) {
|
||||||
|
resp, err := c.do("POST", "/users", map[string]string{"name": name})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return apiDecode[*models.Node](resp)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -19,5 +19,7 @@ type Config interface {
|
|||||||
DeleteAlias(name string) error
|
DeleteAlias(name string) error
|
||||||
ListAliases() ([]*Alias, error)
|
ListAliases() ([]*Alias, error)
|
||||||
GetServerConfig() ServerConfig
|
GetServerConfig() ServerConfig
|
||||||
|
// GetRemoteConfig returns the remote server address and whether remote mode is enabled.
|
||||||
|
GetRemoteConfig() (ServerConfig, bool)
|
||||||
Save() error
|
Save() error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ type fileConfig struct {
|
|||||||
User string `json:"user"`
|
User string `json:"user"`
|
||||||
UserAliases []*Alias `json:"aliases"`
|
UserAliases []*Alias `json:"aliases"`
|
||||||
Serve ServerConfig `json:"serve"`
|
Serve ServerConfig `json:"serve"`
|
||||||
|
Remote ServerConfig `json:"remote"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultAliases = []*Alias{
|
var defaultAliases = []*Alias{
|
||||||
@@ -141,6 +142,17 @@ func (c *fileConfig) ListAliases() ([]*Alias, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *fileConfig) GetRemoteConfig() (ServerConfig, bool) {
|
||||||
|
if c.Remote.Host == "" {
|
||||||
|
return ServerConfig{}, false
|
||||||
|
}
|
||||||
|
port := c.Remote.Port
|
||||||
|
if port == 0 {
|
||||||
|
port = 7000
|
||||||
|
}
|
||||||
|
return ServerConfig{Host: c.Remote.Host, Port: port}, true
|
||||||
|
}
|
||||||
|
|
||||||
func (c *fileConfig) GetServerConfig() ServerConfig {
|
func (c *fileConfig) GetServerConfig() ServerConfig {
|
||||||
host := c.Serve.Host
|
host := c.Serve.Host
|
||||||
if host == "" {
|
if host == "" {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"axolotl/models"
|
"axolotl/models"
|
||||||
"axolotl/store"
|
"axolotl/store"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NodeService is the single entry point for all node operations.
|
// NodeService is the single entry point for all node operations.
|
||||||
@@ -76,6 +77,10 @@ func GetNodeService(cfg Config) (NodeService, error) {
|
|||||||
if user == "" {
|
if user == "" {
|
||||||
return nil, fmt.Errorf("no user configured: run 'ax user set <username>' first")
|
return nil, fmt.Errorf("no user configured: run 'ax user set <username>' first")
|
||||||
}
|
}
|
||||||
|
if rc, ok := cfg.GetRemoteConfig(); ok {
|
||||||
|
base := fmt.Sprintf("http://%s:%d", rc.Host, rc.Port)
|
||||||
|
return &apiClient{base: base, user: user, http: &http.Client{}}, nil
|
||||||
|
}
|
||||||
st, err := store.FindAndOpenSQLiteStore()
|
st, err := store.FindAndOpenSQLiteStore()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user