golift.io/starr@v1.0.0/radarr/importlist.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 bpImportList = APIver + "/importlist"
    15  
    16  // ImportList represents the api/v3/importlist endpoint.
    17  type ImportListInput struct {
    18  	EnableAuto          bool                `json:"enableAuto"`
    19  	Enabled             bool                `json:"enabled"`
    20  	SearchOnAdd         bool                `json:"searchOnAdd"`
    21  	ListOrder           int                 `json:"listOrder"`
    22  	ID                  int64               `json:"id,omitempty"`
    23  	QualityProfileID    int64               `json:"qualityProfileId,omitempty"`
    24  	ConfigContract      string              `json:"configContract,omitempty"`
    25  	Implementation      string              `json:"implementation,omitempty"`
    26  	ImplementationName  string              `json:"implementationName,omitempty"`
    27  	InfoLink            string              `json:"infoLink,omitempty"`
    28  	ListType            string              `json:"listType,omitempty"`
    29  	Monitor             string              `json:"monitor,omitempty"`
    30  	Name                string              `json:"name,omitempty"`
    31  	RootFolderPath      string              `json:"rootFolderPath,omitempty"`
    32  	MinimumAvailability Availability        `json:"minimumAvailability,omitempty"`
    33  	Tags                []int               `json:"tags,omitempty"`
    34  	Fields              []*starr.FieldInput `json:"fields,omitempty"`
    35  }
    36  
    37  // ImportList represents the api/v3/importlist endpoint.
    38  type ImportListOutput struct {
    39  	EnableAuto          bool                 `json:"enableAuto"`
    40  	Enabled             bool                 `json:"enabled"`
    41  	SearchOnAdd         bool                 `json:"searchOnAdd"`
    42  	ID                  int64                `json:"id"`
    43  	ListOrder           int64                `json:"listOrder"`
    44  	QualityProfileID    int64                `json:"qualityProfileId"`
    45  	ConfigContract      string               `json:"configContract"`
    46  	Implementation      string               `json:"implementation"`
    47  	ImplementationName  string               `json:"implementationName"`
    48  	InfoLink            string               `json:"infoLink"`
    49  	Monitor             string               `json:"monitor"`
    50  	ListType            string               `json:"listType"`
    51  	Name                string               `json:"name"`
    52  	RootFolderPath      string               `json:"rootFolderPath"`
    53  	MinimumAvailability Availability         `json:"minimumAvailability"`
    54  	Tags                []int                `json:"tags"`
    55  	Fields              []*starr.FieldOutput `json:"fields"`
    56  }
    57  
    58  // GetImportLists returns all import lists.
    59  func (r *Radarr) GetImportLists() ([]*ImportListOutput, error) {
    60  	return r.GetImportListsContext(context.Background())
    61  }
    62  
    63  // GetImportListsContext returns all import lists.
    64  func (r *Radarr) GetImportListsContext(ctx context.Context) ([]*ImportListOutput, error) {
    65  	var output []*ImportListOutput
    66  
    67  	req := starr.Request{URI: bpImportList}
    68  	if err := r.GetInto(ctx, req, &output); err != nil {
    69  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    70  	}
    71  
    72  	return output, nil
    73  }
    74  
    75  // CreateImportList creates an import list in Radarr.
    76  func (r *Radarr) CreateImportList(list *ImportListInput) (*ImportListOutput, error) {
    77  	return r.CreateImportListContext(context.Background(), list)
    78  }
    79  
    80  // CreateImportListContext creates an import list in Radarr.
    81  func (r *Radarr) CreateImportListContext(ctx context.Context, list *ImportListInput) (*ImportListOutput, error) {
    82  	list.ID = 0
    83  
    84  	var body bytes.Buffer
    85  	if err := json.NewEncoder(&body).Encode(list); err != nil {
    86  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpImportList, err)
    87  	}
    88  
    89  	var output ImportListOutput
    90  
    91  	req := starr.Request{URI: bpImportList, Body: &body}
    92  	if err := r.PostInto(ctx, req, &output); err != nil {
    93  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    94  	}
    95  
    96  	return &output, nil
    97  }
    98  
    99  // DeleteImportList removes an import list from Radarr.
   100  func (r *Radarr) DeleteImportList(ids []int64) error {
   101  	return r.DeleteImportListContext(context.Background(), ids)
   102  }
   103  
   104  // DeleteImportListContext removes an import list from Radarr.
   105  func (r *Radarr) DeleteImportListContext(ctx context.Context, ids []int64) error {
   106  	var errs string
   107  
   108  	for _, id := range ids {
   109  		req := starr.Request{URI: path.Join(bpImportList, fmt.Sprint(id))}
   110  		if err := r.DeleteAny(ctx, req); err != nil {
   111  			errs += fmt.Errorf("api.Delete(%s): %w", &req, err).Error() + " "
   112  		}
   113  	}
   114  
   115  	if errs != "" {
   116  		return fmt.Errorf("%w: %s", starr.ErrRequestError, errs)
   117  	}
   118  
   119  	return nil
   120  }
   121  
   122  // TestImportList tests an import list.
   123  func (r *Radarr) TestImportList(list *ImportListInput) error {
   124  	return r.TestImportListContextt(context.Background(), list)
   125  }
   126  
   127  // TestImportListContextt tests an import list.
   128  func (r *Radarr) TestImportListContextt(ctx context.Context, list *ImportListInput) error {
   129  	var output interface{} // any ok
   130  
   131  	var body bytes.Buffer
   132  	if err := json.NewEncoder(&body).Encode(list); err != nil {
   133  		return fmt.Errorf("json.Marshal(%s): %w", bpImportList, err)
   134  	}
   135  
   136  	req := starr.Request{URI: path.Join(bpImportList, "test"), Body: &body}
   137  	if err := r.PostInto(ctx, req, &output); err != nil {
   138  		return fmt.Errorf("api.Post(%s): %w", &req, err)
   139  	}
   140  
   141  	return nil
   142  }
   143  
   144  // UpdateImportList updates an existing import list and returns the response.
   145  func (r *Radarr) UpdateImportList(list *ImportListInput, force bool) (*ImportListOutput, error) {
   146  	return r.UpdateImportListContext(context.Background(), list, force)
   147  }
   148  
   149  // UpdateImportListContext updates an existing import list and returns the response.
   150  func (r *Radarr) UpdateImportListContext(
   151  	ctx context.Context,
   152  	importList *ImportListInput,
   153  	force bool,
   154  ) (*ImportListOutput, error) {
   155  	var body bytes.Buffer
   156  	if err := json.NewEncoder(&body).Encode(importList); err != nil {
   157  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpImportList, err)
   158  	}
   159  
   160  	var output ImportListOutput
   161  
   162  	req := starr.Request{
   163  		URI:   path.Join(bpImportList, fmt.Sprint(importList.ID)),
   164  		Body:  &body,
   165  		Query: url.Values{"forceSave": []string{fmt.Sprint(force)}},
   166  	}
   167  	if err := r.PutInto(ctx, req, &output); err != nil {
   168  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   169  	}
   170  
   171  	return &output, nil
   172  }