/
name.go
89 lines (78 loc) · 2.02 KB
/
name.go
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
package w3s
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
ipns_pb "github.com/ipfs/boxo/ipns/pb"
)
func (c *client) GetName(ctx context.Context, name string) (*ipns_pb.IpnsEntry, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/name/%s", "https://name.web3.storage", name), nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.cfg.token))
req.Header.Add("X-Client", clientName)
res, err := c.cfg.hc.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode == 400 || res.StatusCode == 404 {
return nil, err
} else if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP response status: %s", res.Status)
}
d := json.NewDecoder(res.Body)
var out struct {
Value string `json:"value"`
Record string `json:"record"`
}
err = d.Decode(&out)
if err != nil {
return nil, err
}
ll, err := base64.StdEncoding.WithPadding(base64.StdPadding).DecodeString(out.Record)
if err != nil {
return nil, err
}
n := ipns_pb.IpnsEntry{}
err = n.Unmarshal(ll)
return &n, err
}
func (c *client) PutName(ctx context.Context, record *ipns_pb.IpnsEntry, name string) error {
b, err := record.Marshal()
if err != nil {
return err
}
s := base64.StdEncoding.WithPadding(base64.StdPadding).EncodeToString(b)
//w.Write(b)
//w.Close()
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/name/%s", "https://name.web3.storage", name), strings.NewReader(s))
if err != nil {
return err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.cfg.token))
req.Header.Add("X-Client", clientName)
res, err := c.cfg.hc.Do(req)
if err != nil {
return err
}
if res.StatusCode != 200 {
//var b bytes.Buffer
b, _ := io.ReadAll(res.Body)
return fmt.Errorf("HTTP response status: %s %s", res.Status, string(b))
}
d := json.NewDecoder(res.Body)
var out struct {
Value string `json:"value"`
Record string `json:"record"`
}
err = d.Decode(&out)
if err != nil {
return err
}
return err
}