-
Notifications
You must be signed in to change notification settings - Fork 11
/
requests.go
187 lines (158 loc) · 5.34 KB
/
requests.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// SPDX-FileCopyrightText: 2024 PK Lab AG <contact@pklab.io>
// SPDX-License-Identifier: MIT
package handler
import (
"strings"
pkgTypes "github.com/celenium-io/celestia-indexer/pkg/types"
"github.com/dipdup-net/indexer-sdk/pkg/storage"
"github.com/labstack/echo/v4"
)
const (
asc = "asc"
desc = "desc"
)
type limitOffsetPagination struct {
Limit int `json:"limit" param:"limit" query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `json:"offset" param:"offset" query:"offset" validate:"omitempty,min=0"`
Sort string `json:"sort" param:"sort" query:"sort" validate:"omitempty,oneof=asc desc"`
}
func (p *limitOffsetPagination) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = asc
}
}
func pgSort(sort string) storage.SortOrder {
switch sort {
case asc:
return storage.SortOrderAsc
case desc:
return storage.SortOrderDesc
default:
return storage.SortOrderAsc
}
}
type txListRequest struct {
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
Height uint64 `query:"height" validate:"omitempty,min=1"`
Status StringArray `query:"status" validate:"omitempty,dive,status"`
MsgType StringArray `query:"msg_type" validate:"omitempty,dive,msg_type"`
ExcludedMsgType StringArray `query:"excluded_msg_type" validate:"omitempty,dive,msg_type"`
Messages bool `query:"messages" validate:"omitempty"`
From int64 `example:"1692892095" query:"from" swaggertype:"integer" validate:"omitempty,min=1"`
To int64 `example:"1692892095" query:"to" swaggertype:"integer" validate:"omitempty,min=1"`
}
func (p *txListRequest) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = asc
}
}
type StringArray []string
func (s *StringArray) UnmarshalParam(param string) error {
*s = StringArray(strings.Split(param, ","))
return nil
}
type StatusArray StringArray
type MsgTypeArray StringArray
func bindAndValidate[T any](c echo.Context) (*T, error) {
req := new(T)
if err := c.Bind(req); err != nil {
return req, err
}
if err := c.Validate(req); err != nil {
return req, err
}
return req, nil
}
type addressTxRequest struct {
Hash string `param:"hash" validate:"required,address"`
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
Height uint64 `query:"height" validate:"omitempty,min=1"`
Status StringArray `query:"status" validate:"omitempty,dive,status"`
MsgType StringArray `query:"msg_type" validate:"omitempty,dive,msg_type"`
From int64 `example:"1692892095" query:"from" swaggertype:"integer" validate:"omitempty,min=1"`
To int64 `example:"1692892095" query:"to" swaggertype:"integer" validate:"omitempty,min=1"`
}
func (p *addressTxRequest) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = asc
}
}
type namespacesByHeightRequest struct {
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
Height pkgTypes.Level `param:"height" validate:"required,min=1"`
}
func (p *namespacesByHeightRequest) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
}
type listMessageByBlockRequest struct {
Height pkgTypes.Level `param:"height" validate:"required,min=1"`
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
MsgType StringArray `query:"msg_type" validate:"omitempty,dive,msg_type"`
ExcludedMsgType StringArray `query:"excluded_msg_type" validate:"omitempty,dive,msg_type"`
}
func (p *listMessageByBlockRequest) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
}
type namespaceList struct {
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=time pfb_count size"`
}
func (p *namespaceList) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = desc
}
}
type listForTx struct {
Hash string `param:"hash" validate:"required,hexadecimal,len=64"`
Limit uint64 `query:"limit" validate:"omitempty,min=1,max=100"`
Offset uint64 `query:"offset" validate:"omitempty,min=0"`
}
func (p *listForTx) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
}
type rollupList struct {
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
SortBy string `query:"sort_by" validate:"omitempty,oneof=time blobs_count size fee"`
}
func (p *rollupList) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = desc
}
if p.SortBy == "" {
p.SortBy = "size"
}
}
type getById struct {
Id uint64 `param:"id" validate:"required,min=1"`
}