-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
header.go
118 lines (107 loc) · 3.58 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package keysetpagination
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/ory/x/stringsx"
)
// Pagination Request Parameters
//
// The `Link` HTTP header contains multiple links (`first`, `next`) formatted as:
// `<https://{project-slug}.projects.oryapis.com/admin/sessions?page_size=250&page_token=>; rel="first"`
//
// For details on pagination please head over to the [pagination documentation](https://www.ory.sh/docs/ecosystem/api-design#pagination).
//
// swagger:model keysetPaginationRequestParameters
type RequestParameters struct {
// Items per Page
//
// This is the number of items per page to return.
// For details on pagination please head over to the [pagination documentation](https://www.ory.sh/docs/ecosystem/api-design#pagination).
//
// required: false
// in: query
// default: 250
// min: 1
// max: 1000
PageSize int `json:"page_size"`
// Next Page Token
//
// The next page token.
// For details on pagination please head over to the [pagination documentation](https://www.ory.sh/docs/ecosystem/api-design#pagination).
//
// required: false
// in: query
PageToken string `json:"page_token"`
}
// Pagination Response Header
//
// The `Link` HTTP header contains multiple links (`first`, `next`) formatted as:
// `<https://{project-slug}.projects.oryapis.com/admin/sessions?page_size=250&page_token=>; rel="first"`
//
// For details on pagination please head over to the [pagination documentation](https://www.ory.sh/docs/ecosystem/api-design#pagination).
//
// swagger:model keysetPaginationResponseHeaders
type ResponseHeaders struct {
// The Link HTTP Header
//
// The `Link` header contains a comma-delimited list of links to the following pages:
//
// - first: The first page of results.
// - next: The next page of results.
//
// Pages are omitted if they do not exist. For example, if there is no next page, the `next` link is omitted. Examples:
//
// </admin/sessions?page_size=250&page_token={last_item_uuid}; rel="first",/admin/sessions?page_size=250&page_token=>; rel="next"
//
Link string `json:"link"`
// The X-Total-Count HTTP Header
//
// The `X-Total-Count` header contains the total number of items in the collection.
TotalCount int `json:"x-total-count"`
}
func header(u *url.URL, rel, token string, size int) string {
q := u.Query()
q.Set("page_token", token)
q.Set("page_size", strconv.Itoa(size))
u.RawQuery = q.Encode()
return fmt.Sprintf("<%s>; rel=\"%s\"", u.String(), rel)
}
// Header adds the Link header for the page encoded by the paginator.
// It contains links to the first and next page, if one exists.
func Header(w http.ResponseWriter, u *url.URL, p *Paginator) {
size := p.Size()
link := []string{header(u, "first", p.defaultToken.Encode(), size)}
if !p.isLast {
link = append(link, header(u, "next", p.Token().Encode(), size))
}
w.Header().Set("Link", strings.Join(link, ","))
}
// Parse returns the pagination options from the URL query.
func Parse(q url.Values, p PageTokenConstructor) ([]Option, error) {
var opts []Option
if pt := stringsx.Coalesce(q["page_token"]...); pt != "" {
pageToken, err := url.QueryUnescape(pt)
if err != nil {
return nil, errors.WithStack(err)
}
parsed, err := p(pageToken)
if err != nil {
return nil, errors.WithStack(err)
}
opts = append(opts, WithToken(parsed))
}
if ps := stringsx.Coalesce(q["page_size"]...); ps != "" {
size, err := strconv.Atoi(ps)
if err != nil {
return nil, errors.WithStack(err)
}
opts = append(opts, WithSize(size))
}
return opts, nil
}