From bf8b6422989c8511c0cc018310e4c74db08783eb Mon Sep 17 00:00:00 2001 From: Kristof Daja Date: Sun, 24 May 2020 01:37:11 +0200 Subject: [PATCH] MilestoneTotalInfo added for MilestoneService.List --- milestones.go | 9 ++++++--- milestones.models.go | 24 +++++++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/milestones.go b/milestones.go index 18b3bcf..3f15972 100644 --- a/milestones.go +++ b/milestones.go @@ -17,7 +17,7 @@ type MilestoneService struct { } // List => https://taigaio.github.io/taiga-doc/dist/api.html#Milestones-list -func (s *MilestoneService) List(queryParams *MilestonesQueryParams) ([]Milestone, error) { +func (s *MilestoneService) List(queryParams *MilestonesQueryParams) ([]Milestone, *MilestoneTotalInfo, error) { // prepare url & parameters url := s.client.MakeURL(s.Endpoint) if queryParams != nil { @@ -30,9 +30,12 @@ func (s *MilestoneService) List(queryParams *MilestonesQueryParams) ([]Milestone var Milestones []Milestone httpResponse, err := s.client.Request.Get(url, &Milestones) if err != nil { - return nil, err + return nil, nil, err } - return Milestones, nil + mti := &MilestoneTotalInfo{} + mti.LoadFromHeaders(httpResponse) + + return Milestones, mti, nil } // Create => https://taigaio.github.io/taiga-doc/dist/api.html#milestones-create diff --git a/milestones.models.go b/milestones.models.go index bdb7dc3..aa3bff4 100644 --- a/milestones.models.go +++ b/milestones.models.go @@ -1,6 +1,10 @@ package taigo -import "time" +import ( + "net/http" + "strconv" + "time" +) // TODO /* @@ -38,3 +42,21 @@ type MilestonesQueryParams struct { Project int `url:"project,omitempty"` Closed bool `url:"closed,omitempty"` } + +// MilestoneTotalInfo holds the two extra headers returned by Taiga when filtering for milestones +// +// Taiga-Info-Total-Opened-Milestones: the number of opened milestones for this project +// Taiga-Info-Total-Closed-Milestones: the number of closed milestones for this project +// +// https://taigaio.github.io/taiga-doc/dist/api.html#milestones-list +type MilestoneTotalInfo struct { + TaigaInfoTotalOpenedMilestones int // Taiga-Info-Total-Opened-Milestones + TaigaInfoTotalClosedMilestones int // Taiga-Info-Total-Closed-Milestones +} + +// LoadFromHeaders accepts an *http.Response struct and reads the relevant +// pagination headers returned by Taiga +func (m *MilestoneTotalInfo) LoadFromHeaders(response *http.Response) { + m.TaigaInfoTotalOpenedMilestones, _ = strconv.Atoi(response.Header.Get("Taiga-Info-Total-Opened-Milestones")) + m.TaigaInfoTotalClosedMilestones, _ = strconv.Atoi(response.Header.Get("Taiga-Info-Total-Closed-Milestones")) +}