golift.io/starr@v1.0.0/radarr/indexer.go (about)

     1  package radarr
     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 (r *Radarr) GetIndexers() ([]*IndexerOutput, error) {
    54  	return r.GetIndexersContext(context.Background())
    55  }
    56  
    57  // GetIndexersContext returns all configured indexers.
    58  func (r *Radarr) GetIndexersContext(ctx context.Context) ([]*IndexerOutput, error) {
    59  	var output []*IndexerOutput
    60  
    61  	req := starr.Request{URI: bpIndexer}
    62  	if err := r.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 (r *Radarr) GetIndexer(indexerID int64) (*IndexerOutput, error) {
    71  	return r.GetIndexerContext(context.Background(), indexerID)
    72  }
    73  
    74  // GetIndGetIndexerContextexer returns a single indexer.
    75  func (r *Radarr) 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 := r.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 (r *Radarr) TestIndexer(indexer *IndexerInput) error {
    88  	return r.TestIndexerContext(context.Background(), indexer)
    89  }
    90  
    91  // TestIndexerContext tests an indexer.
    92  func (r *Radarr) 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 := r.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 (r *Radarr) AddIndexer(indexer *IndexerInput) (*IndexerOutput, error) {
   110  	return r.AddIndexerContext(context.Background(), indexer)
   111  }
   112  
   113  // AddIndexerContext creates a indexer.
   114  func (r *Radarr) 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 := r.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 (r *Radarr) UpdateIndexer(indexer *IndexerInput, force bool) (*IndexerOutput, error) {
   132  	return r.UpdateIndexerContext(context.Background(), indexer, force)
   133  }
   134  
   135  // UpdateIndexerContext updates the indexer.
   136  func (r *Radarr) UpdateIndexerContext(ctx context.Context, indexer *IndexerInput, force bool) (*IndexerOutput, error) {
   137  	var output IndexerOutput
   138  
   139  	var body bytes.Buffer
   140  	if err := json.NewEncoder(&body).Encode(indexer); err != nil {
   141  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpIndexer, err)
   142  	}
   143  
   144  	req := starr.Request{
   145  		URI:   path.Join(bpIndexer, fmt.Sprint(indexer.ID)),
   146  		Body:  &body,
   147  		Query: url.Values{"forceSave": []string{fmt.Sprint(force)}},
   148  	}
   149  	if err := r.PutInto(ctx, req, &output); err != nil {
   150  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   151  	}
   152  
   153  	return &output, nil
   154  }
   155  
   156  // DeleteIndexer removes a single indexer.
   157  func (r *Radarr) DeleteIndexer(indexerID int64) error {
   158  	return r.DeleteIndexerContext(context.Background(), indexerID)
   159  }
   160  
   161  // DeleteIndexerContext removes a single indexer.
   162  func (r *Radarr) DeleteIndexerContext(ctx context.Context, indexerID int64) error {
   163  	req := starr.Request{URI: path.Join(bpIndexer, fmt.Sprint(indexerID))}
   164  	if err := r.DeleteAny(ctx, req); err != nil {
   165  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   166  	}
   167  
   168  	return nil
   169  }