Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tanema/link
A library for parsing link pagination headers.
https://github.com/tanema/link
go golang link-header
Last synced: 7 days ago
JSON representation
A library for parsing link pagination headers.
- Host: GitHub
- URL: https://github.com/tanema/link
- Owner: tanema
- License: mit
- Created: 2023-01-20T19:47:52.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-20T20:01:37.000Z (almost 2 years ago)
- Last Synced: 2024-11-07T16:16:56.580Z (about 2 months ago)
- Topics: go, golang, link-header
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Link 🧝
[![Go Reference](https://pkg.go.dev/badge/github.com/tanema/link.svg)](https://pkg.go.dev/github.com/tanema/link)
Easily parse and create Link headers for pagination.
### Create Link Headers
Easily create link headers for responses to your paginated endpoints.```go
func handler(w http.ResponseWriter, req http.Request) {
// ...
linkHeader := link.NewHeader(map[string]*url.URL{
"first": firstURL,
"last": lastURL,
// if these are nil then they will not be included in the header
"next": nextURL,
"prev": prevURL,
})
w.WriteHeader("link", linkHeader.String())
// ...
}
```### Parse Link Headers for integrating with services.
Easily parse link headers from external services to paginate through results.```go
func loadUsers(path string) {
resp, err := http.DefaultClient.Get(path)
linkHeader, err := link.Parse(resp)
if next := linkHeader.Next(); next != nil {
loadUsers(next.URL.String())
}
}func main() {
loadUsers("http://service.com/users")
}
```