golift.io/starr@v1.0.0/readarr/downloadclient.go (about)

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