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

     1  package lidarr
     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 (l *Lidarr) GetIndexers() ([]*IndexerOutput, error) {
    52  	return l.GetIndexersContext(context.Background())
    53  }
    54  
    55  // GetIndexersContext returns all configured indexers.
    56  func (l *Lidarr) GetIndexersContext(ctx context.Context) ([]*IndexerOutput, error) {
    57  	var output []*IndexerOutput
    58  
    59  	req := starr.Request{URI: bpIndexer}
    60  	if err := l.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 (l *Lidarr) GetIndexer(indexerID int64) (*IndexerOutput, error) {
    69  	return l.GetIndexerContext(context.Background(), indexerID)
    70  }
    71  
    72  // TestIndexer tests an indexer.
    73  func (l *Lidarr) TestIndexer(indexer *IndexerInput) error {
    74  	return l.TestIndexerContext(context.Background(), indexer)
    75  }
    76  
    77  // TestIndexerContext tests an indexer.
    78  func (l *Lidarr) TestIndexerContext(ctx context.Context, indexer *IndexerInput) error {
    79  	var output interface{}
    80  
    81  	var body bytes.Buffer
    82  	if err := json.NewEncoder(&body).Encode(indexer); err != nil {
    83  		return fmt.Errorf("json.Marshal(%s): %w", bpIndexer, err)
    84  	}
    85  
    86  	req := starr.Request{URI: path.Join(bpIndexer, "test"), Body: &body}
    87  	if err := l.PostInto(ctx, req, &output); err != nil {
    88  		return fmt.Errorf("api.Post(%s): %w", &req, err)
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  // GetIndGetIndexerContextexer returns a single indexer.
    95  func (l *Lidarr) GetIndexerContext(ctx context.Context, indexerID int64) (*IndexerOutput, error) {
    96  	var output IndexerOutput
    97  
    98  	req := starr.Request{URI: path.Join(bpIndexer, fmt.Sprint(indexerID))}
    99  	if err := l.GetInto(ctx, req, &output); err != nil {
   100  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   101  	}
   102  
   103  	return &output, nil
   104  }
   105  
   106  // AddIndexer creates a indexer.
   107  func (l *Lidarr) AddIndexer(indexer *IndexerInput) (*IndexerOutput, error) {
   108  	return l.AddIndexerContext(context.Background(), indexer)
   109  }
   110  
   111  // AddIndexerContext creates a indexer.
   112  func (l *Lidarr) 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 := l.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 an indexer.
   129  func (l *Lidarr) UpdateIndexer(indexer *IndexerInput, force bool) (*IndexerOutput, error) {
   130  	return l.UpdateIndexerContext(context.Background(), indexer, force)
   131  }
   132  
   133  // UpdateIndexerContext updates an indexer.
   134  func (l *Lidarr) 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 := l.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 (l *Lidarr) DeleteIndexer(indexerID int64) error {
   156  	return l.DeleteIndexerContext(context.Background(), indexerID)
   157  }
   158  
   159  // DeleteIndexerContext removes a single indexer.
   160  func (l *Lidarr) DeleteIndexerContext(ctx context.Context, indexerID int64) error {
   161  	req := starr.Request{URI: path.Join(bpIndexer, fmt.Sprint(indexerID))}
   162  	if err := l.DeleteAny(ctx, req); err != nil {
   163  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   164  	}
   165  
   166  	return nil
   167  }