golift.io/starr@v1.0.0/lidarr/qualityprofile.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 bpQualityProfile = APIver + "/qualityProfile" 14 15 // QualityProfile is the /api/v1/qualityprofile endpoint. 16 type QualityProfile struct { 17 ID int64 `json:"id"` 18 Name string `json:"name"` 19 UpgradeAllowed bool `json:"upgradeAllowed"` 20 Cutoff int64 `json:"cutoff"` 21 Qualities []*starr.Quality `json:"items"` 22 MinFormatScore int64 `json:"minFormatScore"` 23 CutoffFormatScore int64 `json:"cutoffFormatScore"` 24 FormatItems []*starr.FormatItem `json:"formatItems"` 25 } 26 27 // GetQualityProfiles returns the quality profiles. 28 func (l *Lidarr) GetQualityProfiles() ([]*QualityProfile, error) { 29 return l.GetQualityProfilesContext(context.Background()) 30 } 31 32 // GetQualityProfilesContext returns the quality profiles. 33 func (l *Lidarr) GetQualityProfilesContext(ctx context.Context) ([]*QualityProfile, error) { 34 var output []*QualityProfile 35 36 req := starr.Request{URI: bpQualityProfile} 37 if err := l.GetInto(ctx, req, &output); err != nil { 38 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 39 } 40 41 return output, nil 42 } 43 44 // AddQualityProfile updates a quality profile in place. 45 func (l *Lidarr) AddQualityProfile(profile *QualityProfile) (int64, error) { 46 return l.AddQualityProfileContext(context.Background(), profile) 47 } 48 49 // AddQualityProfileContext updates a quality profile in place. 50 func (l *Lidarr) AddQualityProfileContext(ctx context.Context, profile *QualityProfile) (int64, error) { 51 var body bytes.Buffer 52 if err := json.NewEncoder(&body).Encode(profile); err != nil { 53 return 0, fmt.Errorf("json.Marshal(%s): %w", bpQualityProfile, err) 54 } 55 56 var output QualityProfile 57 58 req := starr.Request{URI: bpQualityProfile, Body: &body} 59 if err := l.PostInto(ctx, req, &output); err != nil { 60 return 0, fmt.Errorf("api.Post(%s): %w", &req, err) 61 } 62 63 return output.ID, nil 64 } 65 66 // UpdateQualityProfile updates a quality profile in place. 67 func (l *Lidarr) UpdateQualityProfile(profile *QualityProfile) (*QualityProfile, error) { 68 return l.UpdateQualityProfileContext(context.Background(), profile) 69 } 70 71 // UpdateQualityProfileContext updates a quality profile in place. 72 func (l *Lidarr) UpdateQualityProfileContext(ctx context.Context, profile *QualityProfile) (*QualityProfile, error) { 73 var body bytes.Buffer 74 if err := json.NewEncoder(&body).Encode(profile); err != nil { 75 return nil, fmt.Errorf("json.Marshal(%s): %w", bpQualityProfile, err) 76 } 77 78 var output QualityProfile 79 80 req := starr.Request{URI: path.Join(bpQualityProfile, fmt.Sprint(profile.ID)), Body: &body} 81 if err := l.PutInto(ctx, req, &output); err != nil { 82 return nil, fmt.Errorf("api.Put(%s): %w", &req, err) 83 } 84 85 return &output, nil 86 } 87 88 // DeleteQualityProfile deletes a quality profile. 89 func (l *Lidarr) DeleteQualityProfile(profileID int64) error { 90 return l.DeleteQualityProfileContext(context.Background(), profileID) 91 } 92 93 // DeleteQualityProfileContext deletes a quality profile. 94 func (l *Lidarr) DeleteQualityProfileContext(ctx context.Context, profileID int64) error { 95 req := starr.Request{URI: path.Join(bpQualityProfile, fmt.Sprint(profileID))} 96 if err := l.DeleteAny(ctx, req); err != nil { 97 return fmt.Errorf("api.Delete(%s): %w", &req, err) 98 } 99 100 return nil 101 }