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

     1  package prowlarr
     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 notification calls.
    14  const bpNotification = APIver + "/notification"
    15  
    16  // NotificationInput is the input for a new or updated notification.
    17  type NotificationInput struct {
    18  	OnGrab                      bool                `json:"onGrab"`
    19  	OnHealthIssue               bool                `json:"onHealthIssue"`
    20  	OnHealthRestored            bool                `json:"onHealthRestored"`
    21  	OnApplicationUpdate         bool                `json:"onApplicationUpdate"`
    22  	SupportsOnGrab              bool                `json:"supportsOnGrab"`
    23  	IncludeManualGrabs          bool                `json:"includeManualGrabs"`
    24  	SupportsOnHealthIssue       bool                `json:"supportsOnHealthIssue"`
    25  	SupportsOnHealthRestored    bool                `json:"supportsOnHealthRestored"`
    26  	IncludeHealthWarnings       bool                `json:"includeHealthWarnings"`
    27  	SupportsOnApplicationUpdate bool                `json:"supportsOnApplicationUpdate"`
    28  	ID                          int64               `json:"id,omitempty"` // update only
    29  	Name                        string              `json:"name"`
    30  	ImplementationName          string              `json:"implementationName"`
    31  	Implementation              string              `json:"implementation"`
    32  	ConfigContract              string              `json:"configContract"`
    33  	InfoLink                    string              `json:"infoLink"`
    34  	Tags                        []int               `json:"tags"`
    35  	Fields                      []*starr.FieldInput `json:"fields"`
    36  }
    37  
    38  // NotificationOutput is the output from the notification methods.
    39  type NotificationOutput struct {
    40  	OnGrab                      bool                 `json:"onGrab"`
    41  	OnHealthIssue               bool                 `json:"onHealthIssue"`
    42  	OnHealthRestored            bool                 `json:"onHealthRestored"`
    43  	OnApplicationUpdate         bool                 `json:"onApplicationUpdate"`
    44  	SupportsOnGrab              bool                 `json:"supportsOnGrab"`
    45  	IncludeManualGrabs          bool                 `json:"includeManualGrabs"`
    46  	SupportsOnHealthIssue       bool                 `json:"supportsOnHealthIssue"`
    47  	SupportsOnHealthRestored    bool                 `json:"supportsOnHealthRestored"`
    48  	IncludeHealthWarnings       bool                 `json:"includeHealthWarnings"`
    49  	SupportsOnApplicationUpdate bool                 `json:"supportsOnApplicationUpdate"`
    50  	ID                          int64                `json:"id"`
    51  	Name                        string               `json:"name"`
    52  	ImplementationName          string               `json:"implementationName"`
    53  	Implementation              string               `json:"implementation"`
    54  	ConfigContract              string               `json:"configContract"`
    55  	InfoLink                    string               `json:"infoLink"`
    56  	Tags                        []int                `json:"tags"`
    57  	Fields                      []*starr.FieldOutput `json:"fields"`
    58  	Message                     struct {
    59  		Message string `json:"message"` // this is a weird place for a message
    60  		Type    string `json:"type"`
    61  	} `json:"message"`
    62  }
    63  
    64  // GetNotifications returns all configured notifications.
    65  func (p *Prowlarr) GetNotifications() ([]*NotificationOutput, error) {
    66  	return p.GetNotificationsContext(context.Background())
    67  }
    68  
    69  // GetNotificationsContext returns all configured notifications.
    70  func (p *Prowlarr) GetNotificationsContext(ctx context.Context) ([]*NotificationOutput, error) {
    71  	var output []*NotificationOutput
    72  
    73  	req := starr.Request{URI: bpNotification}
    74  	if err := p.GetInto(ctx, req, &output); err != nil {
    75  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    76  	}
    77  
    78  	return output, nil
    79  }
    80  
    81  // GetNotification returns a single notification.
    82  func (p *Prowlarr) GetNotification(notificationID int) (*NotificationOutput, error) {
    83  	return p.GetNotificationContext(context.Background(), notificationID)
    84  }
    85  
    86  // GetNotificationContext returns a single notification.
    87  func (p *Prowlarr) GetNotificationContext(ctx context.Context, notificationID int) (*NotificationOutput, error) {
    88  	var output NotificationOutput
    89  
    90  	req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(notificationID))}
    91  	if err := p.GetInto(ctx, req, &output); err != nil {
    92  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    93  	}
    94  
    95  	return &output, nil
    96  }
    97  
    98  // AddNotification creates a notification.
    99  func (p *Prowlarr) AddNotification(notification *NotificationInput) (*NotificationOutput, error) {
   100  	return p.AddNotificationContext(context.Background(), notification)
   101  }
   102  
   103  // AddNotificationContext creates a notification.
   104  func (p *Prowlarr) AddNotificationContext(ctx context.Context, client *NotificationInput) (*NotificationOutput, error) {
   105  	var output NotificationOutput
   106  
   107  	var body bytes.Buffer
   108  	if err := json.NewEncoder(&body).Encode(client); err != nil {
   109  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpNotification, err)
   110  	}
   111  
   112  	req := starr.Request{URI: bpNotification, Body: &body}
   113  	if err := p.PostInto(ctx, req, &output); err != nil {
   114  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
   115  	}
   116  
   117  	return &output, nil
   118  }
   119  
   120  // UpdateNotification updates the notification.
   121  func (p *Prowlarr) UpdateNotification(notification *NotificationInput) (*NotificationOutput, error) {
   122  	return p.UpdateNotificationContext(context.Background(), notification)
   123  }
   124  
   125  // UpdateNotificationContext updates the notification.
   126  func (p *Prowlarr) UpdateNotificationContext(ctx context.Context,
   127  	client *NotificationInput,
   128  ) (*NotificationOutput, error) {
   129  	var output NotificationOutput
   130  
   131  	var body bytes.Buffer
   132  	if err := json.NewEncoder(&body).Encode(client); err != nil {
   133  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpNotification, err)
   134  	}
   135  
   136  	req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(client.ID)), Body: &body}
   137  	if err := p.PutInto(ctx, req, &output); err != nil {
   138  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   139  	}
   140  
   141  	return &output, nil
   142  }
   143  
   144  // DeleteNotification removes a single notification.
   145  func (p *Prowlarr) DeleteNotification(notificationID int64) error {
   146  	return p.DeleteNotificationContext(context.Background(), notificationID)
   147  }
   148  
   149  func (p *Prowlarr) DeleteNotificationContext(ctx context.Context, notificationID int64) error {
   150  	req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(notificationID))}
   151  	if err := p.DeleteAny(ctx, req); err != nil {
   152  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   153  	}
   154  
   155  	return nil
   156  }