golift.io/starr@v1.0.0/lidarr/importlist.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 bpImportList = APIver + "/importlist"
    15  
    16  // ImportListInput is the input for a new or updated import list.
    17  type ImportListInput struct {
    18  	EnableAutomaticAdd    bool                `json:"enableAutomaticAdd"`
    19  	ShouldMonitorExisting bool                `json:"shouldMonitorExisting"`
    20  	ShouldSearch          bool                `json:"shouldSearch"`
    21  	ListOrder             int                 `json:"listOrder"`
    22  	ID                    int64               `json:"id,omitempty"` // for update not add.
    23  	QualityProfileID      int64               `json:"qualityProfileId,omitempty"`
    24  	MetadataProfileID     int64               `json:"metadataProfileId,omitempty"`
    25  	ConfigContract        string              `json:"configContract,omitempty"`
    26  	Implementation        string              `json:"implementation,omitempty"`
    27  	ListType              string              `json:"listType,omitempty"`
    28  	MonitorNewItems       string              `json:"monitorNewItems,omitempty"`
    29  	Name                  string              `json:"name,omitempty"`
    30  	RootFolderPath        string              `json:"rootFolderPath,omitempty"`
    31  	ShouldMonitor         string              `json:"shouldMonitor,omitempty"`
    32  	Tags                  []int               `json:"tags,omitempty"`
    33  	Fields                []*starr.FieldInput `json:"fields,omitempty"`
    34  }
    35  
    36  // ImportListOutput is the output from the import list methods.
    37  type ImportListOutput struct {
    38  	EnableAutomaticAdd    bool                 `json:"enableAutomaticAdd"`
    39  	ShouldMonitorExisting bool                 `json:"shouldMonitorExisting"`
    40  	ShouldSearch          bool                 `json:"shouldSearch"`
    41  	ListOrder             int                  `json:"listOrder"`
    42  	ID                    int64                `json:"id"`
    43  	QualityProfileID      int64                `json:"qualityProfileId"`
    44  	MetadataProfileID     int64                `json:"metadataProfileId"`
    45  	ShouldMonitor         string               `json:"shouldMonitor"`
    46  	RootFolderPath        string               `json:"rootFolderPath"`
    47  	MonitorNewItems       string               `json:"monitorNewItems"`
    48  	ListType              string               `json:"listType"`
    49  	Name                  string               `json:"name"`
    50  	ImplementationName    string               `json:"implementationName"`
    51  	Implementation        string               `json:"implementation"`
    52  	ConfigContract        string               `json:"configContract"`
    53  	InfoLink              string               `json:"infoLink"`
    54  	Tags                  []int                `json:"tags"`
    55  	Fields                []*starr.FieldOutput `json:"fields"`
    56  	Message               struct {
    57  		Message string `json:"message"` // this is a weird place for a message
    58  		Type    string `json:"type"`
    59  	} `json:"message"`
    60  }
    61  
    62  // GetImportLists returns all configured import lists.
    63  func (l *Lidarr) GetImportLists() ([]*ImportListOutput, error) {
    64  	return l.GetImportListsContext(context.Background())
    65  }
    66  
    67  // GetImportListsContext returns all configured import lists.
    68  func (l *Lidarr) GetImportListsContext(ctx context.Context) ([]*ImportListOutput, error) {
    69  	var output []*ImportListOutput
    70  
    71  	req := starr.Request{URI: bpImportList}
    72  	if err := l.GetInto(ctx, req, &output); err != nil {
    73  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    74  	}
    75  
    76  	return output, nil
    77  }
    78  
    79  // GetImportList returns a single import list.
    80  func (l *Lidarr) GetImportList(importListID int64) (*ImportListOutput, error) {
    81  	return l.GetImportListContext(context.Background(), importListID)
    82  }
    83  
    84  // GetIndGetImportListContextexer returns a single import list.
    85  func (l *Lidarr) GetImportListContext(ctx context.Context, importListID int64) (*ImportListOutput, error) {
    86  	var output ImportListOutput
    87  
    88  	req := starr.Request{URI: path.Join(bpImportList, fmt.Sprint(importListID))}
    89  	if err := l.GetInto(ctx, req, &output); err != nil {
    90  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    91  	}
    92  
    93  	return &output, nil
    94  }
    95  
    96  // AddImportList creates a import list.
    97  func (l *Lidarr) AddImportList(importList *ImportListInput) (*ImportListOutput, error) {
    98  	return l.AddImportListContext(context.Background(), importList)
    99  }
   100  
   101  // AddImportListContext creates a import list.
   102  func (l *Lidarr) AddImportListContext(ctx context.Context, importList *ImportListInput) (*ImportListOutput, error) {
   103  	var output ImportListOutput
   104  
   105  	var body bytes.Buffer
   106  	if err := json.NewEncoder(&body).Encode(importList); err != nil {
   107  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpImportList, err)
   108  	}
   109  
   110  	req := starr.Request{URI: bpImportList, Body: &body}
   111  	if err := l.PostInto(ctx, req, &output); err != nil {
   112  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
   113  	}
   114  
   115  	return &output, nil
   116  }
   117  
   118  // TestImportList tests an import list.
   119  func (l *Lidarr) TestImportList(list *ImportListInput) error {
   120  	return l.TestImportListContextt(context.Background(), list)
   121  }
   122  
   123  // TestImportListContextt tests an import list.
   124  func (l *Lidarr) TestImportListContextt(ctx context.Context, list *ImportListInput) error {
   125  	var output interface{}
   126  
   127  	var body bytes.Buffer
   128  	if err := json.NewEncoder(&body).Encode(list); err != nil {
   129  		return fmt.Errorf("json.Marshal(%s): %w", bpImportList, err)
   130  	}
   131  
   132  	req := starr.Request{URI: path.Join(bpImportList, "test"), Body: &body}
   133  	if err := l.PostInto(ctx, req, &output); err != nil {
   134  		return fmt.Errorf("api.Post(%s): %w", &req, err)
   135  	}
   136  
   137  	return nil
   138  }
   139  
   140  // UpdateImportList updates the import list.
   141  func (l *Lidarr) UpdateImportList(importList *ImportListInput, force bool) (*ImportListOutput, error) {
   142  	return l.UpdateImportListContext(context.Background(), importList, force)
   143  }
   144  
   145  // UpdateImportListContext updates the import list.
   146  func (l *Lidarr) UpdateImportListContext(
   147  	ctx context.Context,
   148  	importList *ImportListInput,
   149  	force bool,
   150  ) (*ImportListOutput, error) {
   151  	var output ImportListOutput
   152  
   153  	var body bytes.Buffer
   154  	if err := json.NewEncoder(&body).Encode(importList); err != nil {
   155  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpImportList, err)
   156  	}
   157  
   158  	req := starr.Request{
   159  		URI:   path.Join(bpImportList, fmt.Sprint(importList.ID)),
   160  		Body:  &body,
   161  		Query: url.Values{"forceSave": []string{fmt.Sprint(force)}},
   162  	}
   163  	if err := l.PutInto(ctx, req, &output); err != nil {
   164  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   165  	}
   166  
   167  	return &output, nil
   168  }
   169  
   170  // DeleteImportList removes a single import list.
   171  func (l *Lidarr) DeleteImportList(importListID int64) error {
   172  	return l.DeleteImportListContext(context.Background(), importListID)
   173  }
   174  
   175  // DeleteImportListContext removes a single import list.
   176  func (l *Lidarr) DeleteImportListContext(ctx context.Context, importListID int64) error {
   177  	req := starr.Request{URI: path.Join(bpImportList, fmt.Sprint(importListID))}
   178  	if err := l.DeleteAny(ctx, req); err != nil {
   179  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   180  	}
   181  
   182  	return nil
   183  }