76 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
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
 | 
						|
}
 |