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