golift.io/starr@v1.0.0/sonarr/delayprofile.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 Delay Profile calls.
    14  const bpDelayProfile = APIver + "/delayProfile"
    15  
    16  // DelayProfile is the /api/v3/delayprofile endpoint.
    17  type DelayProfile struct {
    18  	EnableUsenet           bool   `json:"enableUsenet,omitempty"`
    19  	EnableTorrent          bool   `json:"enableTorrent,omitempty"`
    20  	BypassIfHighestQuality bool   `json:"bypassIfHighestQuality,omitempty"`
    21  	UsenetDelay            int64  `json:"usenetDelay,omitempty"`
    22  	TorrentDelay           int64  `json:"torrentDelay,omitempty"`
    23  	ID                     int64  `json:"id,omitempty"`
    24  	Order                  int64  `json:"order,omitempty"`
    25  	Tags                   []int  `json:"tags"`
    26  	PreferredProtocol      string `json:"preferredProtocol,omitempty"`
    27  }
    28  
    29  // GetDelayProfiles returns all configured delay profiles.
    30  func (s *Sonarr) GetDelayProfiles() ([]*DelayProfile, error) {
    31  	return s.GetDelayProfilesContext(context.Background())
    32  }
    33  
    34  // GetDelayProfilesContext returns all configured delay profiles.
    35  func (s *Sonarr) GetDelayProfilesContext(ctx context.Context) ([]*DelayProfile, error) {
    36  	var output []*DelayProfile
    37  
    38  	req := starr.Request{URI: bpDelayProfile}
    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  // GetDelayProfile returns a single delay profile.
    47  func (s *Sonarr) GetDelayProfile(profileID int64) (*DelayProfile, error) {
    48  	return s.GetDelayProfileContext(context.Background(), profileID)
    49  }
    50  
    51  // GetDelayProfileContext returns a single delay profile.
    52  func (s *Sonarr) GetDelayProfileContext(ctx context.Context, profileID int64) (*DelayProfile, error) {
    53  	var output DelayProfile
    54  
    55  	req := starr.Request{URI: path.Join(bpDelayProfile, 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  // AddDelayProfile creates a delay profile.
    64  // AddDelayProfile doesn't take into account the "order" field sent on creation.
    65  // Order will be set to first available. This can only be edited via UpdateDelayProfile later on.
    66  func (s *Sonarr) AddDelayProfile(profile *DelayProfile) (*DelayProfile, error) {
    67  	return s.AddDelayProfileContext(context.Background(), profile)
    68  }
    69  
    70  // AddDelayProfileContext creates a delay profile.
    71  func (s *Sonarr) AddDelayProfileContext(ctx context.Context, profile *DelayProfile) (*DelayProfile, error) {
    72  	var output DelayProfile
    73  
    74  	var body bytes.Buffer
    75  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    76  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpDelayProfile, err)
    77  	}
    78  
    79  	req := starr.Request{URI: bpDelayProfile, Body: &body}
    80  	if err := s.PostInto(ctx, req, &output); err != nil {
    81  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    82  	}
    83  
    84  	return &output, nil
    85  }
    86  
    87  // UpdateDelayProfile updates the delay profile.
    88  func (s *Sonarr) UpdateDelayProfile(profile *DelayProfile) (*DelayProfile, error) {
    89  	return s.UpdateDelayProfileContext(context.Background(), profile)
    90  }
    91  
    92  // UpdateDelayProfileContext updates the delay profile.
    93  func (s *Sonarr) UpdateDelayProfileContext(ctx context.Context, profile *DelayProfile) (*DelayProfile, error) {
    94  	var output DelayProfile
    95  
    96  	var body bytes.Buffer
    97  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    98  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpDelayProfile, err)
    99  	}
   100  
   101  	req := starr.Request{URI: path.Join(bpDelayProfile, fmt.Sprint(profile.ID)), Body: &body}
   102  	if err := s.PutInto(ctx, req, &output); err != nil {
   103  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   104  	}
   105  
   106  	return &output, nil
   107  }
   108  
   109  // DeleteDelayProfile removes a single delay profile.
   110  func (s *Sonarr) DeleteDelayProfile(profileID int64) error {
   111  	return s.DeleteDelayProfileContext(context.Background(), profileID)
   112  }
   113  
   114  // DeleteDelayProfileContext removes a single delay profile.
   115  func (s *Sonarr) DeleteDelayProfileContext(ctx context.Context, profileID int64) error {
   116  	req := starr.Request{URI: path.Join(bpDelayProfile, fmt.Sprint(profileID))}
   117  	if err := s.DeleteAny(ctx, req); err != nil {
   118  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   119  	}
   120  
   121  	return nil
   122  }