Skip to content

Commit

Permalink
Merge pull request golift#40 from Fuochi/feature/sonarr-delay
Browse files Browse the repository at this point in the history
feat(sonarr): add delayprofile qualitydefinition
  • Loading branch information
Fuochi authored Mar 4, 2022
2 parents e029bb5 + 21a00b6 commit 7ff21cc
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
108 changes: 108 additions & 0 deletions sonarr/delayprofile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package sonarr

import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
)

// DelayProfile is the /api/v3/delayprofile endpoint.
type DelayProfile struct {
EnableUsenet bool `json:"enableUsenet"`
EnableTorrent bool `json:"enableTorrent"`
BypassIfHighestQuality bool `json:"bypassIfHighestQuality"`
UsenetDelay int64 `json:"usenetDelay"`
TorrentDelay int64 `json:"torrentDelay"`
ID int64 `json:"id,omitempty"`
Order int64 `json:"order"`
Tags []int `json:"tags"`
PreferredProtocol string `json:"preferredProtocol"`
}

// GetDelayProfiles returns all configured delay profiles.
func (s *Sonarr) GetDelayProfiles() ([]*DelayProfile, error) {
return s.GetDelayProfilesContext(context.Background())
}

func (s *Sonarr) GetDelayProfilesContext(ctx context.Context) ([]*DelayProfile, error) {
var output []*DelayProfile

if _, err := s.GetInto(ctx, "v3/delayProfile", nil, &output); err != nil {
return nil, fmt.Errorf("api.Get(delayProfile): %w", err)
}

return output, nil
}

// GetDelayProfile returns a single delay profile.
func (s *Sonarr) GetDelayProfile(profileID int) (*DelayProfile, error) {
return s.GetDelayProfileContext(context.Background(), profileID)
}

func (s *Sonarr) GetDelayProfileContext(ctx context.Context, profileID int) (*DelayProfile, error) {
var output *DelayProfile

id := strconv.Itoa(profileID)
if _, err := s.GetInto(ctx, "v3/delayProfile/"+id, nil, &output); err != nil {
return nil, fmt.Errorf("api.Get(delayProfile): %w", err)
}

return output, nil
}

// AddDelayProfile creates a delay profile.
func (s *Sonarr) AddDelayProfile(profile *DelayProfile) (*DelayProfile, error) {
return s.AddDelayProfileContext(context.Background(), profile)
}

func (s *Sonarr) AddDelayProfileContext(ctx context.Context, profile *DelayProfile) (*DelayProfile, error) {
var output DelayProfile

var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(profile); err != nil {
return nil, fmt.Errorf("json.Marshal(delayProfile): %w", err)
}

if _, err := s.PostInto(ctx, "v3/delayProfile", nil, &body, &output); err != nil {
return nil, fmt.Errorf("api.Post(delayProfile): %w", err)
}

return &output, nil
}

// UpdateDelayProfile updates the delay profile.
func (s *Sonarr) UpdateDelayProfile(profile *DelayProfile) (*DelayProfile, error) {
return s.UpdateDelayProfileContext(context.Background(), profile)
}

func (s *Sonarr) UpdateDelayProfileContext(ctx context.Context, profile *DelayProfile) (*DelayProfile, error) {
var output DelayProfile

var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(profile); err != nil {
return nil, fmt.Errorf("json.Marshal(delayProfile): %w", err)
}

id := strconv.Itoa(int(profile.ID))
if _, err := s.PutInto(ctx, "v3/delayProfile/"+id, nil, &body, &output); err != nil {
return nil, fmt.Errorf("api.Put(delayProfile): %w", err)
}

return &output, nil
}

// DeleteDelayProfile removes a single delay profile.
func (s *Sonarr) DeleteDelayProfile(profileID int) error {
return s.DeleteDelayProfileContext(context.Background(), profileID)
}

func (s *Sonarr) DeleteDelayProfileContext(ctx context.Context, profileID int) error {
id := strconv.Itoa(profileID)
if _, err := s.Delete(ctx, "v3/delayProfile/"+id, nil); err != nil {
return fmt.Errorf("api.Delete(delayProfile): %w", err)
}

return nil
}
74 changes: 74 additions & 0 deletions sonarr/qualitydefinition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package sonarr

import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"

"golift.io/starr"
)

// QualityDefinition is the /api/v3/qualitydefinition endpoint.
type QualityDefinition struct {
ID int64 `json:"id,omitempty"`
Weight int64 `json:"weight"`
MinSize float64 `json:"minSize"`
MaxSize float64 `json:"maxSize"`
Title string `json:"title"`
Qualities []*starr.Quality `json:"items"`
}

// GetQualityDefinitions returns all configured quality definitions.
func (s *Sonarr) GetQualityDefinitions() ([]*QualityDefinition, error) {
return s.GetQualityDefinitionsContext(context.Background())
}

func (s *Sonarr) GetQualityDefinitionsContext(ctx context.Context) ([]*QualityDefinition, error) {
var output []*QualityDefinition

if _, err := s.GetInto(ctx, "v3/qualityDefinition", nil, &output); err != nil {
return nil, fmt.Errorf("api.Get(qualityDefinition): %w", err)
}

return output, nil
}

// GetQualityDefinition returns a single quality definition.
func (s *Sonarr) GetQualityDefinition(qualityDefinitionID int) (*QualityDefinition, error) {
return s.GetQualityDefinitionContext(context.Background(), qualityDefinitionID)
}

func (s *Sonarr) GetQualityDefinitionContext(ctx context.Context, qualityDefinitionID int) (*QualityDefinition, error) {
var output *QualityDefinition

id := strconv.Itoa(qualityDefinitionID)
if _, err := s.GetInto(ctx, "v3/qualityDefinition/"+id, nil, &output); err != nil {
return nil, fmt.Errorf("api.Get(qualityDefinition): %w", err)
}

return output, nil
}

// UpdateQualityDefinition updates the quality definition.
func (s *Sonarr) UpdateQualityDefinition(definition *QualityDefinition) (*QualityDefinition, error) {
return s.UpdateQualityDefinitionContext(context.Background(), definition)
}

func (s *Sonarr) UpdateQualityDefinitionContext(ctx context.Context,
definition *QualityDefinition) (*QualityDefinition, error) {
var output QualityDefinition

var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(definition); err != nil {
return nil, fmt.Errorf("json.Marshal(qualityDefinition): %w", err)
}

id := strconv.Itoa(int(definition.ID))
if _, err := s.PutInto(ctx, "v3/qualityDefinition/"+id, nil, &body, &output); err != nil {
return nil, fmt.Errorf("api.Put(qualityDefinition): %w", err)
}

return &output, nil
}

0 comments on commit 7ff21cc

Please sign in to comment.