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

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