summaryrefslogtreecommitdiff
path: root/add.go
diff options
context:
space:
mode:
authorDavid Schlachter <t480-debian-git@schlachter.ca>2026-01-08 00:12:05 -0500
committerDavid Schlachter <t480-debian-git@schlachter.ca>2026-01-08 00:12:05 -0500
commite2b5d4e8bf68dc5b790a5424fbc30ebda46c2f04 (patch)
tree00b4a04cca2782aaaa1fd69bca9594a6d8943025 /add.go
parente3cc2555ec52312deb5498df07ffb8b6e03c7329 (diff)
Move card adding logic to new file
Diffstat (limited to 'add.go')
-rw-r--r--add.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/add.go b/add.go
new file mode 100644
index 0000000..40c45ee
--- /dev/null
+++ b/add.go
@@ -0,0 +1,59 @@
+package main
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+)
+
+const apiVersion = 6
+
+func addCard(c *http.Client, front, back string) error {
+ noteRequest := addNote{
+ Action: "addNote",
+ Version: apiVersion,
+ Params: addNoteParams{
+ Note: note{
+ DeckName: deckName,
+ ModelName: modelName,
+ Fields: fields{
+ Front: front,
+ Back: back,
+ },
+ Options: options{
+ AllowDuplicate: false,
+ DuplicateScope: "deck",
+ },
+ },
+ },
+ }
+
+ jsonBytes, err := json.Marshal(noteRequest)
+ if err != nil {
+ return fmt.Errorf("marshaling JSON: %s", err)
+ }
+
+ req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonBytes))
+ req.Header.Set("Content-Type", "application/json")
+
+ resp, err := c.Do(req)
+ if err != nil {
+ return fmt.Errorf("making request: %s", err)
+ }
+ defer resp.Body.Close()
+
+ body, _ := io.ReadAll(resp.Body)
+
+ var jsonResp struct {
+ Error string `json:"error"`
+ }
+
+ json.Unmarshal(body, &jsonResp)
+ if jsonResp.Error != "" {
+ return fmt.Errorf("creating card: %s", jsonResp.Error)
+ }
+
+ return nil
+}