golift.io/starr@v1.0.0/lidarr/tag.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  const bpTag = APIver + "/tag"
    14  
    15  // GetTags returns all configured tags.
    16  func (l *Lidarr) GetTags() ([]*starr.Tag, error) {
    17  	return l.GetTagsContext(context.Background())
    18  }
    19  
    20  // GetTagsContext returns all configured tags.
    21  func (l *Lidarr) GetTagsContext(ctx context.Context) ([]*starr.Tag, error) {
    22  	var output []*starr.Tag
    23  
    24  	req := starr.Request{URI: bpTag}
    25  	if err := l.GetInto(ctx, req, &output); err != nil {
    26  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    27  	}
    28  
    29  	return output, nil
    30  }
    31  
    32  // GetTag returns a single tag.
    33  func (l *Lidarr) GetTag(tagID int) (*starr.Tag, error) {
    34  	return l.GetTagContext(context.Background(), tagID)
    35  }
    36  
    37  // GetTagContext returns a single tag.
    38  func (l *Lidarr) GetTagContext(ctx context.Context, tagID int) (*starr.Tag, error) {
    39  	var output starr.Tag
    40  
    41  	req := starr.Request{URI: path.Join(bpTag, fmt.Sprint(tagID))}
    42  	if err := l.GetInto(ctx, req, &output); err != nil {
    43  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    44  	}
    45  
    46  	return &output, nil
    47  }
    48  
    49  // AddTag creates a tag.
    50  func (l *Lidarr) AddTag(tag *starr.Tag) (*starr.Tag, error) {
    51  	return l.AddTagContext(context.Background(), tag)
    52  }
    53  
    54  // AddTagContext creates a tag.
    55  func (l *Lidarr) AddTagContext(ctx context.Context, tag *starr.Tag) (*starr.Tag, error) {
    56  	var output starr.Tag
    57  
    58  	var body bytes.Buffer
    59  	if err := json.NewEncoder(&body).Encode(tag); err != nil {
    60  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpTag, err)
    61  	}
    62  
    63  	req := starr.Request{URI: bpTag, Body: &body}
    64  	if err := l.PostInto(ctx, req, &output); err != nil {
    65  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    66  	}
    67  
    68  	return &output, nil
    69  }
    70  
    71  // UpdateTag updates a tag.
    72  func (l *Lidarr) UpdateTag(tag *starr.Tag) (*starr.Tag, error) {
    73  	return l.UpdateTagContext(context.Background(), tag)
    74  }
    75  
    76  // UpdateTagContext updates a tag.
    77  func (l *Lidarr) UpdateTagContext(ctx context.Context, tag *starr.Tag) (*starr.Tag, error) {
    78  	var output starr.Tag
    79  
    80  	var body bytes.Buffer
    81  	if err := json.NewEncoder(&body).Encode(tag); err != nil {
    82  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpTag, err)
    83  	}
    84  
    85  	req := starr.Request{URI: path.Join(bpTag, fmt.Sprint(tag.ID)), Body: &body}
    86  	if err := l.PutInto(ctx, req, &output); err != nil {
    87  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
    88  	}
    89  
    90  	return &output, nil
    91  }
    92  
    93  // DeleteTag removes a single tag.
    94  func (l *Lidarr) DeleteTag(tagID int) error {
    95  	return l.DeleteTagContext(context.Background(), tagID)
    96  }
    97  
    98  // DeleteTagContext removes a single tag.
    99  func (l *Lidarr) DeleteTagContext(ctx context.Context, tagID int) error {
   100  	req := starr.Request{URI: path.Join(bpTag, fmt.Sprint(tagID))}
   101  	if err := l.DeleteAny(ctx, req); err != nil {
   102  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   103  	}
   104  
   105  	return nil
   106  }