golift.io/starr@v1.0.0/prowlarr/indexer.go (about) 1 package prowlarr 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "net/url" 9 "path" 10 "time" 11 12 "golift.io/starr" 13 ) 14 15 const bpIndexer = APIver + "/indexer" 16 17 // IndexerInput is the input for a new or updated indexer. 18 type IndexerInput struct { 19 Enable bool `json:"enable"` 20 Redirect bool `json:"redirect"` 21 Priority int64 `json:"priority"` 22 ID int64 `json:"id,omitempty"` 23 AppProfileID int64 `json:"appProfileId"` 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,omitempty"` 29 Fields []*starr.FieldInput `json:"fields"` 30 } 31 32 // IndexerOutput is the output from the indexer methods. 33 type IndexerOutput struct { 34 Enable bool `json:"enable"` 35 Redirect bool `json:"redirect"` 36 SupportsRss bool `json:"supportsRss"` 37 SupportsSearch bool `json:"supportsSearch"` 38 SupportsRedirect bool `json:"supportsRedirect"` 39 AppProfileID int64 `json:"appProfileId"` 40 ID int64 `json:"id,omitempty"` 41 Priority int64 `json:"priority"` 42 SortName string `json:"sortName"` 43 Name string `json:"name"` 44 Protocol string `json:"protocol"` 45 Privacy string `json:"privacy"` 46 DefinitionName string `json:"definitionName"` 47 Description string `json:"description"` 48 Language string `json:"language"` 49 Encoding string `json:"encoding,omitempty"` 50 ImplementationName string `json:"implementationName"` 51 Implementation string `json:"implementation"` 52 ConfigContract string `json:"configContract"` 53 InfoLink string `json:"infoLink"` 54 Added time.Time `json:"added"` 55 Capabilities *Capabilities `json:"capabilities,omitempty"` 56 Tags []int `json:"tags"` 57 IndexerUrls []string `json:"indexerUrls"` 58 LegacyUrls []string `json:"legacyUrls"` 59 Fields []*starr.FieldOutput `json:"fields"` 60 } 61 62 // Capabilities is part of IndexerOutput. 63 type Capabilities struct { 64 SupportsRawSearch bool `json:"supportsRawSearch"` 65 LimitsMax int64 `json:"limitsMax"` 66 LimitsDefault int64 `json:"limitsDefault"` 67 SearchParams []string `json:"searchParams"` 68 TvSearchParams []string `json:"tvSearchParams"` 69 MovieSearchParams []string `json:"movieSearchParams"` 70 MusicSearchParams []string `json:"musicSearchParams"` 71 BookSearchParams []string `json:"bookSearchParams"` 72 Categories []*Categories `json:"categories"` 73 } 74 75 // Categories is part of Capabilities. 76 type Categories struct { 77 ID int64 `json:"id"` 78 Name string `json:"name"` 79 SubCategories []*Categories `json:"subCategories"` 80 } 81 82 // GetIndexers returns all configured indexers. 83 func (p *Prowlarr) GetIndexers() ([]*IndexerOutput, error) { 84 return p.GetIndexersContext(context.Background()) 85 } 86 87 // GetIndexersContext returns all configured indexers. 88 func (p *Prowlarr) GetIndexersContext(ctx context.Context) ([]*IndexerOutput, error) { 89 var output []*IndexerOutput 90 91 req := starr.Request{URI: bpIndexer} 92 if err := p.GetInto(ctx, req, &output); err != nil { 93 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 94 } 95 96 return output, nil 97 } 98 99 // TestIndexer tests an indexer. 100 func (p *Prowlarr) TestIndexer(indexer *IndexerInput) error { 101 return p.TestIndexerContext(context.Background(), indexer) 102 } 103 104 // TestIndexerContext tests an indexer. 105 func (p *Prowlarr) TestIndexerContext(ctx context.Context, indexer *IndexerInput) error { 106 var output interface{} 107 108 var body bytes.Buffer 109 if err := json.NewEncoder(&body).Encode(indexer); err != nil { 110 return fmt.Errorf("json.Marshal(%s): %w", bpIndexer, err) 111 } 112 113 req := starr.Request{URI: path.Join(bpIndexer, "test"), Body: &body} 114 if err := p.PostInto(ctx, req, &output); err != nil { 115 return fmt.Errorf("api.Post(%s): %w", &req, err) 116 } 117 118 return nil 119 } 120 121 // GetIndexer returns a single indexer. 122 func (p *Prowlarr) GetIndexer(indexerID int64) (*IndexerOutput, error) { 123 return p.GetIndexerContext(context.Background(), indexerID) 124 } 125 126 // GetIndGetIndexerContextexer returns a single indexer. 127 func (p *Prowlarr) GetIndexerContext(ctx context.Context, indexerID int64) (*IndexerOutput, error) { 128 var output IndexerOutput 129 130 req := starr.Request{URI: path.Join(bpIndexer, fmt.Sprint(indexerID))} 131 if err := p.GetInto(ctx, req, &output); err != nil { 132 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 133 } 134 135 return &output, nil 136 } 137 138 // AddIndexer creates a indexer. 139 func (p *Prowlarr) AddIndexer(indexer *IndexerInput) (*IndexerOutput, error) { 140 return p.AddIndexerContext(context.Background(), indexer) 141 } 142 143 // AddIndexerContext creates a indexer. 144 func (p *Prowlarr) AddIndexerContext(ctx context.Context, indexer *IndexerInput) (*IndexerOutput, error) { 145 var output IndexerOutput 146 147 var body bytes.Buffer 148 if err := json.NewEncoder(&body).Encode(indexer); err != nil { 149 return nil, fmt.Errorf("json.Marshal(%s): %w", bpIndexer, err) 150 } 151 152 req := starr.Request{URI: bpIndexer, Body: &body} 153 if err := p.PostInto(ctx, req, &output); err != nil { 154 return nil, fmt.Errorf("api.Post(%s): %w", &req, err) 155 } 156 157 return &output, nil 158 } 159 160 // UpdateIndexer updates the indexer. 161 func (p *Prowlarr) UpdateIndexer(indexer *IndexerInput, force bool) (*IndexerOutput, error) { 162 return p.UpdateIndexerContext(context.Background(), indexer, force) 163 } 164 165 // UpdateIndexerContext updates the indexer. 166 func (p *Prowlarr) UpdateIndexerContext( 167 ctx context.Context, 168 indexer *IndexerInput, 169 force bool, 170 ) (*IndexerOutput, error) { 171 var output IndexerOutput 172 173 var body bytes.Buffer 174 if err := json.NewEncoder(&body).Encode(indexer); err != nil { 175 return nil, fmt.Errorf("json.Marshal(%s): %w", bpIndexer, err) 176 } 177 178 req := starr.Request{ 179 URI: path.Join(bpIndexer, fmt.Sprint(indexer.ID)), 180 Body: &body, 181 Query: url.Values{"forceSave": []string{fmt.Sprint(force)}}, 182 } 183 if err := p.PutInto(ctx, req, &output); err != nil { 184 return nil, fmt.Errorf("api.Put(%s): %w", &req, err) 185 } 186 187 return &output, nil 188 } 189 190 // DeleteIndexer removes a single indexer. 191 func (p *Prowlarr) DeleteIndexer(indexerID int64) error { 192 return p.DeleteIndexerContext(context.Background(), indexerID) 193 } 194 195 // DeleteIndexerContext removes a single indexer. 196 func (p *Prowlarr) DeleteIndexerContext(ctx context.Context, indexerID int64) error { 197 req := starr.Request{URI: path.Join(bpIndexer, fmt.Sprint(indexerID))} 198 if err := p.DeleteAny(ctx, req); err != nil { 199 return fmt.Errorf("api.Delete(%s): %w", &req, err) 200 } 201 202 return nil 203 }