From f0b85f6f646b34c4d5e24076ff551c6c3df71171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Mon, 17 Jun 2024 14:02:25 +0200 Subject: [PATCH] Implement PATCH-ing to the api --- api.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ download.go | 10 +++++++ main.go | 9 +++---- 3 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 api.go create mode 100644 download.go diff --git a/api.go b/api.go new file mode 100644 index 0000000..68db7d5 --- /dev/null +++ b/api.go @@ -0,0 +1,75 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "log" + "net/http" +) + +type APIError struct { + Code int `json:"code"` + Message string `json:"message"` + Data APIErrorData `json:"data"` +} + +type APIErrorData struct { + Link APIErrorLink `json:"link"` +} + +type APIErrorLink struct { + Code string `json:"code"` + Message string `json:"message"` +} + +func SetDownloaded(item PBEvent) (err error) { + req, err := http.NewRequestWithContext(context.Background(), "PATCH", FULL_URL+"/"+item.Record.Id, nil) + if err != nil { + log.Printf("Error creating PATCH request: %++v", err) + return err + } + req.Header.Set("Content-Type", "application/json") + + partialItem := new(PBEvent) + partialItem.Record = item.Record + partialItem.Record.Downloaded = true + + body, err := json.Marshal(partialItem.Record) + if err != nil { + log.Printf("Error marshalling subscription body: %++v", err) + return err + } + req.Body = io.NopCloser(bytes.NewReader(body)) + + client := http.Client{} + res, err := client.Do(req) + if err != nil { + log.Printf("Error sending PATCH request: %++v", err) + return err + } + defer res.Body.Close() + + if res.StatusCode != http.StatusOK { + log.Printf("Non-OK HTTP status: %d", res.StatusCode) + + body, err = io.ReadAll(res.Body) + if err != nil { + log.Printf("Error reading response body: %++v", err) + return err + } + var data APIError + err = json.Unmarshal(body, &data) + if err != nil { + log.Printf("Error unmarshaling JSON: %++v", err) + return err + } + + log.Printf("API error: %++v", data) + return fmt.Errorf("Non-OK HTTP status, err: %++v", data) + } + + return nil +} diff --git a/download.go b/download.go new file mode 100644 index 0000000..c058895 --- /dev/null +++ b/download.go @@ -0,0 +1,10 @@ +package main + +import "log" + +func Download(event PBEvent) (err error) { + log.Printf("Download event: %+v\n", event) + // SetDownloaded(event) + // ...do something + return nil +} diff --git a/main.go b/main.go index 5166956..22e6f49 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( "encoding/json" - "io/ioutil" + "io" "log" "net/http" "time" @@ -22,7 +22,7 @@ func main() { log.Fatal(err) } defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { log.Printf("Error reading response body: %+v\n", err) return @@ -49,10 +49,7 @@ func main() { select { case event := <-listener.Create: log.Printf("Create event: %+v\n", event) - case event := <-listener.Update: - log.Printf("Update event: %+v\n", event) - case event := <-listener.Delete: - log.Printf("Delete event: %+v\n", event) + go Download(event) } }