golift.io/starr@v1.0.0/radarr/naming.go (about)

     1  package radarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  
     9  	"golift.io/starr"
    10  )
    11  
    12  // Define Base Path for Naming calls.
    13  const bpNaming = APIver + "/config/naming"
    14  
    15  // Naming represents the config/naming endpoint in Radarr.
    16  type Naming struct {
    17  	RenameMovies             bool   `json:"renameMovies,omitempty"`
    18  	ReplaceIllegalCharacters bool   `json:"replaceIllegalCharacters,omitempty"`
    19  	IncludeQuality           bool   `json:"includeQuality,omitempty"`
    20  	ReplaceSpaces            bool   `json:"replaceSpaces,omitempty"`
    21  	ID                       int64  `json:"id"` // ID must always be 1 (Oct 10, 2022)
    22  	ColonReplacementFormat   string `json:"colonReplacementFormat,omitempty"`
    23  	StandardMovieFormat      string `json:"standardMovieFormat"` // required
    24  	MovieFolderFormat        string `json:"movieFolderFormat"`   // required
    25  	Separator                string `json:"separatort,omitempty"`
    26  	NumberStyle              string `json:"numberStylet,omitempty"`
    27  }
    28  
    29  // GetNaming returns the file naming rules.
    30  func (r *Radarr) GetNaming() (*Naming, error) {
    31  	return r.GetNamingContext(context.Background())
    32  }
    33  
    34  // GetNamingContext returns the file naming rules.
    35  func (r *Radarr) GetNamingContext(ctx context.Context) (*Naming, error) {
    36  	var output Naming
    37  
    38  	req := starr.Request{URI: bpNaming}
    39  	if err := r.GetInto(ctx, req, &output); err != nil {
    40  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    41  	}
    42  
    43  	return &output, nil
    44  }
    45  
    46  // UpdateNaming updates the file naming rules.
    47  func (r *Radarr) UpdateNaming(naming *Naming) (*Naming, error) {
    48  	return r.UpdateNamingContext(context.Background(), naming)
    49  }
    50  
    51  // UpdateNamingContext updates the file naming rules.
    52  func (r *Radarr) UpdateNamingContext(ctx context.Context, naming *Naming) (*Naming, error) {
    53  	var output Naming
    54  
    55  	var body bytes.Buffer
    56  	if err := json.NewEncoder(&body).Encode(naming); err != nil {
    57  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpNaming, err)
    58  	}
    59  
    60  	req := starr.Request{URI: bpNaming, Body: &body}
    61  	if err := r.PutInto(ctx, req, &output); err != nil {
    62  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
    63  	}
    64  
    65  	return &output, nil
    66  }