golift.io/starr@v1.0.0/lidarr/remotepathmapping.go (about)

     1  package lidarr
     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 remote path mapping calls.
    14  const bpRemotePathMapping = APIver + "/remotePathMapping"
    15  
    16  // GetRemotePathMappings returns all configured remote path mappings.
    17  func (l *Lidarr) GetRemotePathMappings() ([]*starr.RemotePathMapping, error) {
    18  	return l.GetRemotePathMappingsContext(context.Background())
    19  }
    20  
    21  // GetRemotePathMappingsContext returns all configured remote path mappings.
    22  func (l *Lidarr) GetRemotePathMappingsContext(ctx context.Context) ([]*starr.RemotePathMapping, error) {
    23  	var output []*starr.RemotePathMapping
    24  
    25  	req := starr.Request{URI: bpRemotePathMapping}
    26  	if err := l.GetInto(ctx, req, &output); err != nil {
    27  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    28  	}
    29  
    30  	return output, nil
    31  }
    32  
    33  // GetRemotePathMapping returns a single remote path mapping.
    34  func (l *Lidarr) GetRemotePathMapping(mappingID int64) (*starr.RemotePathMapping, error) {
    35  	return l.GetRemotePathMappingContext(context.Background(), mappingID)
    36  }
    37  
    38  // GetRemotePathMappingContext returns a single remote path mapping.
    39  func (l *Lidarr) GetRemotePathMappingContext(ctx context.Context, mappingID int64) (*starr.RemotePathMapping, error) {
    40  	var output starr.RemotePathMapping
    41  
    42  	req := starr.Request{URI: path.Join(bpRemotePathMapping, fmt.Sprint(mappingID))}
    43  	if err := l.GetInto(ctx, req, &output); err != nil {
    44  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    45  	}
    46  
    47  	return &output, nil
    48  }
    49  
    50  // AddRemotePathMapping creates a remote path mapping.
    51  func (l *Lidarr) AddRemotePathMapping(mapping *starr.RemotePathMapping) (*starr.RemotePathMapping, error) {
    52  	return l.AddRemotePathMappingContext(context.Background(), mapping)
    53  }
    54  
    55  // AddRemotePathMappingContext creates a remote path mapping.
    56  func (l *Lidarr) AddRemotePathMappingContext(ctx context.Context,
    57  	mapping *starr.RemotePathMapping,
    58  ) (*starr.RemotePathMapping, error) {
    59  	var output starr.RemotePathMapping
    60  
    61  	var body bytes.Buffer
    62  	if err := json.NewEncoder(&body).Encode(mapping); err != nil {
    63  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpRemotePathMapping, err)
    64  	}
    65  
    66  	req := starr.Request{URI: bpRemotePathMapping, Body: &body}
    67  	if err := l.PostInto(ctx, req, &output); err != nil {
    68  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    69  	}
    70  
    71  	return &output, nil
    72  }
    73  
    74  // UpdateRemotePathMapping updates the remote path mapping.
    75  func (l *Lidarr) UpdateRemotePathMapping(mapping *starr.RemotePathMapping) (*starr.RemotePathMapping, error) {
    76  	return l.UpdateRemotePathMappingContext(context.Background(), mapping)
    77  }
    78  
    79  // UpdateRemotePathMappingContext updates the remote path mapping.
    80  func (l *Lidarr) UpdateRemotePathMappingContext(ctx context.Context,
    81  	mapping *starr.RemotePathMapping,
    82  ) (*starr.RemotePathMapping, error) {
    83  	var output starr.RemotePathMapping
    84  
    85  	var body bytes.Buffer
    86  	if err := json.NewEncoder(&body).Encode(mapping); err != nil {
    87  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpRemotePathMapping, err)
    88  	}
    89  
    90  	req := starr.Request{URI: path.Join(bpRemotePathMapping, fmt.Sprint(mapping.ID)), Body: &body}
    91  	if err := l.PutInto(ctx, req, &output); err != nil {
    92  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
    93  	}
    94  
    95  	return &output, nil
    96  }
    97  
    98  // DeleteRemotePathMapping removes a single remote path mapping.
    99  func (l *Lidarr) DeleteRemotePathMapping(mappingID int64) error {
   100  	return l.DeleteRemotePathMappingContext(context.Background(), mappingID)
   101  }
   102  
   103  // DeleteRemotePathMappingContext removes a single remote path mapping.
   104  func (l *Lidarr) DeleteRemotePathMappingContext(ctx context.Context, mappingID int64) error {
   105  	req := starr.Request{URI: path.Join(bpRemotePathMapping, fmt.Sprint(mappingID))}
   106  	if err := l.DeleteAny(ctx, req); err != nil {
   107  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   108  	}
   109  
   110  	return nil
   111  }