golift.io/starr@v1.0.0/sonarr/languageprofile.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 Language Profile calls.
    14  const bpLanguageProfile = APIver + "/languageProfile"
    15  
    16  // LanguageProfile is the /api/v3/languageprofile endpoint.
    17  type LanguageProfile struct {
    18  	UpgradeAllowed bool         `json:"upgradeAllowed"`
    19  	ID             int64        `json:"id,omitempty"`
    20  	Name           string       `json:"name"`
    21  	Cutoff         *starr.Value `json:"cutoff"`
    22  	Languages      []Language   `json:"languages"`
    23  }
    24  
    25  // Language is part of LanguageProfile.
    26  type Language struct {
    27  	Allowed  bool         `json:"allowed"`
    28  	Language *starr.Value `json:"language"`
    29  }
    30  
    31  // GetLanguageProfiles returns all configured language profiles.
    32  func (s *Sonarr) GetLanguageProfiles() ([]*LanguageProfile, error) {
    33  	return s.GetLanguageProfilesContext(context.Background())
    34  }
    35  
    36  // GetLanguageProfilesContext returns all configured language profiles.
    37  func (s *Sonarr) GetLanguageProfilesContext(ctx context.Context) ([]*LanguageProfile, error) {
    38  	var output []*LanguageProfile
    39  
    40  	req := starr.Request{URI: bpLanguageProfile}
    41  	if err := s.GetInto(ctx, req, &output); err != nil {
    42  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    43  	}
    44  
    45  	return output, nil
    46  }
    47  
    48  // GetLanguageProfile returns a single language profile.
    49  func (s *Sonarr) GetLanguageProfile(profileID int64) (*LanguageProfile, error) {
    50  	return s.GetLanguageProfileContext(context.Background(), profileID)
    51  }
    52  
    53  // GetLanguageProfileContext returns a single language profile.
    54  func (s *Sonarr) GetLanguageProfileContext(ctx context.Context, profileID int64) (*LanguageProfile, error) {
    55  	var output LanguageProfile
    56  
    57  	req := starr.Request{URI: path.Join(bpLanguageProfile, fmt.Sprint(profileID))}
    58  	if err := s.GetInto(ctx, req, &output); err != nil {
    59  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    60  	}
    61  
    62  	return &output, nil
    63  }
    64  
    65  // AddLanguageProfile creates a language profile.
    66  func (s *Sonarr) AddLanguageProfile(profile *LanguageProfile) (*LanguageProfile, error) {
    67  	return s.AddLanguageProfileContext(context.Background(), profile)
    68  }
    69  
    70  // AddLanguageProfileContext creates a language profile.
    71  func (s *Sonarr) AddLanguageProfileContext(ctx context.Context, profile *LanguageProfile) (*LanguageProfile, error) {
    72  	var output LanguageProfile
    73  
    74  	var body bytes.Buffer
    75  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    76  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpLanguageProfile, err)
    77  	}
    78  
    79  	req := starr.Request{URI: bpLanguageProfile, Body: &body}
    80  	if err := s.PostInto(ctx, req, &output); err != nil {
    81  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    82  	}
    83  
    84  	return &output, nil
    85  }
    86  
    87  // UpdateLanguageProfile updates the language profile.
    88  func (s *Sonarr) UpdateLanguageProfile(profile *LanguageProfile) (*LanguageProfile, error) {
    89  	return s.UpdateLanguageProfileContext(context.Background(), profile)
    90  }
    91  
    92  // UpdateLanguageProfileContext updates the language profile.
    93  func (s *Sonarr) UpdateLanguageProfileContext(ctx context.Context, profile *LanguageProfile) (*LanguageProfile, error) {
    94  	var output LanguageProfile
    95  
    96  	var body bytes.Buffer
    97  	if err := json.NewEncoder(&body).Encode(profile); err != nil {
    98  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpLanguageProfile, err)
    99  	}
   100  
   101  	req := starr.Request{URI: path.Join(bpLanguageProfile, fmt.Sprint(profile.ID)), Body: &body}
   102  	if err := s.PutInto(ctx, req, &output); err != nil {
   103  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   104  	}
   105  
   106  	return &output, nil
   107  }
   108  
   109  // DeleteLanguageProfile removes a single language profile.
   110  func (s *Sonarr) DeleteLanguageProfile(profileID int64) error {
   111  	return s.DeleteLanguageProfileContext(context.Background(), profileID)
   112  }
   113  
   114  // DeleteLanguageProfileContext removes a single language profile.
   115  func (s *Sonarr) DeleteLanguageProfileContext(ctx context.Context, profileID int64) error {
   116  	req := starr.Request{URI: path.Join(bpLanguageProfile, fmt.Sprint(profileID))}
   117  	if err := s.DeleteAny(ctx, req); err != nil {
   118  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   119  	}
   120  
   121  	return nil
   122  }