golift.io/starr@v1.0.0/radarr/releaseprofile.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  // Define Base Path for Release Profile calls.
    14  const bpReleaseProfile = APIver + "/releaseProfile"
    15  
    16  // ReleaseProfile defines a release profile's data from Radarr. v4 only.
    17  type ReleaseProfile struct {
    18  	Name      string   `json:"name"`
    19  	Enabled   bool     `json:"enabled"`
    20  	Required  []string `json:"required"`
    21  	Ignored   []string `json:"ignored"`
    22  	IndexerID int64    `json:"indexerId"`
    23  	Tags      []int    `json:"tags"`
    24  	ID        int64    `json:"id,omitempty"`
    25  }
    26  
    27  // GetReleaseProfiles returns all configured release profiles.
    28  func (r *Radarr) GetReleaseProfiles() ([]*ReleaseProfile, error) {
    29  	return r.GetReleaseProfilesContext(context.Background())
    30  }
    31  
    32  // GetReleaseProfilesContext returns all configured release profiles.
    33  func (r *Radarr) GetReleaseProfilesContext(ctx context.Context) ([]*ReleaseProfile, error) {
    34  	var output []*ReleaseProfile
    35  
    36  	req := starr.Request{URI: bpReleaseProfile}
    37  	if err := r.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  // GetReleaseProfile returns a single release profile.
    45  func (r *Radarr) GetReleaseProfile(profileID int64) (*ReleaseProfile, error) {
    46  	return r.GetReleaseProfileContext(context.Background(), profileID)
    47  }
    48  
    49  // GetReleaseProfileContext returns a single release profile.
    50  func (r *Radarr) GetReleaseProfileContext(ctx context.Context, profileID int64) (*ReleaseProfile, error) {
    51  	var output ReleaseProfile
    52  
    53  	req := starr.Request{URI: path.Join(bpReleaseProfile, fmt.Sprint(profileID))}
    54  	if err := r.GetInto(ctx, req, &output); err != nil {
    55  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    56  	}
    57  
    58  	return &output, nil
    59  }
    60  
    61  // AddReleaseProfile creates a release profile.
    62  func (r *Radarr) AddReleaseProfile(profile *ReleaseProfile) (*ReleaseProfile, error) {
    63  	return r.AddReleaseProfileContext(context.Background(), profile)
    64  }
    65  
    66  // AddReleaseProfileContext creates a release profile.
    67  func (r *Radarr) AddReleaseProfileContext(ctx context.Context, profile *ReleaseProfile) (*ReleaseProfile, error) {
    68  	var output ReleaseProfile
    69  
    70  	var body bytes.Buffer
    71  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    72  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpReleaseProfile, err)
    73  	}
    74  
    75  	req := starr.Request{URI: bpReleaseProfile, Body: &body}
    76  	if err := r.PostInto(ctx, req, &output); err != nil {
    77  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    78  	}
    79  
    80  	return &output, nil
    81  }
    82  
    83  // UpdateReleaseProfile updates the release profile.
    84  func (r *Radarr) UpdateReleaseProfile(profile *ReleaseProfile) (*ReleaseProfile, error) {
    85  	return r.UpdateReleaseProfileContext(context.Background(), profile)
    86  }
    87  
    88  // UpdateReleaseProfileContext updates the release profile.
    89  func (r *Radarr) UpdateReleaseProfileContext(ctx context.Context, profile *ReleaseProfile) (*ReleaseProfile, error) {
    90  	var output ReleaseProfile
    91  
    92  	var body bytes.Buffer
    93  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    94  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpReleaseProfile, err)
    95  	}
    96  
    97  	req := starr.Request{URI: path.Join(bpReleaseProfile, fmt.Sprint(profile.ID)), Body: &body}
    98  	if err := r.PutInto(ctx, req, &output); err != nil {
    99  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   100  	}
   101  
   102  	return &output, nil
   103  }
   104  
   105  // DeleteReleaseProfile removes a single release profile.
   106  func (r *Radarr) DeleteReleaseProfile(profileID int64) error {
   107  	return r.DeleteReleaseProfileContext(context.Background(), profileID)
   108  }
   109  
   110  // DeleteReleaseProfileContext removes a single release profile.
   111  func (r *Radarr) DeleteReleaseProfileContext(ctx context.Context, profileID int64) error {
   112  	req := starr.Request{URI: path.Join(bpReleaseProfile, fmt.Sprint(profileID))}
   113  	if err := r.DeleteAny(ctx, req); err != nil {
   114  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   115  	}
   116  
   117  	return nil
   118  }