Skip to content

Commit

Permalink
Response context extended
Browse files Browse the repository at this point in the history
*http.Response is now internally returned to originating function call
  • Loading branch information
theriverman committed May 23, 2020
1 parent e076147 commit 10c52d8
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 100 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ To enable pagination (for all future requests) set `Client.DisablePagination` to
client.DisablePagination(false)
```

If you enable pagination, you can collect pagination details by accessing the returned `*http.Response`, <br>
and extracting the relevant headers into a taigo.Pagination struct the following way:
```go
// Collect returned Pagination headers
p := Pagination{}
p.LoadFromHeaders(client, response)
```

**Note:** See `type Pagination struct` in [TAIGO/common.models.go](common.models.go) for more details.

For details on Taiga's pagination, see [Taiga REST API / Pagination](https://taigaio.github.io/taiga-doc/dist/api.html#_pagination).

# Usage
Expand Down
4 changes: 2 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *AuthService) PublicRegistry(credentials *Credentials) (*UserAuthenticat

credentials.Type = "public"
credentials.AcceptedTerms = true // Hardcoded for simplicity; otherwise this func would be useless
err := s.client.Request.Post(url, &credentials, &u)
_, err := s.client.Request.Post(url, &credentials, &u)
if err != nil {
return nil, err
}
Expand All @@ -34,7 +34,7 @@ func (s *AuthService) login(credentials *Credentials) (*UserAuthenticationDetail
url := s.client.MakeURL(s.Endpoint)
u := UserAuthenticationDetail{}

err := s.client.Request.Post(url, &credentials, &u)
_, err := s.client.Request.Post(url, &credentials, &u)
if err != nil {
log.Println("Failed to authenticate to Taiga.")
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions common.functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func listAttachmentsForEndpoint(c *Client, queryParams *attachmentsQueryParams)
paramValues, _ := query.Values(queryParams)
url := c.MakeURL(queryParams.endpointURI, "attachments?", paramValues.Encode())
var attachments []Attachment
err := c.Request.Get(url, &attachments)
_, err := c.Request.Get(url, &attachments)
if err != nil {
return nil, err
}
Expand All @@ -29,7 +29,7 @@ func listAttachmentsForEndpoint(c *Client, queryParams *attachmentsQueryParams)
func getAttachmentForEndpoint(c *Client, attachmentID int, endpointURI string) (*Attachment, error) {
url := c.MakeURL(endpointURI, fmt.Sprintf("attachments/%d", attachmentID))
var a Attachment
err := c.Request.Get(url, &a)
_, err := c.Request.Get(url, &a)
if err != nil {
return nil, err
}
Expand Down
20 changes: 6 additions & 14 deletions common.models.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,14 @@ type Pagination struct {
// LoadFromHeaders accepts an *http.Response struct and reads the relevant
// pagination headers returned by Taiga
func (p *Pagination) LoadFromHeaders(c *Client, response *http.Response) {
if c.paginationDisabled {
p.Paginated = false
return
}

headers := response.Header

// Check if response is paginated
paginated := headers.Get("X-Paginated")
paginated := response.Header.Get("X-Paginated") // Check if response is paginated
if paginated == "true" {
p.Paginated = true
p.PaginatedBy, _ = strconv.Atoi(headers.Get("X-Paginated-By"))
p.PaginationCount, _ = strconv.Atoi(headers.Get("X-Paginated-Count"))
p.PaginationCurrent, _ = strconv.Atoi(headers.Get("X-Paginated-Current"))
p.PaginationNext, _ = url.Parse(headers.Get("X-Pagination-Next"))
p.PaginationPrev, _ = url.Parse(headers.Get("X-Pagination-Prev"))
p.PaginatedBy, _ = strconv.Atoi(response.Header.Get("X-Paginated-By"))
p.PaginationCount, _ = strconv.Atoi(response.Header.Get("X-Paginated-Count"))
p.PaginationCurrent, _ = strconv.Atoi(response.Header.Get("X-Paginated-Current"))
p.PaginationNext, _ = url.Parse(response.Header.Get("X-Pagination-Next"))
p.PaginationPrev, _ = url.Parse(response.Header.Get("X-Pagination-Prev"))
} else {
p.Paginated = false
}
Expand Down
17 changes: 9 additions & 8 deletions epics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package taigo
import (
"errors"
"fmt"
"net/http"

"github.com/google/go-querystring/query"
)
Expand All @@ -27,7 +28,7 @@ func (s *EpicService) List(queryParams *EpicsQueryParams) ([]Epic, error) {
url = url + s.client.GetDefaultProjectAsQueryParam()
}
var epics EpicDetailLIST
err := s.client.Request.Get(url, &epics)
_, err := s.client.Request.Get(url, &epics)
if err != nil {
return nil, err
}
Expand All @@ -48,7 +49,7 @@ func (s *EpicService) Create(epic *Epic) (*Epic, error) {
return nil, errors.New("A mandatory field(Project, Subject) is missing. See API documentataion")
}

err := s.client.Request.Post(url, &epic, &e)
_, err := s.client.Request.Post(url, &epic, &e)
if err != nil {
return nil, err
}
Expand All @@ -62,7 +63,7 @@ func (s *EpicService) Create(epic *Epic) (*Epic, error) {
func (s *EpicService) Get(epicID int) (*Epic, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/%d", s.Endpoint, epicID))
var e EpicDetailGET
err := s.client.Request.Get(url, &e)
_, err := s.client.Request.Get(url, &e)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -94,7 +95,7 @@ func (s *EpicService) GetByRef(epicRef int, project *Project) (*Epic, error) {
return nil, errors.New("No ID or Ref defined in passed project struct")
}

err := s.client.Request.Get(url, &e)
_, err := s.client.Request.Get(url, &e)
if err != nil {
return nil, err
}
Expand All @@ -117,15 +118,15 @@ func (s *EpicService) Edit(epic *Epic) (*Epic, error) {
return nil, err
}
epic.Version = remoteEpic.Version
err = s.client.Request.Patch(url, &epic, &e)
_, err = s.client.Request.Patch(url, &epic, &e)
if err != nil {
return nil, err
}
return e.AsEpic()
}

// Delete => https://taigaio.github.io/taiga-doc/dist/api.html#epics-delete
func (s *EpicService) Delete(epicID int) error {
func (s *EpicService) Delete(epicID int) (*http.Response, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/%d", s.Endpoint, epicID))
return s.client.Request.Delete(url)
}
Expand All @@ -148,7 +149,7 @@ It seems to be pointless to implement this operation here. A for loop around `Cr
func (s *EpicService) ListRelatedUserStories(epicID int) ([]EpicRelatedUserStoryDetail, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/%d/related_userstories", s.Endpoint, epicID))
var e []EpicRelatedUserStoryDetail
err := s.client.Request.Get(url, &e)
_, err := s.client.Request.Get(url, &e)
if err != nil {
return nil, err
}
Expand All @@ -162,7 +163,7 @@ func (s *EpicService) ListRelatedUserStories(epicID int) ([]EpicRelatedUserStory
func (s *EpicService) CreateRelatedUserStory(EpicID int, UserStoryID int) (*EpicRelatedUserStoryDetail, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/%d/related_userstories", s.Endpoint, EpicID))
e := EpicRelatedUserStoryDetail{EpicID: EpicID, UserStoryID: UserStoryID}
err := s.client.Request.Post(url, &e, &e)
_, err := s.client.Request.Post(url, &e, &e)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *IssueService) List(queryParams *IssueQueryParams) ([]Issue, error) {
}
// execute requests
var issues IssueDetailLIST
err := s.client.Request.Get(url, &issues)
_, err := s.client.Request.Get(url, &issues)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions milestones.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package taigo
import (
"errors"
"fmt"
"net/http"

"github.com/google/go-querystring/query"
)
Expand All @@ -27,7 +28,7 @@ func (s *MilestoneService) List(queryParams *MilestonesQueryParams) ([]Milestone
}
// execute requests
var Milestones []Milestone
err := s.client.Request.Get(url, &Milestones)
httpResponse, err := s.client.Request.Get(url, &Milestones)
if err != nil {
return nil, err
}
Expand All @@ -47,7 +48,7 @@ func (s *MilestoneService) Create(milestone *Milestone) (*Milestone, error) {
isEmpty(milestone.EstimatedFinish) {
return nil, errors.New("A mandatory field is missing. See API documentataion")
}
err := s.client.Request.Post(url, &milestone, &respMilestone)
_, err := s.client.Request.Post(url, &milestone, &respMilestone)
if err != nil {
return nil, err
}
Expand All @@ -58,7 +59,7 @@ func (s *MilestoneService) Create(milestone *Milestone) (*Milestone, error) {
func (s *MilestoneService) Get(milestoneID int) (*Milestone, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/%d", s.Endpoint, milestoneID))
var m Milestone
err := s.client.Request.Get(url, &m)
_, err := s.client.Request.Get(url, &m)
if err != nil {
return nil, err
}
Expand All @@ -73,15 +74,15 @@ func (s *MilestoneService) Edit(milestone *Milestone) (*Milestone, error) {
if milestone.ID == 0 {
return nil, errors.New("Passed Milestone does not have an ID yet. Does it exist?")
}
err := s.client.Request.Patch(url, &milestone, &m)
_, err := s.client.Request.Patch(url, &milestone, &m)
if err != nil {
return nil, err
}
return &m, nil
}

// Delete => https://taigaio.github.io/taiga-doc/dist/api.html#milestones-delete
func (s *MilestoneService) Delete(milestoneID int) error {
func (s *MilestoneService) Delete(milestoneID int) (*http.Response, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/%d", s.Endpoint, milestoneID))
return s.client.Request.Delete(url)
}
Expand Down
13 changes: 7 additions & 6 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package taigo
import (
"errors"
"fmt"
"net/http"

"github.com/google/go-querystring/query"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ func (s *ProjectService) List(queryParameters *ProjectsQueryParameters) (*Projec
}
var projects ProjectsList

err := s.client.Request.Get(url, &projects)
_, err := s.client.Request.Get(url, &projects)
if err != nil {
return nil, err
}
Expand All @@ -63,7 +64,7 @@ func (s *ProjectService) Create(project *Project) (*Project, error) {
if isEmpty(project.Name) || isEmpty(project.Description) {
return nil, errors.New("A mandatory field is missing. See API documentataion")
}
err := s.client.Request.Post(url, &project, &p)
_, err := s.client.Request.Post(url, &project, &p)
if err != nil {
return nil, err
}
Expand All @@ -75,7 +76,7 @@ func (s *ProjectService) Get(projectID int) (*Project, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/%d", s.Endpoint, projectID))
var p ProjectDetail

err := s.client.Request.Get(url, &p)
_, err := s.client.Request.Get(url, &p)
if err != nil {
return nil, err
}
Expand All @@ -87,7 +88,7 @@ func (s *ProjectService) GetBySlug(slug string) (*Project, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/by_slug?slug=%s", s.Endpoint, slug))
var p ProjectDetail

err := s.client.Request.Get(url, &p)
_, err := s.client.Request.Get(url, &p)
if err != nil {
return nil, err
}
Expand All @@ -104,15 +105,15 @@ func (s *ProjectService) Edit(project *Project) (*Project, error) {
return nil, errors.New("Passed Project does not have an ID yet. Does it exist?")
}

err := s.client.Request.Patch(url, &project, &p)
_, err := s.client.Request.Patch(url, &project, &p)
if err != nil {
return nil, err
}
return p.AsProject()
}

// Delete => https://taigaio.github.io/taiga-doc/dist/api.html#projects-delete
func (s *ProjectService) Delete(projectID int) error {
func (s *ProjectService) Delete(projectID int) (*http.Response, error) {
url := s.client.MakeURL(fmt.Sprintf("%s/%d", s.Endpoint, projectID))
return s.client.Request.Delete(url)
}
Loading

0 comments on commit 10c52d8

Please sign in to comment.