Implement PATCH-ing to the api
This commit is contained in:
75
api.go
Normal file
75
api.go
Normal file
@@ -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
|
||||||
|
}
|
10
download.go
Normal file
10
download.go
Normal file
@@ -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
|
||||||
|
}
|
9
main.go
9
main.go
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -22,7 +22,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
body, err := io.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reading response body: %+v\n", err)
|
log.Printf("Error reading response body: %+v\n", err)
|
||||||
return
|
return
|
||||||
@@ -49,10 +49,7 @@ func main() {
|
|||||||
select {
|
select {
|
||||||
case event := <-listener.Create:
|
case event := <-listener.Create:
|
||||||
log.Printf("Create event: %+v\n", event)
|
log.Printf("Create event: %+v\n", event)
|
||||||
case event := <-listener.Update:
|
go Download(event)
|
||||||
log.Printf("Update event: %+v\n", event)
|
|
||||||
case event := <-listener.Delete:
|
|
||||||
log.Printf("Delete event: %+v\n", event)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user