github.1485827954.workers.dev/newrelic/newrelic-client-go@v1.1.0/internal/http/paging.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/tomnomnom/linkheader"
     7  )
     8  
     9  // Pager represents a pagination implementation.
    10  type Pager interface {
    11  	Parse(res *http.Response) Paging
    12  }
    13  
    14  // Paging represents the pagination context returned from the Pager implementation.
    15  type Paging struct {
    16  	Next string
    17  }
    18  
    19  // LinkHeaderPager represents a pagination implementation that adheres to RFC 5988.
    20  type LinkHeaderPager struct{}
    21  
    22  // Parse is used to parse a pagination context from an HTTP response.
    23  func (l *LinkHeaderPager) Parse(resp *http.Response) Paging {
    24  	paging := Paging{}
    25  	header := resp.Header.Get("Link")
    26  	if header != "" {
    27  		links := linkheader.Parse(header)
    28  
    29  		for _, link := range links.FilterByRel("next") {
    30  			paging.Next = link.URL
    31  			break
    32  		}
    33  	}
    34  
    35  	return paging
    36  }