Skip to content

Commit

Permalink
test 400 when passing in invalid stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
glamboyosa committed Jul 20, 2024
1 parent 31b857b commit ed0c26a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fmt:
gofmt -s -w .
run-examples:
go run ./examples
go run ./examples/basic/basic_usage.go
7 changes: 4 additions & 3 deletions examples/basic/basic_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
)

func main() {
userId := "user_4f2G8T1zS4XkNoVfX8bW5E57P3U9"
rateLimiter := unkey.New(os.Getenv("ROOT_KEY"), unkey.UnkeyRateLimiterNew{
Namespace: "example",
Namespace: "osa.test",
Limit: 100,
Duration: 60,
Duration: 120000,
})

result, err := rateLimiter.Ratelimit(context.Background(), "user_123", nil)
result, err := rateLimiter.Ratelimit(context.Background(), userId, nil)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
Expand Down
9 changes: 5 additions & 4 deletions examples/options/with_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import (
)

func main() {
userId := "user_4f2G8T1zS4XkNoVfX8bW5E57P3U9"
rateLimiter := unkeygoratelimit.New(os.Getenv("ROOT_KEY"), unkeygoratelimit.UnkeyRateLimiterNew{
Namespace: "example",
Namespace: "osa.test",
Limit: 100,
Duration: 60,
Duration: 120000,
})

opts := &providers.UnkeyRateLimiterOptions{
Cost: 5,
Async: true,
Async: false,
}

result, err := rateLimiter.Ratelimit(context.Background(), "user_123", opts)
result, err := rateLimiter.Ratelimit(context.Background(), userId, opts)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
Expand Down
4 changes: 2 additions & 2 deletions examples/timeout/with_timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

func main() {
rateLimiter := unkeygoratelimit.New(os.Getenv("ROOT_KEY"), unkeygoratelimit.UnkeyRateLimiterNew{
Namespace: "example",
Namespace: "osa.test",
Limit: 100,
Duration: 60,
Duration: 120000,
Timeout: &unkeygoratelimit.UnkeyRateLimiterTimeout{
Ms: 5000,
Fallback: providers.RateLimitResult{
Expand Down
12 changes: 10 additions & 2 deletions limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ func New(rootKey string, i UnkeyRateLimiterNew) unkeyRateLimiterNewInit {
}
func (r *unkeyRateLimiterNewInit) Ratelimit(ctx context.Context, identifier string, opts *providers.UnkeyRateLimiterOptions) (providers.RateLimitResult, error) {
url := "https://api.unkey.dev/v1/ratelimits.limit"

fmt.Println(r, opts, identifier)
payload := mergePayload(r, opts, identifier)

payloadBytes, err := json.Marshal(payload)
if err != nil {
return providers.RateLimitResult{}, fmt.Errorf("error marshalling payload: %v", err)
}
fmt.Println(string(payloadBytes))

req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
Expand Down Expand Up @@ -95,7 +96,9 @@ func (r *unkeyRateLimiterNewInit) Ratelimit(ctx context.Context, identifier stri
if r.Timeout != nil {
return r.Timeout.Fallback, fmt.Errorf("unexpected status code: %v, body: %v using Fallback %v", res.StatusCode, string(body), r.Timeout.Fallback)
}
return providers.RateLimitResult{}, fmt.Errorf("unexpected status code: %v, body: %v using Fallback %v", res.StatusCode, string(body), r.Timeout.Fallback)
fmt.Println(res.StatusCode)

return providers.RateLimitResult{}, fmt.Errorf("unexpected status code: %v, body: %v using Fallback %v", res.StatusCode, string(body), nil)
}

fmt.Println("Response:", string(body))
Expand All @@ -108,15 +111,19 @@ func (r *unkeyRateLimiterNewInit) Ratelimit(ctx context.Context, identifier stri
}

func mergePayload(r *unkeyRateLimiterNewInit, opts *providers.UnkeyRateLimiterOptions, id string) providers.UnkeyRateLimiterPayload {

if opts == nil {

payload := providers.UnkeyRateLimiterPayload{
Namespace: r.Namespace,
Identifier: id,
Limit: r.Limit,
Duration: r.Duration,
}
fmt.Println("wth is the payload", payload)
return payload
} else {

payload := providers.UnkeyRateLimiterPayload{
Namespace: r.Namespace,
Identifier: id,
Expand All @@ -127,6 +134,7 @@ func mergePayload(r *unkeyRateLimiterNewInit, opts *providers.UnkeyRateLimiterOp
Meta: opts.Meta,
Resources: opts.Resources,
}
fmt.Println("wth is the payload",payload)
return payload
}
}
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
rateLimiter := unkeygoratelimit.New("your-root-key", unkeygoratelimit.UnkeyRateLimiterNew{
Namespace: "example",
Limit: 100,
Duration: 60,
Duration: 120000,
})

result, err := rateLimiter.Ratelimit(context.Background(), "user_123", nil)
Expand Down Expand Up @@ -67,7 +67,7 @@ func main() {
rateLimiter := unkeygoratelimit.New("your-root-key", unkeygoratelimit.UnkeyRateLimiterNew{
Namespace: "example",
Limit: 100,
Duration: 60,
Duration: 120000,
})

opts := &providers.UnkeyRateLimiterOptions{
Expand Down Expand Up @@ -105,7 +105,7 @@ func main() {
rateLimiter := unkeygoratelimit.New("your-root-key", unkeygoratelimit.UnkeyRateLimiterNew{
Namespace: "example",
Limit: 100,
Duration: 60,
Duration: 120000,
Timeout: &unkeygoratelimit.UnkeyRateLimiterTimeout{
Ms: 5000,
Fallback: providers.RateLimitResult{
Expand Down Expand Up @@ -138,7 +138,7 @@ Defines the configuration needed to initialize a new rate limiter instance.
type UnkeyRateLimiterNew struct {
Namespace string // Required. The namespace for your application.
Limit int // Required. The rate limit.
Duration int // Required. The duration of the rate limit in seconds.
Duration int // Required. The duration of the rate limit in milliseconds.
Timeout *UnkeyRateLimiterTimeout // Optional. Timeout settings and fallback result.
}
```
Expand Down

0 comments on commit ed0c26a

Please sign in to comment.