Skip to content

Commit 45455c2

Browse files
committed
update accounts and farms to filter and update with options
1 parent 6128abb commit 45455c2

File tree

3 files changed

+288
-75
lines changed

3 files changed

+288
-75
lines changed

node-registrar/client/account.go

Lines changed: 90 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@ func (c RegistrarClient) GetAccountByPK(pk []byte) (account Account, err error)
2626
return c.getAccountByPK(pk)
2727
}
2828

29-
func (c RegistrarClient) UpdateAccount(relays []string, rmbEncKey string) (err error) {
30-
return c.updateAccount(relays, rmbEncKey)
29+
func (c RegistrarClient) UpdateAccount(opts ...UpdateAccountOpts) (err error) {
30+
return c.updateAccount(opts)
3131
}
3232

3333
func (c RegistrarClient) EnsureAccount(pk []byte, relays []string, rmbEncKey string) (account Account, err error) {
3434
return c.ensureAccount(pk, relays, rmbEncKey)
3535
}
3636

37-
func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (result Account, err error) {
37+
func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (account Account, err error) {
3838
url, err := url.JoinPath(c.baseURL, "accounts")
3939
if err != nil {
40-
return result, errors.Wrap(err, "failed to construct registrar url")
40+
return account, errors.Wrap(err, "failed to construct registrar url")
4141
}
4242

4343
publicKeyBase64 := base64.StdEncoding.EncodeToString(c.keyPair.publicKey)
4444

4545
timestamp := time.Now().Unix()
4646
signature := c.signRequest(timestamp)
4747

48-
account := map[string]any{
48+
data := map[string]any{
4949
"public_key": publicKeyBase64,
5050
"signature": signature,
5151
"timestamp": timestamp,
@@ -54,25 +54,25 @@ func (c *RegistrarClient) createAccount(relays []string, rmbEncKey string) (resu
5454
}
5555

5656
var body bytes.Buffer
57-
err = json.NewEncoder(&body).Encode(account)
57+
err = json.NewEncoder(&body).Encode(data)
5858
if err != nil {
59-
return result, errors.Wrap(err, "failed to parse request body")
59+
return account, errors.Wrap(err, "failed to parse request body")
6060
}
6161

6262
resp, err := c.httpClient.Post(url, "application/json", &body)
6363
if err != nil {
64-
return result, errors.Wrap(err, "failed to send request to the registrar")
64+
return account, errors.Wrap(err, "failed to send request to the registrar")
6565
}
6666

6767
if resp.StatusCode != http.StatusCreated {
6868
err = parseResponseError(resp.Body)
69-
return result, errors.Wrapf(err, "failed to create account with status %s", resp.Status)
69+
return account, errors.Wrapf(err, "failed to create account with status %s", resp.Status)
7070
}
7171
defer resp.Body.Close()
7272

73-
err = json.NewDecoder(resp.Body).Decode(&result)
73+
err = json.NewDecoder(resp.Body).Decode(&account)
7474

75-
c.twinID = result.TwinID
75+
c.twinID = account.TwinID
7676
return
7777
}
7878

@@ -114,24 +114,57 @@ func (c RegistrarClient) getAccount(id uint64) (account Account, err error) {
114114
return
115115
}
116116

117-
func (c RegistrarClient) updateAccount(relays []string, rmbEncKey string) (err error) {
118-
url, err := url.JoinPath(c.baseURL, "accounts", fmt.Sprint(c.twinID))
117+
func (c RegistrarClient) getAccountByPK(pk []byte) (account Account, err error) {
118+
url, err := url.JoinPath(c.baseURL, "accounts")
119119
if err != nil {
120-
return errors.Wrap(err, "failed to construct registrar url")
120+
return account, errors.Wrap(err, "failed to construct registrar url")
121+
}
122+
123+
publicKeyBase64 := base64.StdEncoding.EncodeToString(pk)
124+
125+
req, err := http.NewRequest("GET", url, nil)
126+
if err != nil {
127+
return account, err
128+
}
129+
130+
q := req.URL.Query()
131+
q.Add("public_key", publicKeyBase64)
132+
req.URL.RawQuery = q.Encode()
133+
134+
resp, err := c.httpClient.Do(req)
135+
if err != nil {
136+
return account, err
121137
}
122138

123-
acc := map[string]any{}
139+
if resp == nil {
140+
return account, errors.New("no response received")
141+
}
142+
defer resp.Body.Close()
124143

125-
if len(relays) != 0 {
126-
acc["relays"] = relays
144+
if resp.StatusCode == http.StatusNotFound {
145+
return account, ErrorAccountNotFround
127146
}
128147

129-
if len(rmbEncKey) != 0 {
130-
acc["rmb_enc_key"] = rmbEncKey
148+
if resp.StatusCode != http.StatusOK {
149+
err = parseResponseError(resp.Body)
150+
return account, errors.Wrapf(err, "failed to get account by public_key with status code %s", resp.Status)
151+
}
152+
153+
err = json.NewDecoder(resp.Body).Decode(&account)
154+
155+
return account, err
156+
}
157+
158+
func (c RegistrarClient) updateAccount(opts []UpdateAccountOpts) (err error) {
159+
url, err := url.JoinPath(c.baseURL, "accounts", fmt.Sprint(c.twinID))
160+
if err != nil {
161+
return errors.Wrap(err, "failed to construct registrar url")
131162
}
132163

133164
var body bytes.Buffer
134-
err = json.NewEncoder(&body).Encode(acc)
165+
data := parseUpdateAccountOpts(opts)
166+
167+
err = json.NewEncoder(&body).Encode(data)
135168
if err != nil {
136169
return errors.Wrap(err, "failed to parse request body")
137170
}
@@ -162,45 +195,25 @@ func (c RegistrarClient) updateAccount(relays []string, rmbEncKey string) (err e
162195
return
163196
}
164197

165-
func (c RegistrarClient) getAccountByPK(pk []byte) (account Account, err error) {
166-
url, err := url.JoinPath(c.baseURL, "accounts")
167-
if err != nil {
168-
return account, errors.Wrap(err, "failed to construct registrar url")
169-
}
170-
171-
publicKeyBase64 := base64.StdEncoding.EncodeToString(pk)
172-
173-
req, err := http.NewRequest("GET", url, nil)
174-
if err != nil {
175-
return account, err
176-
}
177-
178-
q := req.URL.Query()
179-
q.Add("public_key", publicKeyBase64)
180-
req.URL.RawQuery = q.Encode()
181-
182-
resp, err := c.httpClient.Do(req)
183-
if err != nil {
184-
return account, err
185-
}
198+
type accountCfg struct {
199+
relays []string
200+
rmbEncKey string
201+
}
186202

187-
if resp == nil {
188-
return account, errors.New("no response received")
189-
}
190-
defer resp.Body.Close()
203+
type (
204+
UpdateAccountOpts func(*accountCfg)
205+
)
191206

192-
if resp.StatusCode == http.StatusNotFound {
193-
return account, ErrorAccountNotFround
207+
func UpdateAccountWithRelays(relays []string) UpdateAccountOpts {
208+
return func(n *accountCfg) {
209+
n.relays = relays
194210
}
211+
}
195212

196-
if resp.StatusCode != http.StatusOK {
197-
err = parseResponseError(resp.Body)
198-
return account, errors.Wrapf(err, "failed to get account by public_key with status code %s", resp.Status)
213+
func UpdateAccountWithRMBEncKey(rmbEncKey string) UpdateAccountOpts {
214+
return func(n *accountCfg) {
215+
n.rmbEncKey = rmbEncKey
199216
}
200-
201-
err = json.NewDecoder(resp.Body).Decode(&account)
202-
203-
return account, err
204217
}
205218

206219
func (c RegistrarClient) ensureAccount(pk []byte, relays []string, rmbEncKey string) (account Account, err error) {
@@ -227,3 +240,26 @@ func (c *RegistrarClient) ensureTwinID() error {
227240
c.twinID = twin.TwinID
228241
return nil
229242
}
243+
244+
func parseUpdateAccountOpts(opts []UpdateAccountOpts) map[string]any {
245+
cfg := accountCfg{
246+
rmbEncKey: "",
247+
relays: []string{},
248+
}
249+
250+
for _, opt := range opts {
251+
opt(&cfg)
252+
}
253+
254+
data := map[string]any{}
255+
256+
if len(cfg.relays) != 0 {
257+
data["relays"] = cfg.relays
258+
}
259+
260+
if len(cfg.rmbEncKey) != 0 {
261+
data["rmb_enc_key"] = cfg.rmbEncKey
262+
}
263+
264+
return data
265+
}

node-registrar/client/farm.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ func (c RegistrarClient) createFarm(farmName string, twinID uint64, dedicated bo
9797
return farmID, errors.Wrap(err, "failed to construct registrar url")
9898
}
9999

100-
data := map[string]any{
101-
"farm_name": farmName,
102-
"twin_id": twinID,
103-
"dedicated": dedicated,
100+
data := Farm{
101+
FarmName: farmName,
102+
TwinID: twinID,
103+
Dedicated: dedicated,
104104
}
105105

106106
var body bytes.Buffer
@@ -129,12 +129,15 @@ func (c RegistrarClient) createFarm(farmName string, twinID uint64, dedicated bo
129129
return farmID, fmt.Errorf("failed to create farm with status code %s", resp.Status)
130130
}
131131

132-
var result uint64
132+
result := struct {
133+
FarmID uint64 `json:"farm_id"`
134+
}{}
135+
133136
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
134137
return farmID, errors.Wrap(err, "failed to decode response body")
135138
}
136139

137-
return result, nil
140+
return result.FarmID, nil
138141
}
139142

140143
func (c RegistrarClient) updateFarm(farmID uint64, opts []UpdateFarmOpts) (err error) {
@@ -144,7 +147,7 @@ func (c RegistrarClient) updateFarm(farmID uint64, opts []UpdateFarmOpts) (err e
144147
}
145148

146149
var body bytes.Buffer
147-
data := parseUpdateFarmOpts(opts...)
150+
data := parseUpdateFarmOpts(opts)
148151

149152
err = json.NewEncoder(&body).Encode(data)
150153
if err != nil {
@@ -215,16 +218,17 @@ func (c RegistrarClient) listFarms(opts ...ListFarmOpts) (farms []Farm, err erro
215218

216219
data := parseListFarmOpts(opts)
217220

218-
var body bytes.Buffer
219-
err = json.NewEncoder(&body).Encode(data)
221+
req, err := http.NewRequest("GET", url, nil)
220222
if err != nil {
221-
return farms, errors.Wrap(err, "failed to encode request body")
223+
return farms, errors.Wrap(err, "failed to construct http request to the registrar")
222224
}
223225

224-
req, err := http.NewRequest("GET", url, &body)
225-
if err != nil {
226-
return farms, errors.Wrap(err, "failed to construct http request to the registrar")
226+
q := req.URL.Query()
227+
228+
for key, val := range data {
229+
q.Add(key, fmt.Sprint(val))
227230
}
231+
req.URL.RawQuery = q.Encode()
228232

229233
resp, err := c.httpClient.Do(req)
230234
if err != nil {
@@ -282,7 +286,7 @@ func parseListFarmOpts(opts []ListFarmOpts) map[string]any {
282286
return data
283287
}
284288

285-
func parseUpdateFarmOpts(opts ...UpdateFarmOpts) map[string]any {
289+
func parseUpdateFarmOpts(opts []UpdateFarmOpts) map[string]any {
286290
cfg := farmCfg{
287291
farmName: "",
288292
dedicated: false,

0 commit comments

Comments
 (0)