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

     1  package lidarr
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/url"
     7  	"path"
     8  	"time"
     9  
    10  	"golift.io/starr"
    11  )
    12  
    13  // Define Base Path for Calendar queries.
    14  const bpCalendar = APIver + "/calendar"
    15  
    16  // Calendar defines the filters for fetching calendar items.
    17  type Calendar struct {
    18  	Start         time.Time
    19  	End           time.Time
    20  	Unmonitored   bool
    21  	IncludeArtist bool
    22  }
    23  
    24  // GetCalendar returns calendars based on filters.
    25  func (l *Lidarr) GetCalendar(filter Calendar) ([]*Album, error) {
    26  	return l.GetCalendarContext(context.Background(), filter)
    27  }
    28  
    29  // GetCalendarContext returns calendars based on filters.
    30  func (l *Lidarr) GetCalendarContext(ctx context.Context, filter Calendar) ([]*Album, error) {
    31  	var output []*Album
    32  
    33  	req := starr.Request{URI: bpCalendar, Query: make(url.Values)}
    34  	req.Query.Add("unmonitored", fmt.Sprint(filter.Unmonitored))
    35  	req.Query.Add("includeArtist", fmt.Sprint(filter.IncludeArtist))
    36  
    37  	if !filter.Start.IsZero() {
    38  		req.Query.Add("start", filter.Start.UTC().Format(starr.CalendarTimeFilterFormat))
    39  	}
    40  
    41  	if !filter.End.IsZero() {
    42  		req.Query.Add("end", filter.End.UTC().Format(starr.CalendarTimeFilterFormat))
    43  	}
    44  
    45  	if err := l.GetInto(ctx, req, &output); err != nil {
    46  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    47  	}
    48  
    49  	return output, nil
    50  }
    51  
    52  // GetCalendarID returns a single calendar by ID.
    53  func (l *Lidarr) GetCalendarID(calendarID int64) (*Album, error) {
    54  	return l.GetCalendarIDContext(context.Background(), calendarID)
    55  }
    56  
    57  // GetCalendarIDContext returns a single calendar by ID.
    58  func (l *Lidarr) GetCalendarIDContext(ctx context.Context, calendarID int64) (*Album, error) {
    59  	var output *Album
    60  
    61  	req := starr.Request{URI: path.Join(bpCalendar, fmt.Sprint(calendarID))}
    62  	if err := l.GetInto(ctx, req, &output); err != nil {
    63  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    64  	}
    65  
    66  	return output, nil
    67  }