golift.io/starr@v1.0.0/readarr/importlist.go (about) 1 package readarr 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "net/url" 9 "path" 10 11 "golift.io/starr" 12 ) 13 14 const bpImportList = APIver + "/importlist" 15 16 // ImportListInput is the input for a new or updated import list. 17 type ImportListInput struct { 18 EnableAutomaticAdd bool `json:"enableAutomaticAdd"` 19 ShouldMonitorExisting bool `json:"shouldMonitorExisting"` 20 ShouldSearch bool `json:"shouldSearch"` 21 ListOrder int `json:"listOrder"` 22 ID int64 `json:"id,omitempty"` // for update not add. 23 MetadataProfileID int64 `json:"metadataProfileId,omitempty"` 24 QualityProfileID int64 `json:"qualityProfileId,omitempty"` 25 ListType string `json:"listType,omitempty"` 26 ConfigContract string `json:"configContract,omitempty"` 27 Implementation string `json:"implementation,omitempty"` 28 Name string `json:"name,omitempty"` 29 RootFolderPath string `json:"rootFolderPath,omitempty"` 30 ShouldMonitor string `json:"shouldMonitor,omitempty"` 31 MonitorNewItems string `json:"monitorNewItems,omitempty"` 32 Tags []int `json:"tags,omitempty"` 33 Fields []*starr.FieldInput `json:"fields,omitempty"` 34 } 35 36 // ImportListOutput is the output from the import list methods. 37 type ImportListOutput struct { 38 EnableAutomaticAdd bool `json:"enableAutomaticAdd"` 39 ShouldMonitorExisting bool `json:"shouldMonitorExisting"` 40 ShouldSearch bool `json:"shouldSearch"` 41 ID int64 `json:"id"` 42 ListOrder int64 `json:"listOrder"` 43 MetadataProfileID int64 `json:"metadataProfileId"` 44 QualityProfileID int64 `json:"qualityProfileId"` 45 ConfigContract string `json:"configContract"` 46 Implementation string `json:"implementation"` 47 ImplementationName string `json:"implementationName"` 48 InfoLink string `json:"infoLink"` 49 ListType string `json:"listType"` 50 MonitorNewItems string `json:"monitorNewItems"` 51 Name string `json:"name"` 52 RootFolderPath string `json:"rootFolderPath"` 53 ShouldMonitor string `json:"shouldMonitor"` 54 Tags []int `json:"tags"` 55 Fields []*starr.FieldOutput `json:"fields"` 56 } 57 58 // GetImportLists returns all configured import lists. 59 func (r *Readarr) GetImportLists() ([]*ImportListOutput, error) { 60 return r.GetImportListsContext(context.Background()) 61 } 62 63 // GetImportListsContext returns all configured import lists. 64 func (r *Readarr) GetImportListsContext(ctx context.Context) ([]*ImportListOutput, error) { 65 var output []*ImportListOutput 66 67 req := starr.Request{URI: bpImportList} 68 if err := r.GetInto(ctx, req, &output); err != nil { 69 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 70 } 71 72 return output, nil 73 } 74 75 // GetImportList returns a single import list. 76 func (r *Readarr) GetImportList(importListID int64) (*ImportListOutput, error) { 77 return r.GetImportListContext(context.Background(), importListID) 78 } 79 80 // GetIndGetImportListContextexer returns a single import list. 81 func (r *Readarr) GetImportListContext(ctx context.Context, importListID int64) (*ImportListOutput, error) { 82 var output ImportListOutput 83 84 req := starr.Request{URI: path.Join(bpImportList, fmt.Sprint(importListID))} 85 if err := r.GetInto(ctx, req, &output); err != nil { 86 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 87 } 88 89 return &output, nil 90 } 91 92 // AddImportList creates a import list. 93 func (r *Readarr) AddImportList(importList *ImportListInput) (*ImportListOutput, error) { 94 return r.AddImportListContext(context.Background(), importList) 95 } 96 97 // AddImportListContext creates a import list. 98 func (r *Readarr) AddImportListContext(ctx context.Context, importList *ImportListInput) (*ImportListOutput, error) { 99 var output ImportListOutput 100 101 var body bytes.Buffer 102 if err := json.NewEncoder(&body).Encode(importList); err != nil { 103 return nil, fmt.Errorf("json.Marshal(%s): %w", bpImportList, err) 104 } 105 106 req := starr.Request{URI: bpImportList, Body: &body} 107 if err := r.PostInto(ctx, req, &output); err != nil { 108 return nil, fmt.Errorf("api.Post(%s): %w", &req, err) 109 } 110 111 return &output, nil 112 } 113 114 // TestImportList tests an import list. 115 func (r *Readarr) TestImportList(list *ImportListInput) error { 116 return r.TestImportListContextt(context.Background(), list) 117 } 118 119 // TestImportListContextt tests an import list. 120 func (r *Readarr) TestImportListContextt(ctx context.Context, list *ImportListInput) error { 121 var output interface{} 122 123 var body bytes.Buffer 124 if err := json.NewEncoder(&body).Encode(list); err != nil { 125 return fmt.Errorf("json.Marshal(%s): %w", bpImportList, err) 126 } 127 128 req := starr.Request{URI: path.Join(bpImportList, "test"), Body: &body} 129 if err := r.PostInto(ctx, req, &output); err != nil { 130 return fmt.Errorf("api.Post(%s): %w", &req, err) 131 } 132 133 return nil 134 } 135 136 // UpdateImportList updates the import list. 137 func (r *Readarr) UpdateImportList(importList *ImportListInput, force bool) (*ImportListOutput, error) { 138 return r.UpdateImportListContext(context.Background(), importList, force) 139 } 140 141 // UpdateImportListContext updates the import list. 142 func (r *Readarr) UpdateImportListContext( 143 ctx context.Context, 144 importList *ImportListInput, 145 force bool, 146 ) (*ImportListOutput, error) { 147 var output ImportListOutput 148 149 var body bytes.Buffer 150 if err := json.NewEncoder(&body).Encode(importList); err != nil { 151 return nil, fmt.Errorf("json.Marshal(%s): %w", bpImportList, err) 152 } 153 154 req := starr.Request{ 155 URI: path.Join(bpImportList, fmt.Sprint(importList.ID)), 156 Body: &body, 157 Query: url.Values{"forceSave": []string{fmt.Sprint(force)}}, 158 } 159 if err := r.PutInto(ctx, req, &output); err != nil { 160 return nil, fmt.Errorf("api.Put(%s): %w", &req, err) 161 } 162 163 return &output, nil 164 } 165 166 // DeleteImportList removes a single import list. 167 func (r *Readarr) DeleteImportList(importListID int64) error { 168 return r.DeleteImportListContext(context.Background(), importListID) 169 } 170 171 // DeleteImportListContext removes a single import list. 172 func (r *Readarr) DeleteImportListContext(ctx context.Context, importListID int64) error { 173 req := starr.Request{URI: path.Join(bpImportList, fmt.Sprint(importListID))} 174 if err := r.DeleteAny(ctx, req); err != nil { 175 return fmt.Errorf("api.Delete(%s): %w", &req, err) 176 } 177 178 return nil 179 }