golift.io/starr@v1.0.0/sonarr/indexer.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 bpIndexer = APIver + "/indexer" 15 16 // IndexerInput is the input for a new or updated indexer. 17 type IndexerInput struct { 18 EnableAutomaticSearch bool `json:"enableAutomaticSearch"` 19 EnableInteractiveSearch bool `json:"enableInteractiveSearch"` 20 EnableRss bool `json:"enableRss"` 21 DownloadClientID int64 `json:"downloadClientId"` 22 Priority int64 `json:"priority"` 23 ID int64 `json:"id,omitempty"` 24 ConfigContract string `json:"configContract"` 25 Implementation string `json:"implementation"` 26 Name string `json:"name"` 27 Protocol string `json:"protocol"` 28 Tags []int `json:"tags"` 29 Fields []*starr.FieldInput `json:"fields"` 30 } 31 32 // IndexerOutput is the output from the indexer methods. 33 type IndexerOutput struct { 34 EnableAutomaticSearch bool `json:"enableAutomaticSearch"` 35 EnableInteractiveSearch bool `json:"enableInteractiveSearch"` 36 EnableRss bool `json:"enableRss"` 37 SupportsRss bool `json:"supportsRss"` 38 SupportsSearch bool `json:"supportsSearch"` 39 DownloadClientID int64 `json:"downloadClientId"` 40 Priority int64 `json:"priority"` 41 ID int64 `json:"id,omitempty"` 42 ConfigContract string `json:"configContract"` 43 Implementation string `json:"implementation"` 44 ImplementationName string `json:"implementationName"` 45 InfoLink string `json:"infoLink"` 46 Name string `json:"name"` 47 Protocol string `json:"protocol"` 48 Tags []int `json:"tags"` 49 Fields []*starr.FieldOutput `json:"fields"` 50 } 51 52 // GetIndexers returns all configured indexers. 53 func (s *Sonarr) GetIndexers() ([]*IndexerOutput, error) { 54 return s.GetIndexersContext(context.Background()) 55 } 56 57 // GetIndexersContext returns all configured indexers. 58 func (s *Sonarr) GetIndexersContext(ctx context.Context) ([]*IndexerOutput, error) { 59 var output []*IndexerOutput 60 61 req := starr.Request{URI: bpIndexer} 62 if err := s.GetInto(ctx, req, &output); err != nil { 63 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 64 } 65 66 return output, nil 67 } 68 69 // GetIndexer returns a single indexer. 70 func (s *Sonarr) GetIndexer(indexerID int64) (*IndexerOutput, error) { 71 return s.GetIndexerContext(context.Background(), indexerID) 72 } 73 74 // GetIndGetIndexerContextexer returns a single indexer. 75 func (s *Sonarr) GetIndexerContext(ctx context.Context, indexerID int64) (*IndexerOutput, error) { 76 var output IndexerOutput 77 78 req := starr.Request{URI: path.Join(bpIndexer, fmt.Sprint(indexerID))} 79 if err := s.GetInto(ctx, req, &output); err != nil { 80 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 81 } 82 83 return &output, nil 84 } 85 86 // TestIndexer tests an indexer. 87 func (s *Sonarr) TestIndexer(indexer *IndexerInput) error { 88 return s.TestIndexerContext(context.Background(), indexer) 89 } 90 91 // TestIndexerContext tests an indexer. 92 func (s *Sonarr) TestIndexerContext(ctx context.Context, indexer *IndexerInput) error { 93 var output interface{} // any ok 94 95 var body bytes.Buffer 96 if err := json.NewEncoder(&body).Encode(indexer); err != nil { 97 return fmt.Errorf("json.Marshal(%s): %w", bpIndexer, err) 98 } 99 100 req := starr.Request{URI: path.Join(bpIndexer, "test"), Body: &body} 101 if err := s.PostInto(ctx, req, &output); err != nil { 102 return fmt.Errorf("api.Post(%s): %w", &req, err) 103 } 104 105 return nil 106 } 107 108 // AddIndexer creates a indexer. 109 func (s *Sonarr) AddIndexer(indexer *IndexerInput) (*IndexerOutput, error) { 110 return s.AddIndexerContext(context.Background(), indexer) 111 } 112 113 // AddIndexerContext creates a indexer. 114 func (s *Sonarr) AddIndexerContext(ctx context.Context, indexer *IndexerInput) (*IndexerOutput, error) { 115 var output IndexerOutput 116 117 var body bytes.Buffer 118 if err := json.NewEncoder(&body).Encode(indexer); err != nil { 119 return nil, fmt.Errorf("json.Marshal(%s): %w", bpIndexer, err) 120 } 121 122 req := starr.Request{URI: bpIndexer, Body: &body} 123 if err := s.PostInto(ctx, req, &output); err != nil { 124 return nil, fmt.Errorf("api.Post(%s): %w", &req, err) 125 } 126 127 return &output, nil 128 } 129 130 // UpdateIndexer updates the indexer. 131 func (s *Sonarr) UpdateIndexer(indexer *IndexerInput, force bool) (*IndexerOutput, error) { 132 return s.UpdateIndexerContext(context.Background(), indexer, force) 133 } 134 135 // UpdateIndexerContext updates the indexer. 136 func (s *Sonarr) UpdateIndexerContext( 137 ctx context.Context, 138 indexer *IndexerInput, 139 force bool, 140 ) (*IndexerOutput, error) { 141 var output IndexerOutput 142 143 var body bytes.Buffer 144 if err := json.NewEncoder(&body).Encode(indexer); err != nil { 145 return nil, fmt.Errorf("json.Marshal(%s): %w", bpIndexer, err) 146 } 147 148 req := starr.Request{ 149 URI: path.Join(bpIndexer, fmt.Sprint(indexer.ID)), 150 Body: &body, 151 Query: url.Values{"forceSave": []string{fmt.Sprint(force)}}, 152 } 153 if err := s.PutInto(ctx, req, &output); err != nil { 154 return nil, fmt.Errorf("api.Put(%s): %w", &req, err) 155 } 156 157 return &output, nil 158 } 159 160 // DeleteIndexer removes a single indexer. 161 func (s *Sonarr) DeleteIndexer(indexerID int64) error { 162 return s.DeleteIndexerContext(context.Background(), indexerID) 163 } 164 165 // DeleteIndexerContext removes a single indexer. 166 func (s *Sonarr) DeleteIndexerContext(ctx context.Context, indexerID int64) error { 167 req := starr.Request{URI: path.Join(bpIndexer, fmt.Sprint(indexerID))} 168 if err := s.DeleteAny(ctx, req); err != nil { 169 return fmt.Errorf("api.Delete(%s): %w", &req, err) 170 } 171 172 return nil 173 }