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

     1  package sonarr
     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 + "/importlistexclusion"
    14  
    15  // Exclusion is a Sonarr excluded item.
    16  type Exclusion struct {
    17  	TVDBID int64  `json:"tvdbId"`
    18  	Title  string `json:"title"`
    19  	ID     int64  `json:"id,omitempty"`
    20  }
    21  
    22  // GetExclusions returns all configured exclusions from Sonarr.
    23  func (s *Sonarr) GetExclusions() ([]*Exclusion, error) {
    24  	return s.GetExclusionsContext(context.Background())
    25  }
    26  
    27  // GetExclusionsContext returns all configured exclusions from Sonarr.
    28  func (s *Sonarr) GetExclusionsContext(ctx context.Context) ([]*Exclusion, error) {
    29  	var output []*Exclusion
    30  
    31  	req := starr.Request{URI: bpExclusions}
    32  	if err := s.GetInto(ctx, req, &output); err != nil {
    33  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    34  	}
    35  
    36  	return output, nil
    37  }
    38  
    39  // UpdateExclusion changes an exclusions in Sonarr.
    40  func (s *Sonarr) UpdateExclusion(exclusion *Exclusion) (*Exclusion, error) {
    41  	return s.UpdateExclusionContext(context.Background(), exclusion)
    42  }
    43  
    44  // UpdateExclusionContext changes an exclusions in Sonarr.
    45  func (s *Sonarr) UpdateExclusionContext(ctx context.Context, exclusion *Exclusion) (*Exclusion, error) {
    46  	var body bytes.Buffer
    47  	if err := json.NewEncoder(&body).Encode(exclusion); err != nil {
    48  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpExclusions, err)
    49  	}
    50  
    51  	var output Exclusion
    52  
    53  	req := starr.Request{URI: path.Join(bpExclusions, fmt.Sprint(exclusion.ID)), Body: &body}
    54  	if err := s.PutInto(ctx, req, &output); err != nil {
    55  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
    56  	}
    57  
    58  	return &output, nil
    59  }
    60  
    61  // DeleteExclusions removes exclusions from Sonarr.
    62  func (s *Sonarr) DeleteExclusions(ids []int64) error {
    63  	return s.DeleteExclusionsContext(context.Background(), ids)
    64  }
    65  
    66  // DeleteExclusionsContext removes exclusions from Sonarr.
    67  func (s *Sonarr) DeleteExclusionsContext(ctx context.Context, ids []int64) error {
    68  	var errs string
    69  
    70  	for _, id := range ids {
    71  		req := starr.Request{URI: path.Join(bpExclusions, fmt.Sprint(id))}
    72  		if err := s.DeleteAny(ctx, req); err != nil {
    73  			errs += fmt.Sprintf("api.Post(%s): %v ", &req, err)
    74  		}
    75  	}
    76  
    77  	if errs != "" {
    78  		return fmt.Errorf("%w: %s", starr.ErrRequestError, errs)
    79  	}
    80  
    81  	return nil
    82  }