/
api_stream.go
93 lines (79 loc) · 2.38 KB
/
api_stream.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
90
91
92
93
package ceramic
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const (
COMMIT_GENESIS uint = iota
COMMIT_SIGNED
COMMIT_ANCHOR
)
const (
SIGNATURE_STATUS_GENESIS uint = iota
SIGNATURE_STATUS_PARTIAL
SIGNATURE_STATUS_SIGNED
)
type CeramicAnchorProof struct {
BlockNumber uint `json:"blockNumber"`
BlockTimestamp uint `json:"blockTimestamp"`
ChainId string `json:"chainId"`
Root string `json:"root"`
TxHash string `json:"txHash"`
}
type CeramicLogEntry struct {
CID string `json:"cid"`
Timestamp *uint `json:"timestamp,omitempty"`
Type uint `json:"type"`
}
type CeramicStreamMetadata struct {
Controllers []string `json:"controllers"`
Family *string `json:"family,omitempty"`
ForbidControllerChange *bool `json:"forbidControllerChange,omitempty"`
Schema *string `json:"schema,omitempty"`
Tags []string `json:"tags,omitempty"`
}
type CeramicStreamNext struct {
Content *json.RawMessage `json:"content,omitempty"`
Controllers []string `json:"controllers,omitempty"`
Metadata *CeramicStreamMetadata `json:"metadata,omitempty"`
}
// https://developers.ceramic.network/reference/typescript/interfaces/_ceramicnetwork_common.streamstate-1.html
type CeramicStreamState struct {
AnchorProof *CeramicAnchorProof `json:"anchorProof,omitempty"`
// AnchorStatus not supoorted yet
Content json.RawMessage `json:"content"`
Log []CeramicLogEntry `json:"log"`
Metadata CeramicStreamMetadata `json:"metadata"`
Next *CeramicStreamNext `json:"next,omitempty"`
SignatureStatus uint `json:"signature"`
Type int `json:"type"`
}
type CeramicGetResponse struct {
StreamID string `json:"streamId"`
State CeramicStreamState `json:"state"`
}
func (a *API) GetStream(streamid string) (*CeramicGetResponse, error) {
handle := url.URL{
Host: a.host,
Scheme: a.scheme,
Path: fmt.Sprintf("%s/api/v0/streams/%s", a.root, streamid),
}
resp, err := http.Get(handle.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
res := CeramicGetResponse{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &res)
if err != nil {
return nil, err
}
return &res, nil
}