golift.io/starr@v1.0.0/sonarr/customformat.go (about)

     1  package sonarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"path"
     9  
    10  	"golift.io/starr"
    11  )
    12  
    13  /* Custom Formats do not exist in Sonarr v3; this is v4 only. */
    14  
    15  const bpCustomFormat = APIver + "/customFormat"
    16  
    17  // CustomFormatInput is the input for a new or updated CustomFormat.
    18  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
    19  type CustomFormatInput struct {
    20  	ID                    int64                    `json:"id,omitempty"`
    21  	Name                  string                   `json:"name"`
    22  	IncludeCFWhenRenaming bool                     `json:"includeCustomFormatWhenRenaming"`
    23  	Specifications        []*CustomFormatInputSpec `json:"specifications"`
    24  }
    25  
    26  // CustomFormatInputSpec is part of a CustomFormatInput.
    27  type CustomFormatInputSpec struct {
    28  	Name           string              `json:"name"`
    29  	Implementation string              `json:"implementation"`
    30  	Negate         bool                `json:"negate"`
    31  	Required       bool                `json:"required"`
    32  	Fields         []*starr.FieldInput `json:"fields"`
    33  }
    34  
    35  // CustomFormatOutput is the output from the CustomFormat methods.
    36  type CustomFormatOutput struct {
    37  	ID                    int64                     `json:"id"`
    38  	Name                  string                    `json:"name"`
    39  	IncludeCFWhenRenaming bool                      `json:"includeCustomFormatWhenRenaming"`
    40  	Specifications        []*CustomFormatOutputSpec `json:"specifications"`
    41  }
    42  
    43  // CustomFormatOutputSpec is part of a CustomFormatOutput.
    44  type CustomFormatOutputSpec struct {
    45  	Name               string               `json:"name"`
    46  	Implementation     string               `json:"implementation"`
    47  	ImplementationName string               `json:"implementationName"`
    48  	InfoLink           string               `json:"infoLink"`
    49  	Negate             bool                 `json:"negate"`
    50  	Required           bool                 `json:"required"`
    51  	Fields             []*starr.FieldOutput `json:"fields"`
    52  }
    53  
    54  // GetCustomFormats returns all configured Custom Formats.
    55  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
    56  func (s *Sonarr) GetCustomFormats() ([]*CustomFormatOutput, error) {
    57  	return s.GetCustomFormatsContext(context.Background())
    58  }
    59  
    60  // GetCustomFormatsContext returns all configured Custom Formats.
    61  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
    62  func (s *Sonarr) GetCustomFormatsContext(ctx context.Context) ([]*CustomFormatOutput, error) {
    63  	var output []*CustomFormatOutput
    64  
    65  	req := starr.Request{URI: bpCustomFormat}
    66  	if err := s.GetInto(ctx, req, &output); err != nil {
    67  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    68  	}
    69  
    70  	return output, nil
    71  }
    72  
    73  // GetCustomFormat returns a single customformat.
    74  func (s *Sonarr) GetCustomFormat(customformatID int64) (*CustomFormatOutput, error) {
    75  	return s.GetCustomFormatContext(context.Background(), customformatID)
    76  }
    77  
    78  // GetCustomFormatContext returns a single customformat.
    79  func (s *Sonarr) GetCustomFormatContext(ctx context.Context, customformatID int64) (*CustomFormatOutput, error) {
    80  	var output CustomFormatOutput
    81  
    82  	req := starr.Request{URI: path.Join(bpCustomFormat, fmt.Sprint(customformatID))}
    83  	if err := s.GetInto(ctx, req, &output); err != nil {
    84  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    85  	}
    86  
    87  	return &output, nil
    88  }
    89  
    90  // AddCustomFormat creates a new custom format and returns the response (with ID).
    91  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
    92  func (s *Sonarr) AddCustomFormat(format *CustomFormatInput) (*CustomFormatOutput, error) {
    93  	return s.AddCustomFormatContext(context.Background(), format)
    94  }
    95  
    96  // AddCustomFormatContext creates a new custom format and returns the response (with ID).
    97  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
    98  func (s *Sonarr) AddCustomFormatContext(ctx context.Context, format *CustomFormatInput) (*CustomFormatOutput, error) {
    99  	var output CustomFormatOutput
   100  
   101  	if format == nil {
   102  		return &output, nil
   103  	}
   104  
   105  	format.ID = 0 // ID must be zero when adding.
   106  
   107  	var body bytes.Buffer
   108  	if err := json.NewEncoder(&body).Encode(format); err != nil {
   109  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpCustomFormat, err)
   110  	}
   111  
   112  	req := starr.Request{URI: bpCustomFormat, Body: &body}
   113  	if err := s.PostInto(ctx, req, &output); err != nil {
   114  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
   115  	}
   116  
   117  	return &output, nil
   118  }
   119  
   120  // UpdateCustomFormat updates an existing custom format and returns the response.
   121  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
   122  func (s *Sonarr) UpdateCustomFormat(format *CustomFormatInput) (*CustomFormatOutput, error) {
   123  	return s.UpdateCustomFormatContext(context.Background(), format)
   124  }
   125  
   126  // UpdateCustomFormatContext updates an existing custom format and returns the response.
   127  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
   128  func (s *Sonarr) UpdateCustomFormatContext(
   129  	ctx context.Context,
   130  	format *CustomFormatInput,
   131  ) (*CustomFormatOutput, error) {
   132  	var body bytes.Buffer
   133  	if err := json.NewEncoder(&body).Encode(format); err != nil {
   134  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpCustomFormat, err)
   135  	}
   136  
   137  	var output CustomFormatOutput
   138  
   139  	req := starr.Request{URI: path.Join(bpCustomFormat, fmt.Sprint(format.ID)), Body: &body}
   140  	if err := s.PutInto(ctx, req, &output); err != nil {
   141  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   142  	}
   143  
   144  	return &output, nil
   145  }
   146  
   147  // DeleteCustomFormat deletes a custom format.
   148  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
   149  func (s *Sonarr) DeleteCustomFormat(formatID int64) error {
   150  	return s.DeleteCustomFormatContext(context.Background(), formatID)
   151  }
   152  
   153  // DeleteCustomFormatContext deletes a custom format.
   154  // This data and these endpoints do not exist in Sonarr v3; this is v4 only.
   155  func (s *Sonarr) DeleteCustomFormatContext(ctx context.Context, formatID int64) error {
   156  	req := starr.Request{URI: path.Join(bpCustomFormat, fmt.Sprint(formatID))}
   157  	if err := s.DeleteAny(ctx, req); err != nil {
   158  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   159  	}
   160  
   161  	return nil
   162  }