golift.io/starr@v1.0.0/radarr/qualityprofile.go (about)

     1  package radarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"path"
     9  
    10  	"golift.io/starr"
    11  )
    12  
    13  const bpQualityProfile = APIver + "/qualityProfile"
    14  
    15  // QualityProfile is applied to Movies.
    16  type QualityProfile struct {
    17  	ID                int64               `json:"id,omitempty"`
    18  	Name              string              `json:"name,omitempty"`
    19  	UpgradeAllowed    bool                `json:"upgradeAllowed"`
    20  	Cutoff            int64               `json:"cutoff"`
    21  	Qualities         []*starr.Quality    `json:"items,omitempty"`
    22  	MinFormatScore    int64               `json:"minFormatScore"`
    23  	CutoffFormatScore int64               `json:"cutoffFormatScore"`
    24  	FormatItems       []*starr.FormatItem `json:"formatItems"`
    25  	Language          *starr.Value        `json:"language,omitempty"`
    26  }
    27  
    28  // GetQualityProfiles returns all configured quality profiles.
    29  func (r *Radarr) GetQualityProfiles() ([]*QualityProfile, error) {
    30  	return r.GetQualityProfilesContext(context.Background())
    31  }
    32  
    33  // GetQualityProfilesContext returns all configured quality profiles.
    34  func (r *Radarr) GetQualityProfilesContext(ctx context.Context) ([]*QualityProfile, error) {
    35  	var output []*QualityProfile
    36  
    37  	req := starr.Request{URI: bpQualityProfile}
    38  	if err := r.GetInto(ctx, req, &output); err != nil {
    39  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    40  	}
    41  
    42  	return output, nil
    43  }
    44  
    45  // GetQualityProfile returns a single quality profile.
    46  func (r *Radarr) GetQualityProfile(profileID int64) (*QualityProfile, error) {
    47  	return r.GetQualityProfileContext(context.Background(), profileID)
    48  }
    49  
    50  // GetQualityProfileContext returns a single quality profile.
    51  func (r *Radarr) GetQualityProfileContext(ctx context.Context, profileID int64) (*QualityProfile, error) {
    52  	var output QualityProfile
    53  
    54  	req := starr.Request{URI: path.Join(bpQualityProfile, fmt.Sprint(profileID))}
    55  	if err := r.GetInto(ctx, req, &output); err != nil {
    56  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    57  	}
    58  
    59  	return &output, nil
    60  }
    61  
    62  // AddQualityProfile updates a quality profile in place.
    63  func (r *Radarr) AddQualityProfile(profile *QualityProfile) (*QualityProfile, error) {
    64  	return r.AddQualityProfileContext(context.Background(), profile)
    65  }
    66  
    67  // AddQualityProfileContext updates a quality profile in place.
    68  func (r *Radarr) AddQualityProfileContext(ctx context.Context, profile *QualityProfile) (*QualityProfile, error) {
    69  	var output QualityProfile
    70  
    71  	var body bytes.Buffer
    72  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    73  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpQualityProfile, err)
    74  	}
    75  
    76  	req := starr.Request{URI: bpQualityProfile, Body: &body}
    77  	if err := r.PostInto(ctx, req, &output); err != nil {
    78  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    79  	}
    80  
    81  	return &output, nil
    82  }
    83  
    84  // UpdateQualityProfile updates a quality profile in place.
    85  func (r *Radarr) UpdateQualityProfile(profile *QualityProfile) (*QualityProfile, error) {
    86  	return r.UpdateQualityProfileContext(context.Background(), profile)
    87  }
    88  
    89  // UpdateQualityProfileContext updates a quality profile in place.
    90  func (r *Radarr) UpdateQualityProfileContext(ctx context.Context, profile *QualityProfile) (*QualityProfile, error) {
    91  	var output QualityProfile
    92  
    93  	var body bytes.Buffer
    94  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    95  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpQualityProfile, err)
    96  	}
    97  
    98  	req := starr.Request{URI: path.Join(bpQualityProfile, fmt.Sprint(profile.ID)), Body: &body}
    99  	if err := r.PutInto(ctx, req, &output); err != nil {
   100  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   101  	}
   102  
   103  	return &output, nil
   104  }
   105  
   106  // DeleteQualityProfile deletes a quality profile.
   107  func (r *Radarr) DeleteQualityProfile(profileID int64) error {
   108  	return r.DeleteQualityProfileContext(context.Background(), profileID)
   109  }
   110  
   111  // DeleteQualityProfileContext deletes a quality profile.
   112  func (r *Radarr) DeleteQualityProfileContext(ctx context.Context, profileID int64) error {
   113  	req := starr.Request{URI: path.Join(bpQualityProfile, fmt.Sprint(profileID))}
   114  	if err := r.DeleteAny(ctx, req); err != nil {
   115  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   116  	}
   117  
   118  	return nil
   119  }