-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkpoints.go
156 lines (137 loc) · 3.92 KB
/
checkpoints.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2022 SundaeSwap Labs, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// Licensed under the MIT License;
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package kugo
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/SundaeSwap-finance/ogmigo/v6"
)
type checkpointsOptions struct {
singular bool
slot uint64
}
func (c checkpointsOptions) apply(url *url.URL) {
if c.slot != 0 {
url.Path += fmt.Sprintf("/%v", c.slot)
}
}
type CheckpointsFilter struct {
before func(*checkpointsOptions)
after func(points []Point) []Point
}
func (c *Client) Checkpoints(ctx context.Context, filters ...CheckpointsFilter) (points []Point, err error) {
start := time.Now()
defer func() {
errStr := ""
if err != nil {
errStr = err.Error()
}
c.options.logger.Info("Checkpoints() finished",
ogmigo.KV("duration", time.Since(start).Round(time.Millisecond).String()),
ogmigo.KV("err", errStr),
)
}()
endpoint, err := url.Parse(c.options.endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse endpoint ")
}
endpoint.Path = "/v1/checkpoints"
o := checkpointsOptions{}
for _, f := range filters {
if f.before != nil {
f.before(&o)
}
}
o.apply(endpoint)
req, err := http.NewRequest("GET", endpoint.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to build request: %w", err)
}
req.Close = true
req = req.WithContext(ctx)
client := &http.Client{
Timeout: c.options.timeout,
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to retrieve checkpoint by slot: %w", err)
}
if resp == nil {
return nil, fmt.Errorf("failed with a nil response")
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got unexpected response: %v: %v", resp.StatusCode, string(body))
}
if o.singular {
var point Point
if err := json.Unmarshal(body, &point); err != nil {
return nil, fmt.Errorf("error parsing response %v: %w", string(body), err)
}
points = []Point{point}
} else {
if err := json.Unmarshal(body, &points); err != nil {
return nil, fmt.Errorf("error parsing response %v: %w", string(body), err)
}
}
for _, f := range filters {
if f.after != nil {
points = f.after(points)
}
}
return points, nil
}
// Return a recent sampling of kupo checkpoints
// NOTE: equivalent to providing no filters, but useful for documenting your purpose
func Recent() CheckpointsFilter {
return CheckpointsFilter{
before: nil,
after: nil,
}
}
func Latest() CheckpointsFilter {
return CheckpointsFilter{
before: nil,
after: func(points []Point) []Point {
return []Point{points[0]}
},
}
}
func BySlot(slot uint64) CheckpointsFilter {
return CheckpointsFilter{
before: func(co *checkpointsOptions) {
co.singular = true
co.slot = slot
},
after: nil,
}
}