diff options
Diffstat (limited to 'add.go')
| -rw-r--r-- | add.go | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -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 +} |
