2024-05-03 16:43:42 +01:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
2024-02-23 00:26:49 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-01-30 14:02:52 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2024-02-23 00:26:49 +00:00
|
|
|
package forgejo
|
2020-01-30 14:02:52 +00:00
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2024-02-23 00:26:49 +00:00
|
|
|
// ListOptions options for using Forgejo's API pagination
|
2020-01-30 14:02:52 +00:00
|
|
|
type ListOptions struct {
|
2022-04-26 20:07:37 +01:00
|
|
|
// Setting Page to -1 disables pagination on endpoints that support it.
|
|
|
|
// Page numbering starts at 1.
|
|
|
|
Page int
|
|
|
|
// The default value depends on the server config DEFAULT_PAGING_NUM
|
|
|
|
// The highest valid value depends on the server config MAX_RESPONSE_ITEMS
|
2020-01-30 14:02:52 +00:00
|
|
|
PageSize int
|
|
|
|
}
|
2020-02-05 07:59:55 +00:00
|
|
|
|
|
|
|
func (o ListOptions) getURLQuery() url.Values {
|
|
|
|
query := make(url.Values)
|
|
|
|
query.Add("page", fmt.Sprintf("%d", o.Page))
|
|
|
|
query.Add("limit", fmt.Sprintf("%d", o.PageSize))
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:07:37 +01:00
|
|
|
// setDefaults applies default pagination options.
|
|
|
|
// If .Page is set to -1, it will disable pagination.
|
|
|
|
// WARNING: This function is not idempotent, make sure to never call this method twice!
|
2021-03-29 21:57:07 +01:00
|
|
|
func (o *ListOptions) setDefaults() {
|
2021-03-30 21:28:44 +01:00
|
|
|
if o.Page < 0 {
|
|
|
|
o.Page, o.PageSize = 0, 0
|
|
|
|
return
|
|
|
|
} else if o.Page == 0 {
|
2020-02-05 07:59:55 +00:00
|
|
|
o.Page = 1
|
|
|
|
}
|
|
|
|
}
|