golift.io/starr@v1.0.0/radarr/customformat.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 bpCustomFormat = APIver + "/customFormat" 14 15 // CustomFormatInput is the input for a new or updated CustomFormat. 16 type CustomFormatInput struct { 17 ID int64 `json:"id,omitempty"` 18 Name string `json:"name"` 19 IncludeCFWhenRenaming bool `json:"includeCustomFormatWhenRenaming"` 20 Specifications []*CustomFormatInputSpec `json:"specifications"` 21 } 22 23 // CustomFormatInputSpec is part of a CustomFormatInput. 24 type CustomFormatInputSpec struct { 25 Name string `json:"name"` 26 Implementation string `json:"implementation"` 27 Negate bool `json:"negate"` 28 Required bool `json:"required"` 29 Fields []*starr.FieldInput `json:"fields"` 30 } 31 32 // CustomFormatOutput is the output from the CustomFormat methods. 33 type CustomFormatOutput struct { 34 ID int64 `json:"id"` 35 Name string `json:"name"` 36 IncludeCFWhenRenaming bool `json:"includeCustomFormatWhenRenaming"` 37 Specifications []*CustomFormatOutputSpec `json:"specifications"` 38 } 39 40 // CustomFormatOutputSpec is part of a CustomFormatOutput. 41 type CustomFormatOutputSpec struct { 42 Name string `json:"name"` 43 Implementation string `json:"implementation"` 44 ImplementationName string `json:"implementationName"` 45 InfoLink string `json:"infoLink"` 46 Negate bool `json:"negate"` 47 Required bool `json:"required"` 48 Fields []*starr.FieldOutput `json:"fields"` 49 } 50 51 // GetCustomFormats returns all configured Custom Formats. 52 func (r *Radarr) GetCustomFormats() ([]*CustomFormatOutput, error) { 53 return r.GetCustomFormatsContext(context.Background()) 54 } 55 56 // GetCustomFormatsContext returns all configured Custom Formats. 57 func (r *Radarr) GetCustomFormatsContext(ctx context.Context) ([]*CustomFormatOutput, error) { 58 var output []*CustomFormatOutput 59 60 req := starr.Request{URI: bpCustomFormat} 61 if err := r.GetInto(ctx, req, &output); err != nil { 62 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 63 } 64 65 return output, nil 66 } 67 68 // GetCustomFormat returns a single customformat. 69 func (r *Radarr) GetCustomFormat(customformatID int64) (*CustomFormatOutput, error) { 70 return r.GetCustomFormatContext(context.Background(), customformatID) 71 } 72 73 // GetCustomFormatContext returns a single customformat. 74 func (r *Radarr) GetCustomFormatContext(ctx context.Context, customformatID int64) (*CustomFormatOutput, error) { 75 var output CustomFormatOutput 76 77 req := starr.Request{URI: path.Join(bpCustomFormat, fmt.Sprint(customformatID))} 78 if err := r.GetInto(ctx, req, &output); err != nil { 79 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 80 } 81 82 return &output, nil 83 } 84 85 // AddCustomFormat creates a new custom format and returns the response (with ID). 86 func (r *Radarr) AddCustomFormat(format *CustomFormatInput) (*CustomFormatOutput, error) { 87 return r.AddCustomFormatContext(context.Background(), format) 88 } 89 90 // AddCustomFormatContext creates a new custom format and returns the response (with ID). 91 func (r *Radarr) AddCustomFormatContext(ctx context.Context, format *CustomFormatInput) (*CustomFormatOutput, error) { 92 var output CustomFormatOutput 93 94 var body bytes.Buffer 95 if err := json.NewEncoder(&body).Encode(format); err != nil { 96 return nil, fmt.Errorf("json.Marshal(%s): %w", bpCustomFormat, err) 97 } 98 99 req := starr.Request{URI: bpCustomFormat, Body: &body} 100 if err := r.PostInto(ctx, req, &output); err != nil { 101 return nil, fmt.Errorf("api.Post(%s): %w", &req, err) 102 } 103 104 return &output, nil 105 } 106 107 // UpdateCustomFormat updates an existing custom format and returns the response. 108 func (r *Radarr) UpdateCustomFormat(cf *CustomFormatInput) (*CustomFormatOutput, error) { 109 return r.UpdateCustomFormatContext(context.Background(), cf) 110 } 111 112 // UpdateCustomFormatContext updates an existing custom format and returns the response. 113 func (r *Radarr) UpdateCustomFormatContext(ctx context.Context, 114 format *CustomFormatInput, 115 ) (*CustomFormatOutput, error) { 116 var output CustomFormatOutput 117 118 var body bytes.Buffer 119 if err := json.NewEncoder(&body).Encode(format); err != nil { 120 return nil, fmt.Errorf("json.Marshal(%s): %w", bpCustomFormat, err) 121 } 122 123 req := starr.Request{URI: path.Join(bpCustomFormat, fmt.Sprint(format.ID)), Body: &body} 124 if err := r.PutInto(ctx, req, &output); err != nil { 125 return nil, fmt.Errorf("api.Put(%s): %w", &req, err) 126 } 127 128 return &output, nil 129 } 130 131 // DeleteCustomFormat deletes a custom format. 132 func (r *Radarr) DeleteCustomFormat(cfID int64) error { 133 return r.DeleteCustomFormatContext(context.Background(), cfID) 134 } 135 136 // DeleteCustomFormatContext deletes a custom format. 137 func (r *Radarr) DeleteCustomFormatContext(ctx context.Context, cfID int64) error { 138 req := starr.Request{URI: path.Join(bpCustomFormat, fmt.Sprint(cfID))} 139 if err := r.DeleteAny(ctx, req); err != nil { 140 return fmt.Errorf("api.Delete(%s): %w", &req, err) 141 } 142 143 return nil 144 }