golift.io/starr@v1.0.0/sonarr/releaseprofile.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  // Define Base Path for Release Profile calls.
    14  const bpReleaseProfile = APIver + "/releaseProfile"
    15  
    16  // ReleaseProfile defines a release profile's data from Sonarr.
    17  type ReleaseProfile struct {
    18  	Name            string            `json:"name"`
    19  	Enabled         bool              `json:"enabled"`
    20  	Required        []string          `json:"required"`
    21  	Ignored         []string          `json:"ignored"`
    22  	IndexerID       int64             `json:"indexerId"`
    23  	Tags            []int             `json:"tags"`
    24  	ID              int64             `json:"id,omitempty"`
    25  	IncPrefOnRename *bool             `json:"includePreferredWhenRenaming,omitempty"` // V3 only, removed from v4.
    26  	Preferred       []*starr.KeyValue `json:"preferred,omitempty"`                    // V3 only, removed from v4.
    27  }
    28  
    29  // GetReleaseProfiles returns all configured release profiles.
    30  func (s *Sonarr) GetReleaseProfiles() ([]*ReleaseProfile, error) {
    31  	return s.GetReleaseProfilesContext(context.Background())
    32  }
    33  
    34  // GetReleaseProfilesContext returns all configured release profiles.
    35  func (s *Sonarr) GetReleaseProfilesContext(ctx context.Context) ([]*ReleaseProfile, error) {
    36  	var output []*ReleaseProfile
    37  
    38  	req := starr.Request{URI: bpReleaseProfile}
    39  	if err := s.GetInto(ctx, req, &output); err != nil {
    40  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    41  	}
    42  
    43  	return output, nil
    44  }
    45  
    46  // GetReleaseProfile returns a single release profile.
    47  func (s *Sonarr) GetReleaseProfile(profileID int64) (*ReleaseProfile, error) {
    48  	return s.GetReleaseProfileContext(context.Background(), profileID)
    49  }
    50  
    51  // GetReleaseProfileContext returns a single release profile.
    52  func (s *Sonarr) GetReleaseProfileContext(ctx context.Context, profileID int64) (*ReleaseProfile, error) {
    53  	var output ReleaseProfile
    54  
    55  	req := starr.Request{URI: path.Join(bpReleaseProfile, fmt.Sprint(profileID))}
    56  	if err := s.GetInto(ctx, req, &output); err != nil {
    57  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    58  	}
    59  
    60  	return &output, nil
    61  }
    62  
    63  // AddReleaseProfile creates a release profile.
    64  func (s *Sonarr) AddReleaseProfile(profile *ReleaseProfile) (*ReleaseProfile, error) {
    65  	return s.AddReleaseProfileContext(context.Background(), profile)
    66  }
    67  
    68  // AddReleaseProfileContext creates a release profile.
    69  func (s *Sonarr) AddReleaseProfileContext(ctx context.Context, profile *ReleaseProfile) (*ReleaseProfile, error) {
    70  	var output ReleaseProfile
    71  
    72  	var body bytes.Buffer
    73  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    74  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpReleaseProfile, err)
    75  	}
    76  
    77  	req := starr.Request{URI: bpReleaseProfile, Body: &body}
    78  	if err := s.PostInto(ctx, req, &output); err != nil {
    79  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    80  	}
    81  
    82  	return &output, nil
    83  }
    84  
    85  // UpdateReleaseProfile updates the release profile.
    86  func (s *Sonarr) UpdateReleaseProfile(profile *ReleaseProfile) (*ReleaseProfile, error) {
    87  	return s.UpdateReleaseProfileContext(context.Background(), profile)
    88  }
    89  
    90  // UpdateReleaseProfileContext updates the release profile.
    91  func (s *Sonarr) UpdateReleaseProfileContext(ctx context.Context, profile *ReleaseProfile) (*ReleaseProfile, error) {
    92  	var output ReleaseProfile
    93  
    94  	var body bytes.Buffer
    95  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    96  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpReleaseProfile, err)
    97  	}
    98  
    99  	req := starr.Request{URI: path.Join(bpReleaseProfile, fmt.Sprint(profile.ID)), Body: &body}
   100  	if err := s.PutInto(ctx, req, &output); err != nil {
   101  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   102  	}
   103  
   104  	return &output, nil
   105  }
   106  
   107  // DeleteReleaseProfile removes a single release profile.
   108  func (s *Sonarr) DeleteReleaseProfile(profileID int64) error {
   109  	return s.DeleteReleaseProfileContext(context.Background(), profileID)
   110  }
   111  
   112  // DeleteReleaseProfileContext removes a single release profile.
   113  func (s *Sonarr) DeleteReleaseProfileContext(ctx context.Context, profileID int64) error {
   114  	req := starr.Request{URI: path.Join(bpReleaseProfile, fmt.Sprint(profileID))}
   115  	if err := s.DeleteAny(ctx, req); err != nil {
   116  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   117  	}
   118  
   119  	return nil
   120  }