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

     1  package readarr
     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 Readarr excluded item.
    16  type Exclusion struct {
    17  	ForeignID  string `json:"foreignId"`
    18  	AuthorName string `json:"authorName"`
    19  	ID         int64  `json:"id,omitempty"`
    20  }
    21  
    22  // GetExclusions returns all configured exclusions from Readarr.
    23  func (r *Readarr) GetExclusions() ([]*Exclusion, error) {
    24  	return r.GetExclusionsContext(context.Background())
    25  }
    26  
    27  // GetExclusionsContext returns all configured exclusions from Readarr.
    28  func (r *Readarr) GetExclusionsContext(ctx context.Context) ([]*Exclusion, error) {
    29  	var output []*Exclusion
    30  
    31  	req := starr.Request{URI: bpExclusions}
    32  	if err := r.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 Readarr.
    40  func (r *Readarr) UpdateExclusion(exclusion *Exclusion) (*Exclusion, error) {
    41  	return r.UpdateExclusionContext(context.Background(), exclusion)
    42  }
    43  
    44  // UpdateExclusionContext changes an exclusions in Readarr.
    45  func (r *Readarr) 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 := r.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 Readarr.
    62  func (r *Readarr) DeleteExclusions(ids []int64) error {
    63  	return r.DeleteExclusionsContext(context.Background(), ids)
    64  }
    65  
    66  // DeleteExclusionsContext removes exclusions from Readarr.
    67  func (r *Readarr) 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 := r.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  }