File size: 3,144 Bytes
7def60a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package clients

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

// Define a struct to hold the store API client
type StoreClient struct {
	BaseURL string
	Client  *http.Client
}

type SetRequest struct {
	Keys   [][]float32 `json:"keys"`
	Values []string    `json:"values"`
}

type GetRequest struct {
	Keys [][]float32 `json:"keys"`
}

type GetResponse struct {
	Keys   [][]float32 `json:"keys"`
	Values []string    `json:"values"`
}

type DeleteRequest struct {
	Keys [][]float32 `json:"keys"`
}

type FindRequest struct {
	TopK int       `json:"topk"`
	Key  []float32 `json:"key"`
}

type FindResponse struct {
	Keys         [][]float32 `json:"keys"`
	Values       []string    `json:"values"`
	Similarities []float32   `json:"similarities"`
}

// Constructor for StoreClient
func NewStoreClient(baseUrl string) *StoreClient {
	return &StoreClient{
		BaseURL: baseUrl,
		Client:  &http.Client{},
	}
}

// Implement Set method
func (c *StoreClient) Set(req SetRequest) error {
	return c.doRequest("stores/set", req)
}

// Implement Get method
func (c *StoreClient) Get(req GetRequest) (*GetResponse, error) {
	body, err := c.doRequestWithResponse("stores/get", req)
	if err != nil {
		return nil, err
	}

	var resp GetResponse
	err = json.Unmarshal(body, &resp)
	if err != nil {
		return nil, err
	}

	return &resp, nil
}

// Implement Delete method
func (c *StoreClient) Delete(req DeleteRequest) error {
	return c.doRequest("stores/delete", req)
}

// Implement Find method
func (c *StoreClient) Find(req FindRequest) (*FindResponse, error) {
	body, err := c.doRequestWithResponse("stores/find", req)
	if err != nil {
		return nil, err
	}

	var resp FindResponse
	err = json.Unmarshal(body, &resp)
	if err != nil {
		return nil, err
	}

	return &resp, nil
}

// Helper function to perform a request without expecting a response body
func (c *StoreClient) doRequest(path string, data interface{}) error {
	jsonData, err := json.Marshal(data)
	if err != nil {
		return err
	}

	req, err := http.NewRequest("POST", c.BaseURL+"/"+path, bytes.NewBuffer(jsonData))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := c.Client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("API request to %s failed with status code %d", path, resp.StatusCode)
	}

	return nil
}

// Helper function to perform a request and parse the response body
func (c *StoreClient) doRequestWithResponse(path string, data interface{}) ([]byte, error) {
	jsonData, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}

	req, err := http.NewRequest("POST", c.BaseURL+"/"+path, bytes.NewBuffer(jsonData))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := c.Client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("API request to %s failed with status code %d", path, resp.StatusCode)
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	return body, nil
}