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

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