-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmochi.go
168 lines (150 loc) · 4.09 KB
/
mochi.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type MochiClient struct {
client *http.Client
key string
}
func NewMochiClient(key string) *MochiClient {
return &MochiClient{
client: &http.Client{},
key: key,
}
}
func (mc *MochiClient) ListDecks() ([]Deck, error) {
decks := Pagination[Deck]{}
if err := mc.getJSON("https://app.mochi.cards/api/decks", &decks); err != nil {
return nil, err
}
return decks.Docs, nil
}
func (mc *MochiClient) postJSON(path string, payload any, into any) error {
req, err := http.NewRequest("POST", path, nil)
if err != nil {
return err
}
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
if err := enc.Encode(payload); err != nil {
return err
}
req.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(mc.key, "")
resp, err := mc.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("failed: %s", string(bodyText))
}
return json.Unmarshal(bodyText, into)
}
func (mc *MochiClient) getJSON(path string, into any) error {
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return err
}
req.SetBasicAuth(mc.key, "")
resp, err := mc.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
bodyText, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(bodyText, into)
}
func (mc *MochiClient) CreateCard(card Card) (*Card, error) {
var result Card
if err := mc.postJSON("https://app.mochi.cards/api/cards", card, &result); err != nil {
return &result, err
}
return &result, nil
}
type Pagination[T any] struct {
Bookmark string `json:"bookmark"`
Docs []T `json:"docs"`
}
type Deck struct {
Name string `json:"name"`
ID string `json:"id"`
AnkiDeckID string `json:"anki/deck-id"`
Sort int `json:"sort"`
CardsView string `json:"cards-view"`
TemplateID string `json:"template-id"`
Archived bool `json:"archived?"`
}
type MochiTime struct {
Date time.Time `json:"date"`
}
type Field struct {
ID string `json:"id"`
Value string `json:"value"`
}
type Card struct {
ID string `json:"id,omitempty"`
Content string `json:"content,omitempty"`
DeckID string `json:"deck-id"`
TemplateID string `json:"template-id"`
Fields map[string]Field `json:"fields"`
ReviewReverse bool `json:"review-reverse?"`
Archived bool `json:"archived?"`
Pos string `json:"pos,omitempty"`
UpdatedAt *MochiTime `json:"updated-at,omitempty"`
Tags []string `json:"tags,omitempty"`
Name *string `json:"name,omitempty"`
Reviews []any `json:"reviews"`
CreatedAt *MochiTime `json:"created-at,omitempty"`
New *bool `json:"new?,omitempty"`
}
type Template struct {
ID string `json:"id"`
Name string `json:"name"`
Content string `json:"content"`
Pos string `json:"pos"`
Fields map[string]struct {
ID string `json:"id"`
Name string `json:"name"`
Pos string `json:"pos"`
Options struct {
MultiLine bool `json:"multi-line?"`
} `json:"options"`
} `json:"fields"`
}
func (mc *MochiClient) ListTemplates() ([]Template, error) {
decks := Pagination[Template]{}
if err := mc.getJSON("https://app.mochi.cards/api/templates", &decks); err != nil {
return nil, err
}
return decks.Docs, nil
}
func (mc *MochiClient) ListAllCards() ([]Card, error) {
decks := Pagination[Card]{}
if err := mc.getJSON("https://app.mochi.cards/api/cards", &decks); err != nil {
return nil, err
}
return decks.Docs, nil
}
func (mc *MochiClient) ListCardsInDeck(deckID string) ([]Card, error) {
decks := Pagination[Card]{}
// TODO uriencode
url := fmt.Sprintf("https://app.mochi.cards/api/cards?deck-id=%s", deckID)
if err := mc.getJSON(url, &decks); err != nil {
return nil, err
}
return decks.Docs, nil
}