-
Notifications
You must be signed in to change notification settings - Fork 64
/
key.go
222 lines (179 loc) · 6.44 KB
/
key.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package key
import (
"fmt"
"strings"
"time"
internal_errors "github.com/bricks-cloud/bricksllm/internal/errors"
)
type Provider string
const (
OpenAiProvider Provider = "openai"
)
type UpdateKey struct {
Name string `json:"name"`
UpdatedAt int64 `json:"updatedAt"`
Tags []string `json:"tags"`
Revoked *bool `json:"revoked"`
RevokedReason string `json:"revokedReason"`
CostLimitInUsd float64 `json:"costLimitInUsd"`
CostLimitInUsdOverTime float64 `json:"costLimitInUsdOverTime"`
CostLimitInUsdUnit TimeUnit `json:"costLimitInUsdUnit"`
RateLimitOverTime int `json:"rateLimitOverTime"`
RateLimitUnit TimeUnit `json:"rateLimitUnit"`
Ttl string `json:"ttl"`
}
func (uk *UpdateKey) Validate() error {
invalid := []string{}
for _, tag := range uk.Tags {
if len(tag) == 0 {
invalid = append(invalid, "tags")
break
}
}
if uk.UpdatedAt <= 0 {
invalid = append(invalid, "updatedAt")
}
if uk.CostLimitInUsd < 0 {
invalid = append(invalid, "costLimitInUsd")
}
if uk.CostLimitInUsdOverTime < 0 {
invalid = append(invalid, "costLimitInUsdOverTime")
}
if uk.RateLimitOverTime < 0 {
invalid = append(invalid, "rateLimitOverTime")
}
if len(uk.Ttl) != 0 {
_, err := time.ParseDuration(uk.Ttl)
if err != nil {
invalid = append(invalid, "ttl")
}
}
if len(invalid) > 0 {
return internal_errors.NewValidationError(fmt.Sprintf("fields [%s] are invalid", strings.Join(invalid, ", ")))
}
if len(uk.RateLimitUnit) != 0 && uk.RateLimitOverTime == 0 {
return internal_errors.NewValidationError("rate limit over time can not be empty if rate limit unit is specified")
}
if len(uk.CostLimitInUsdUnit) != 0 && uk.CostLimitInUsdOverTime == 0 {
return internal_errors.NewValidationError("cost limit over time can not be empty if cost limit unit is specified")
}
if uk.RateLimitOverTime != 0 {
if len(uk.RateLimitUnit) == 0 {
return internal_errors.NewValidationError("rate limit unit can not be empty if rate limit over time is specified")
}
if uk.RateLimitUnit != HourTimeUnit && uk.RateLimitUnit != MinuteTimeUnit && uk.RateLimitUnit != SecondTimeUnit && uk.RateLimitUnit != DayTimeUnit {
return internal_errors.NewValidationError("rate limit unit can not be identified")
}
}
if uk.CostLimitInUsdOverTime != 0 {
if len(uk.CostLimitInUsdUnit) == 0 {
return internal_errors.NewValidationError("cost limit unit can not be empty if cost limit over time is specified")
}
if uk.CostLimitInUsdUnit != HourTimeUnit && uk.CostLimitInUsdUnit != DayTimeUnit {
return internal_errors.NewValidationError("cost limit unit can not be identified")
}
}
return nil
}
type RequestKey struct {
Name string `json:"name"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
Tags []string `json:"tags"`
KeyId string `json:"keyId"`
Key string `json:"key"`
CostLimitInUsd float64 `json:"costLimitInUsd"`
CostLimitInUsdOverTime float64 `json:"costLimitInUsdOverTime"`
CostLimitInUsdUnit TimeUnit `json:"costLimitInUsdUnit"`
RateLimitOverTime int `json:"rateLimitOverTime"`
RateLimitUnit TimeUnit `json:"rateLimitUnit"`
Ttl string `json:"ttl"`
}
func (rk *RequestKey) Validate() error {
invalid := []string{}
for _, tag := range rk.Tags {
if len(tag) == 0 {
invalid = append(invalid, "tags")
break
}
}
if rk.UpdatedAt <= 0 {
invalid = append(invalid, "updatedAt")
}
if len(rk.Key) == 0 {
invalid = append(invalid, "key")
}
if rk.CreatedAt <= 0 {
invalid = append(invalid, "createdAt")
}
if len(rk.Name) == 0 {
invalid = append(invalid, "name")
}
if len(rk.KeyId) == 0 {
invalid = append(invalid, "keyId")
}
if rk.CostLimitInUsd < 0 {
invalid = append(invalid, "costLimitInUsd")
}
if rk.CostLimitInUsdOverTime < 0 {
invalid = append(invalid, "costLimitInUsdOverTime")
}
if rk.RateLimitOverTime < 0 {
invalid = append(invalid, "rateLimitOverTime")
}
if len(rk.Ttl) != 0 {
_, err := time.ParseDuration(rk.Ttl)
if err != nil {
invalid = append(invalid, "ttl")
}
}
if len(invalid) > 0 {
return internal_errors.NewValidationError(fmt.Sprintf("fields [%s] are invalid", strings.Join(invalid, ", ")))
}
if len(rk.RateLimitUnit) != 0 && rk.RateLimitOverTime == 0 {
return internal_errors.NewValidationError("rate limit over time can not be empty if rate limit unit is specified")
}
if len(rk.CostLimitInUsdUnit) != 0 && rk.CostLimitInUsdOverTime == 0 {
return internal_errors.NewValidationError("cost limit over time can not be empty if cost limit unit is specified")
}
if rk.RateLimitOverTime != 0 {
if len(rk.RateLimitUnit) == 0 {
return internal_errors.NewValidationError("rate limit unit can not be empty if rate limit over time is specified")
}
if rk.RateLimitUnit != HourTimeUnit && rk.RateLimitUnit != MinuteTimeUnit && rk.RateLimitUnit != SecondTimeUnit && rk.RateLimitUnit != DayTimeUnit {
return internal_errors.NewValidationError("rate limit unit can not be identified")
}
}
if rk.CostLimitInUsdOverTime != 0 {
if len(rk.CostLimitInUsdUnit) == 0 {
return internal_errors.NewValidationError("cost limit unit can not be empty if cost limit over time is specified")
}
if rk.CostLimitInUsdUnit != DayTimeUnit && rk.CostLimitInUsdUnit != HourTimeUnit {
return internal_errors.NewValidationError("cost limit unit can not be identified")
}
}
return nil
}
type TimeUnit string
const (
HourTimeUnit TimeUnit = "h"
MinuteTimeUnit TimeUnit = "m"
SecondTimeUnit TimeUnit = "s"
DayTimeUnit TimeUnit = "d"
)
type ResponseKey struct {
Name string `json:"name"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
Tags []string `json:"tags"`
KeyId string `json:"keyId"`
Revoked bool `json:"revoked"`
Key string `json:"key"`
RevokedReason string `json:"revokedReason"`
CostLimitInUsd float64 `json:"costLimitInUsd"`
CostLimitInUsdOverTime float64 `json:"costLimitInUsdOverTime"`
CostLimitInUsdUnit TimeUnit `json:"costLimitInUsdUnit"`
RateLimitOverTime int `json:"rateLimitOverTime"`
RateLimitUnit TimeUnit `json:"rateLimitUnit"`
Ttl string `json:"ttl"`
}