golift.io/starr@v1.0.0/sonarr/rootfolder.go (about)

     1  package sonarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"path"
     9  
    10  	"golift.io/starr"
    11  )
    12  
    13  // Define Base Path for MediaManagement calls.
    14  const bpRootFolder = APIver + "/rootFolder"
    15  
    16  // RootFolder is the /api/v3/rootfolder endpoint.
    17  type RootFolder struct {
    18  	Accessible      bool          `json:"accessible,omitempty"`
    19  	ID              int64         `json:"id,omitempty"`
    20  	FreeSpace       int64         `json:"freeSpace,omitempty"`
    21  	Path            string        `json:"path"`
    22  	UnmappedFolders []*starr.Path `json:"unmappedFolders,omitempty"`
    23  }
    24  
    25  // GetRootFolders returns all configured root folders.
    26  func (s *Sonarr) GetRootFolders() ([]*RootFolder, error) {
    27  	return s.GetRootFoldersContext(context.Background())
    28  }
    29  
    30  // GetRootFoldersContext returns all configured root folders.
    31  func (s *Sonarr) GetRootFoldersContext(ctx context.Context) ([]*RootFolder, error) {
    32  	var output []*RootFolder
    33  
    34  	req := starr.Request{URI: bpRootFolder}
    35  	if err := s.GetInto(ctx, req, &output); err != nil {
    36  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    37  	}
    38  
    39  	return output, nil
    40  }
    41  
    42  // GetRootFolder returns a single root folder.
    43  func (s *Sonarr) GetRootFolder(folderID int64) (*RootFolder, error) {
    44  	return s.GetRootFolderContext(context.Background(), folderID)
    45  }
    46  
    47  // GetRootFolderContext returns a single root folder.
    48  func (s *Sonarr) GetRootFolderContext(ctx context.Context, folderID int64) (*RootFolder, error) {
    49  	var output RootFolder
    50  
    51  	req := starr.Request{URI: path.Join(bpRootFolder, fmt.Sprint(folderID))}
    52  	if err := s.GetInto(ctx, req, &output); err != nil {
    53  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    54  	}
    55  
    56  	return &output, nil
    57  }
    58  
    59  // AddRootFolder creates a root folder.
    60  func (s *Sonarr) AddRootFolder(folder *RootFolder) (*RootFolder, error) {
    61  	return s.AddRootFolderContext(context.Background(), folder)
    62  }
    63  
    64  // AddRootFolderContext creates a root folder.
    65  func (s *Sonarr) AddRootFolderContext(ctx context.Context, folder *RootFolder) (*RootFolder, error) {
    66  	var output RootFolder
    67  
    68  	var body bytes.Buffer
    69  	if err := json.NewEncoder(&body).Encode(folder); err != nil {
    70  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpRootFolder, err)
    71  	}
    72  
    73  	req := starr.Request{URI: bpRootFolder, Body: &body}
    74  	if err := s.PostInto(ctx, req, &output); err != nil {
    75  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    76  	}
    77  
    78  	return &output, nil
    79  }
    80  
    81  // DeleteRootFolder removes a single root folder.
    82  func (s *Sonarr) DeleteRootFolder(folderID int64) error {
    83  	return s.DeleteRootFolderContext(context.Background(), folderID)
    84  }
    85  
    86  // DeleteRootFolderContext removes a single root folder.
    87  func (s *Sonarr) DeleteRootFolderContext(ctx context.Context, folderID int64) error {
    88  	req := starr.Request{URI: path.Join(bpRootFolder, fmt.Sprint(folderID))}
    89  	if err := s.DeleteAny(ctx, req); err != nil {
    90  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
    91  	}
    92  
    93  	return nil
    94  }