golift.io/starr@v1.0.0/sonarr/qualityprofile.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 Quality Profile calls.
    14  const bpQualityProfile = APIver + "/qualityProfile"
    15  
    16  // QualityProfile is the /api/v3/qualityprofile endpoint.
    17  type QualityProfile struct {
    18  	UpgradeAllowed    bool                `json:"upgradeAllowed"`
    19  	ID                int64               `json:"id"`
    20  	Cutoff            int64               `json:"cutoff"`
    21  	Name              string              `json:"name"`
    22  	Qualities         []*starr.Quality    `json:"items"`
    23  	MinFormatScore    int64               `json:"minFormatScore"`        // v4 only.
    24  	CutoffFormatScore int64               `json:"cutoffFormatScore"`     // v4 only.
    25  	FormatItems       []*starr.FormatItem `json:"formatItems,omitempty"` // v4 only.
    26  	Language          *starr.Value        `json:"language,omitempty"`    // v4 only.
    27  }
    28  
    29  // GetQualityProfiles returns all configured quality profiles.
    30  func (s *Sonarr) GetQualityProfiles() ([]*QualityProfile, error) {
    31  	return s.GetQualityProfilesContext(context.Background())
    32  }
    33  
    34  // GetQualityProfilesContext returns all configured quality profiles.
    35  func (s *Sonarr) GetQualityProfilesContext(ctx context.Context) ([]*QualityProfile, error) {
    36  	var output []*QualityProfile
    37  
    38  	req := starr.Request{URI: bpQualityProfile}
    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  // GetQualityProfile returns a single quality profile.
    47  func (s *Sonarr) GetQualityProfile(profileID int64) (*QualityProfile, error) {
    48  	return s.GetQualityProfileContext(context.Background(), profileID)
    49  }
    50  
    51  // GetQualityProfileContext returns a single quality profile.
    52  func (s *Sonarr) GetQualityProfileContext(ctx context.Context, profileID int64) (*QualityProfile, error) {
    53  	var output QualityProfile
    54  
    55  	req := starr.Request{URI: path.Join(bpQualityProfile, 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  // AddQualityProfile creates a quality profile.
    64  func (s *Sonarr) AddQualityProfile(profile *QualityProfile) (*QualityProfile, error) {
    65  	return s.AddQualityProfileContext(context.Background(), profile)
    66  }
    67  
    68  // AddQualityProfileContext creates a quality profile.
    69  func (s *Sonarr) AddQualityProfileContext(ctx context.Context, profile *QualityProfile) (*QualityProfile, error) {
    70  	var output QualityProfile
    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", bpQualityProfile, err)
    75  	}
    76  
    77  	req := starr.Request{URI: bpQualityProfile, 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  // UpdateQualityProfile updates the quality profile.
    86  func (s *Sonarr) UpdateQualityProfile(profile *QualityProfile) (*QualityProfile, error) {
    87  	return s.UpdateQualityProfileContext(context.Background(), profile)
    88  }
    89  
    90  // UpdateQualityProfileContext updates the quality profile.
    91  func (s *Sonarr) UpdateQualityProfileContext(ctx context.Context, profile *QualityProfile) (*QualityProfile, error) {
    92  	var output QualityProfile
    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", bpQualityProfile, err)
    97  	}
    98  
    99  	req := starr.Request{URI: path.Join(bpQualityProfile, 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  // DeleteQualityProfile removes a single quality profile.
   108  func (s *Sonarr) DeleteQualityProfile(profileID int64) error {
   109  	return s.DeleteQualityProfileContext(context.Background(), profileID)
   110  }
   111  
   112  // DeleteQualityProfileContext removes a single quality profile.
   113  func (s *Sonarr) DeleteQualityProfileContext(ctx context.Context, profileID int64) error {
   114  	req := starr.Request{URI: path.Join(bpQualityProfile, 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  }