|
| 1 | +package readarr |
| 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 | + RenameBooks bool `json:"renameBooks"` |
| 18 | + ReplaceIllegalCharacters bool `json:"replaceIllegalCharacters"` |
| 19 | + IncludeAuthorName bool `json:"includeAuthorName"` |
| 20 | + IncludeBookTitle bool `json:"includeBookTitle"` |
| 21 | + IncludeQuality bool `json:"includeQuality"` |
| 22 | + ReplaceSpaces bool `json:"replaceSpaces"` |
| 23 | + ColonReplacementFormat CRF `json:"colonReplacementFormat"` |
| 24 | + ID int64 `json:"id"` |
| 25 | + StandardBookFormat string `json:"standardBookFormat"` |
| 26 | + AuthorFolderFormat string `json:"authorFolderFormat"` |
| 27 | +} |
| 28 | + |
| 29 | +// CRF is ColonReplacementFormat, for naming config. |
| 30 | +type CRF int |
| 31 | + |
| 32 | +// These are all of the possible Colon Replacement Formats (for naming config) in Readarr. |
| 33 | +const ( |
| 34 | + ColonDelete CRF = iota |
| 35 | + ColonReplaceWithDash |
| 36 | + ColonReplaceWithSpaceDash |
| 37 | + ColonReplaceWithSpaceDashSpace |
| 38 | + ColonSmartReplace |
| 39 | +) |
| 40 | + |
| 41 | +// GetNaming returns the file naming rules. |
| 42 | +func (r *Readarr) GetNaming() (*Naming, error) { |
| 43 | + return r.GetNamingContext(context.Background()) |
| 44 | +} |
| 45 | + |
| 46 | +// GetNamingContext returns the file naming rules. |
| 47 | +func (r *Readarr) GetNamingContext(ctx context.Context) (*Naming, error) { |
| 48 | + var output Naming |
| 49 | + |
| 50 | + req := starr.Request{URI: bpNaming} |
| 51 | + if err := r.GetInto(ctx, req, &output); err != nil { |
| 52 | + return nil, fmt.Errorf("api.Get(%s): %w", &req, err) |
| 53 | + } |
| 54 | + |
| 55 | + return &output, nil |
| 56 | +} |
| 57 | + |
| 58 | +// UpdateNaming updates the file naming rules. |
| 59 | +func (r *Readarr) UpdateNaming(naming *Naming) (*Naming, error) { |
| 60 | + return r.UpdateNamingContext(context.Background(), naming) |
| 61 | +} |
| 62 | + |
| 63 | +// UpdateNamingContext updates the file naming rules. |
| 64 | +func (r *Readarr) UpdateNamingContext(ctx context.Context, naming *Naming) (*Naming, error) { |
| 65 | + var ( |
| 66 | + output Naming |
| 67 | + body bytes.Buffer |
| 68 | + ) |
| 69 | + |
| 70 | + naming.ID = 1 |
| 71 | + if err := json.NewEncoder(&body).Encode(naming); err != nil { |
| 72 | + return nil, fmt.Errorf("json.Marshal(%s): %w", bpNaming, err) |
| 73 | + } |
| 74 | + |
| 75 | + req := starr.Request{URI: bpNaming, Body: &body} |
| 76 | + if err := r.PutInto(ctx, req, &output); err != nil { |
| 77 | + return nil, fmt.Errorf("api.Put(%s): %w", &req, err) |
| 78 | + } |
| 79 | + |
| 80 | + return &output, nil |
| 81 | +} |
0 commit comments