-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
44 lines (39 loc) · 933 Bytes
/
metadata.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
package hashkeydid_go
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
const metaDataUrl = "https://api.hashkey.id/did/api/nft/metadata/%s"
type Metadata struct {
Name string `json:"name"`
Description string `json:"description"`
Image string `json:"image"`
}
// GetMetadataImage returns the image url in metadata by tokenId
func GetMetadataImage(tokenId string) (string, error) {
metadata, err := GetMetadata(tokenId)
if err != nil {
return "", err
}
return metadata.Image, nil
}
// GetMetadata returns the Metadata by tokenId
func GetMetadata(tokenId string) (*Metadata, error) {
client := &http.Client{}
res, err := client.Get(fmt.Sprintf(metaDataUrl, tokenId))
if err != nil {
return nil, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var metadata Metadata
err = json.Unmarshal(body, &metadata)
if err != nil {
return nil, err
}
return &metadata, nil
}