golift.io/starr@v1.0.0/sonarr/importlist.go (about) 1 package sonarr 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 SeasonFolder bool `json:"seasonFolder"` 20 ListOrder int `json:"listOrder"` 21 QualityProfileID int64 `json:"qualityProfileId,omitempty"` 22 ID int64 `json:"id,omitempty"` // update only. 23 ConfigContract string `json:"configContract,omitempty"` 24 Implementation string `json:"implementation,omitempty"` 25 ImplementationName string `json:"implementationName,omitempty"` 26 InfoLink string `json:"infoLink,omitempty"` 27 ListType string `json:"listType,omitempty"` 28 MinRefreshInterval string `json:"minRefreshInterval,omitempty"` 29 Name string `json:"name,omitempty"` 30 RootFolderPath string `json:"rootFolderPath,omitempty"` 31 SeriesType string `json:"seriesType,omitempty"` 32 ShouldMonitor string `json:"shouldMonitor,omitempty"` 33 Tags []int `json:"tags,omitempty"` 34 Fields []*starr.FieldInput `json:"fields,omitempty"` 35 } 36 37 // ImportListOutput is the output from the import list methods. 38 type ImportListOutput struct { 39 EnableAutomaticAdd bool `json:"enableAutomaticAdd"` 40 SeasonFolder bool `json:"seasonFolder"` 41 QualityProfileID int64 `json:"qualityProfileId"` 42 ListOrder int64 `json:"listOrder"` 43 ID int64 `json:"id"` 44 ConfigContract string `json:"configContract"` 45 Implementation string `json:"implementation"` 46 ImplementationName string `json:"implementationName"` 47 InfoLink string `json:"infoLink"` 48 ListType string `json:"listType"` 49 MinRefreshInterval string `json:"minRefreshInterval"` 50 Name string `json:"name"` 51 RootFolderPath string `json:"rootFolderPath"` 52 SeriesType string `json:"seriesType"` 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 (s *Sonarr) GetImportLists() ([]*ImportListOutput, error) { 60 return s.GetImportListsContext(context.Background()) 61 } 62 63 // GetImportListsContext returns all configured import lists. 64 func (s *Sonarr) GetImportListsContext(ctx context.Context) ([]*ImportListOutput, error) { 65 var output []*ImportListOutput 66 67 req := starr.Request{URI: bpImportList} 68 if err := s.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 (s *Sonarr) GetImportList(importListID int64) (*ImportListOutput, error) { 77 return s.GetImportListContext(context.Background(), importListID) 78 } 79 80 // GetIndGetImportListContextexer returns a single import list. 81 func (s *Sonarr) 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 := s.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 (s *Sonarr) AddImportList(importList *ImportListInput) (*ImportListOutput, error) { 94 return s.AddImportListContext(context.Background(), importList) 95 } 96 97 // AddImportListContext creates a import list. 98 func (s *Sonarr) 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 := s.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 (s *Sonarr) TestImportList(list *ImportListInput) error { 116 return s.TestImportListContextt(context.Background(), list) 117 } 118 119 // TestImportListContextt tests an import list. 120 func (s *Sonarr) TestImportListContextt(ctx context.Context, list *ImportListInput) error { 121 var output interface{} // any ok 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 := s.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 (s *Sonarr) UpdateImportList(importList *ImportListInput, force bool) (*ImportListOutput, error) { 138 return s.UpdateImportListContext(context.Background(), importList, force) 139 } 140 141 // UpdateImportListContext updates the import list. 142 func (s *Sonarr) 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 := s.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 (s *Sonarr) DeleteImportList(importListID int64) error { 168 return s.DeleteImportListContext(context.Background(), importListID) 169 } 170 171 // DeleteImportListContext removes a single import list. 172 func (s *Sonarr) DeleteImportListContext(ctx context.Context, importListID int64) error { 173 req := starr.Request{URI: path.Join(bpImportList, fmt.Sprint(importListID))} 174 if err := s.DeleteAny(ctx, req); err != nil { 175 return fmt.Errorf("api.Delete(%s): %w", &req, err) 176 } 177 178 return nil 179 }