golift.io/starr@v1.0.0/radarr/exclusions.go (about)

     1  package radarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"path"
     9  
    10  	"golift.io/starr"
    11  )
    12  
    13  const bpExclusions = APIver + "/exclusions"
    14  
    15  // Exclusion is a Radarr excluded item.
    16  type Exclusion struct {
    17  	TMDBID int64  `json:"tmdbId"`
    18  	Title  string `json:"movieTitle"`
    19  	Year   int    `json:"movieYear"`
    20  	ID     int64  `json:"id,omitempty"`
    21  }
    22  
    23  // GetExclusions returns all configured exclusions from Radarr.
    24  func (r *Radarr) GetExclusions() ([]*Exclusion, error) {
    25  	return r.GetExclusionsContext(context.Background())
    26  }
    27  
    28  // GetExclusionsContext returns all configured exclusions from Radarr.
    29  func (r *Radarr) GetExclusionsContext(ctx context.Context) ([]*Exclusion, error) {
    30  	var output []*Exclusion
    31  
    32  	req := starr.Request{URI: bpExclusions}
    33  	if err := r.GetInto(ctx, req, &output); err != nil {
    34  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    35  	}
    36  
    37  	return output, nil
    38  }
    39  
    40  // UpdateExclusion changes an exclusions in Radarr.
    41  func (r *Radarr) UpdateExclusion(exclusion *Exclusion) (*Exclusion, error) {
    42  	return r.UpdateExclusionContext(context.Background(), exclusion)
    43  }
    44  
    45  // UpdateExclusionContext changes an exclusions in Radarr.
    46  func (r *Radarr) UpdateExclusionContext(ctx context.Context, exclusion *Exclusion) (*Exclusion, error) {
    47  	var body bytes.Buffer
    48  	if err := json.NewEncoder(&body).Encode(exclusion); err != nil {
    49  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpExclusions, err)
    50  	}
    51  
    52  	var output Exclusion
    53  
    54  	req := starr.Request{URI: path.Join(bpExclusions, fmt.Sprint(exclusion.ID)), Body: &body}
    55  	if err := r.PutInto(ctx, req, &output); err != nil {
    56  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
    57  	}
    58  
    59  	return &output, nil
    60  }
    61  
    62  // DeleteExclusions removes exclusions from Radarr.
    63  func (r *Radarr) DeleteExclusions(ids []int64) error {
    64  	return r.DeleteExclusionsContext(context.Background(), ids)
    65  }
    66  
    67  // DeleteExclusionsContext removes exclusions from Radarr.
    68  func (r *Radarr) DeleteExclusionsContext(ctx context.Context, ids []int64) error {
    69  	var errs string
    70  
    71  	for _, id := range ids {
    72  		req := starr.Request{URI: path.Join(bpExclusions, fmt.Sprint(id))}
    73  		if err := r.DeleteAny(ctx, req); err != nil {
    74  			errs += fmt.Sprintf("api.Post(%s): %v ", &req, err)
    75  		}
    76  	}
    77  
    78  	if errs != "" {
    79  		return fmt.Errorf("%w: %s", starr.ErrRequestError, errs)
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  // AddExclusions adds multiple exclusions to Radarr.
    86  func (r *Radarr) AddExclusions(exclusions []*Exclusion) error {
    87  	return r.AddExclusionsContext(context.Background(), exclusions)
    88  }
    89  
    90  // AddExclusionsContext adds exclusions to Radarr.
    91  func (r *Radarr) AddExclusionsContext(ctx context.Context, exclusions []*Exclusion) error {
    92  	for i := range exclusions {
    93  		exclusions[i].ID = 0
    94  	}
    95  
    96  	var body bytes.Buffer
    97  	if err := json.NewEncoder(&body).Encode(exclusions); err != nil {
    98  		return fmt.Errorf("json.Marshal(%s): %w", bpExclusions, err)
    99  	}
   100  
   101  	var output interface{} // any ok
   102  
   103  	req := starr.Request{URI: path.Join(bpExclusions, "bulk"), Body: &body}
   104  	if err := r.PostInto(ctx, req, &output); err != nil {
   105  		return fmt.Errorf("api.Post(%s): %w", &req, err)
   106  	}
   107  
   108  	return nil
   109  }