golift.io/starr@v1.0.0/lidarr/downloadclient.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  // Define Base Path for download client calls.
    15  const bpDownloadClient = APIver + "/downloadClient"
    16  
    17  // DownloadClientInput is the input for a new or updated download client.
    18  type DownloadClientInput struct {
    19  	Enable                   bool                `json:"enable"`
    20  	RemoveCompletedDownloads bool                `json:"removeCompletedDownloads"`
    21  	RemoveFailedDownloads    bool                `json:"removeFailedDownloads"`
    22  	Priority                 int                 `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  // DownloadClientOutput is the output from the download client methods.
    33  type DownloadClientOutput struct {
    34  	Enable                   bool                 `json:"enable"`
    35  	RemoveCompletedDownloads bool                 `json:"removeCompletedDownloads"`
    36  	RemoveFailedDownloads    bool                 `json:"removeFailedDownloads"`
    37  	Priority                 int                  `json:"priority"`
    38  	ID                       int64                `json:"id,omitempty"`
    39  	ConfigContract           string               `json:"configContract"`
    40  	Implementation           string               `json:"implementation"`
    41  	ImplementationName       string               `json:"implementationName"`
    42  	InfoLink                 string               `json:"infoLink"`
    43  	Name                     string               `json:"name"`
    44  	Protocol                 string               `json:"protocol"`
    45  	Tags                     []int                `json:"tags"`
    46  	Fields                   []*starr.FieldOutput `json:"fields"`
    47  }
    48  
    49  // GetDownloadClients returns all configured download clients.
    50  func (l *Lidarr) GetDownloadClients() ([]*DownloadClientOutput, error) {
    51  	return l.GetDownloadClientsContext(context.Background())
    52  }
    53  
    54  // GetDownloadClientsContext returns all configured download clients.
    55  func (l *Lidarr) GetDownloadClientsContext(ctx context.Context) ([]*DownloadClientOutput, error) {
    56  	var output []*DownloadClientOutput
    57  
    58  	req := starr.Request{URI: bpDownloadClient}
    59  	if err := l.GetInto(ctx, req, &output); err != nil {
    60  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    61  	}
    62  
    63  	return output, nil
    64  }
    65  
    66  // GetDownloadClient returns a single download client.
    67  func (l *Lidarr) GetDownloadClient(downloadclientID int64) (*DownloadClientOutput, error) {
    68  	return l.GetDownloadClientContext(context.Background(), downloadclientID)
    69  }
    70  
    71  // GetDownloadClientContext returns a single download client.
    72  func (l *Lidarr) GetDownloadClientContext(ctx context.Context, downloadclientID int64) (*DownloadClientOutput, error) {
    73  	var output DownloadClientOutput
    74  
    75  	req := starr.Request{URI: path.Join(bpDownloadClient, fmt.Sprint(downloadclientID))}
    76  	if err := l.GetInto(ctx, req, &output); err != nil {
    77  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    78  	}
    79  
    80  	return &output, nil
    81  }
    82  
    83  // AddDownloadClient creates a download client.
    84  func (l *Lidarr) AddDownloadClient(downloadclient *DownloadClientInput) (*DownloadClientOutput, error) {
    85  	return l.AddDownloadClientContext(context.Background(), downloadclient)
    86  }
    87  
    88  // AddDownloadClientContext creates a download client.
    89  func (l *Lidarr) AddDownloadClientContext(ctx context.Context,
    90  	client *DownloadClientInput,
    91  ) (*DownloadClientOutput, error) {
    92  	var output DownloadClientOutput
    93  
    94  	var body bytes.Buffer
    95  	if err := json.NewEncoder(&body).Encode(client); err != nil {
    96  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpDownloadClient, err)
    97  	}
    98  
    99  	req := starr.Request{URI: bpDownloadClient, Body: &body}
   100  	if err := l.PostInto(ctx, req, &output); err != nil {
   101  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
   102  	}
   103  
   104  	return &output, nil
   105  }
   106  
   107  // TestDownloadClient tests a download client.
   108  func (l *Lidarr) TestDownloadClient(client *DownloadClientInput) error {
   109  	return l.TestDownloadClientContext(context.Background(), client)
   110  }
   111  
   112  // TestDownloadClientContext tests a download client.
   113  func (l *Lidarr) TestDownloadClientContext(ctx context.Context, client *DownloadClientInput) error {
   114  	var output interface{}
   115  
   116  	var body bytes.Buffer
   117  	if err := json.NewEncoder(&body).Encode(client); err != nil {
   118  		return fmt.Errorf("json.Marshal(%s): %w", bpDownloadClient, err)
   119  	}
   120  
   121  	req := starr.Request{URI: path.Join(bpDownloadClient, "test"), Body: &body}
   122  	if err := l.PostInto(ctx, req, &output); err != nil {
   123  		return fmt.Errorf("api.Post(%s): %w", &req, err)
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  // UpdateDownloadClient updates the download client.
   130  func (l *Lidarr) UpdateDownloadClient(downloadclient *DownloadClientInput, force bool) (*DownloadClientOutput, error) {
   131  	return l.UpdateDownloadClientContext(context.Background(), downloadclient, force)
   132  }
   133  
   134  // UpdateDownloadClientContext updates the download client.
   135  func (l *Lidarr) UpdateDownloadClientContext(ctx context.Context,
   136  	client *DownloadClientInput,
   137  	force bool,
   138  ) (*DownloadClientOutput, error) {
   139  	var output DownloadClientOutput
   140  
   141  	var body bytes.Buffer
   142  	if err := json.NewEncoder(&body).Encode(client); err != nil {
   143  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpDownloadClient, err)
   144  	}
   145  
   146  	req := starr.Request{
   147  		URI:   path.Join(bpDownloadClient, fmt.Sprint(client.ID)),
   148  		Body:  &body,
   149  		Query: url.Values{"forceSave": []string{fmt.Sprint(force)}},
   150  	}
   151  	if err := l.PutInto(ctx, req, &output); err != nil {
   152  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   153  	}
   154  
   155  	return &output, nil
   156  }
   157  
   158  // DeleteDownloadClient removes a single download client.
   159  func (l *Lidarr) DeleteDownloadClient(downloadclientID int64) error {
   160  	return l.DeleteDownloadClientContext(context.Background(), downloadclientID)
   161  }
   162  
   163  // DeleteDownloadClientContext removes a single download client.
   164  func (l *Lidarr) DeleteDownloadClientContext(ctx context.Context, downloadclientID int64) error {
   165  	req := starr.Request{URI: path.Join(bpDownloadClient, fmt.Sprint(downloadclientID))}
   166  	if err := l.DeleteAny(ctx, req); err != nil {
   167  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   168  	}
   169  
   170  	return nil
   171  }