golift.io/starr@v1.0.0/radarr/restriction.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 bpRestriction = APIver + "/restriction"
    14  
    15  // Restriction is the input for a new or updated restriction.
    16  type Restriction struct {
    17  	Tags     []int  `json:"tags,omitempty"`
    18  	Required string `json:"required,omitempty"`
    19  	Ignored  string `json:"ignored,omitempty"`
    20  	ID       int64  `json:"id,omitempty"`
    21  }
    22  
    23  // GetRestrictions returns all configured restrictions.
    24  func (r *Radarr) GetRestrictions() ([]*Restriction, error) {
    25  	return r.GetRestrictionsContext(context.Background())
    26  }
    27  
    28  // GetRestrictionsContext returns all configured restrictions.
    29  func (r *Radarr) GetRestrictionsContext(ctx context.Context) ([]*Restriction, error) {
    30  	var output []*Restriction
    31  
    32  	req := starr.Request{URI: bpRestriction}
    33  	if err := r.GetInto(ctx, req, &output); err != nil {
    34  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    35  	}
    36  
    37  	return output, nil
    38  }
    39  
    40  // GetRestriction returns a single restriction.
    41  func (r *Radarr) GetRestriction(restrictionID int64) (*Restriction, error) {
    42  	return r.GetRestrictionContext(context.Background(), restrictionID)
    43  }
    44  
    45  // GetIndGetRestrictionContextexer returns a single restriction.
    46  func (r *Radarr) GetRestrictionContext(ctx context.Context, restrictionID int64) (*Restriction, error) {
    47  	var output Restriction
    48  
    49  	req := starr.Request{URI: path.Join(bpRestriction, fmt.Sprint(restrictionID))}
    50  	if err := r.GetInto(ctx, req, &output); err != nil {
    51  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    52  	}
    53  
    54  	return &output, nil
    55  }
    56  
    57  // AddRestriction creates a restriction.
    58  func (r *Radarr) AddRestriction(restriction *Restriction) (*Restriction, error) {
    59  	return r.AddRestrictionContext(context.Background(), restriction)
    60  }
    61  
    62  // AddRestrictionContext creates a restriction.
    63  func (r *Radarr) AddRestrictionContext(ctx context.Context, restriction *Restriction) (*Restriction, error) {
    64  	var output Restriction
    65  
    66  	var body bytes.Buffer
    67  	if err := json.NewEncoder(&body).Encode(restriction); err != nil {
    68  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpRestriction, err)
    69  	}
    70  
    71  	req := starr.Request{URI: bpRestriction, Body: &body}
    72  	if err := r.PostInto(ctx, req, &output); err != nil {
    73  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    74  	}
    75  
    76  	return &output, nil
    77  }
    78  
    79  // UpdateRestriction updates the restriction.
    80  func (r *Radarr) UpdateRestriction(restriction *Restriction) (*Restriction, error) {
    81  	return r.UpdateRestrictionContext(context.Background(), restriction)
    82  }
    83  
    84  // UpdateRestrictionContext updates the restriction.
    85  func (r *Radarr) UpdateRestrictionContext(ctx context.Context, restriction *Restriction) (*Restriction, error) {
    86  	var output Restriction
    87  
    88  	var body bytes.Buffer
    89  	if err := json.NewEncoder(&body).Encode(restriction); err != nil {
    90  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpRestriction, err)
    91  	}
    92  
    93  	req := starr.Request{URI: path.Join(bpRestriction, fmt.Sprint(restriction.ID)), Body: &body}
    94  	if err := r.PutInto(ctx, req, &output); err != nil {
    95  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
    96  	}
    97  
    98  	return &output, nil
    99  }
   100  
   101  // DeleteRestriction removes a single restriction.
   102  func (r *Radarr) DeleteRestriction(restrictionID int64) error {
   103  	return r.DeleteRestrictionContext(context.Background(), restrictionID)
   104  }
   105  
   106  // DeleteRestrictionContext removes a single restriction.
   107  func (r *Radarr) DeleteRestrictionContext(ctx context.Context, restrictionID int64) error {
   108  	req := starr.Request{URI: path.Join(bpRestriction, fmt.Sprint(restrictionID))}
   109  	if err := r.DeleteAny(ctx, req); err != nil {
   110  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   111  	}
   112  
   113  	return nil
   114  }