-
Notifications
You must be signed in to change notification settings - Fork 29
/
helpers.go
89 lines (72 loc) · 2.25 KB
/
helpers.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
package main
import (
"fmt"
"net/http"
"reflect"
"strings"
h "github.com/RedHatInsights/sources-api-go/middleware/headers"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/labstack/echo/v4"
"github.com/redhatinsights/platform-go-middlewares/identity"
)
func getFilters(c echo.Context) ([]util.Filter, error) {
var filters []util.Filter
var ok bool
filterVal := c.Get("filters")
if filters, ok = filterVal.([]util.Filter); !ok {
return nil, fmt.Errorf("failed to pull filters from request")
}
return filters, nil
}
func getLimitAndOffset(c echo.Context) (int, int, error) {
var limit, offset int
var ok bool
limitVal := c.Get("limit")
if limit, ok = limitVal.(int); !ok {
return 0, 0, util.NewErrBadRequest("failed to parse limit")
}
if limit < 1 {
return 0, 0, util.NewErrBadRequest("limit is less than 1")
}
offsetVal := c.Get("offset")
if offset, ok = offsetVal.(int); !ok {
return 0, 0, util.NewErrBadRequest("failed to parse offset")
}
if offset < 0 {
return 0, 0, util.NewErrBadRequest("offset is less than 0")
}
return limit, offset, nil
}
func setNotificationForAvailabilityStatus(c echo.Context, previousStatus string, resource m.EmailNotification) {
c.Set("emailNotificationInfo", resource.ToEmail(previousStatus))
}
func setEventStreamResource(c echo.Context, model m.Event) {
// get the model type we're raising the event for
// 1. Strip the pointer symbol
// 2. Strip the package prefix ("model.")
t := reflect.TypeOf(model)
m := strings.TrimPrefix(t.String(), "*")
m = strings.TrimPrefix(m, "model.")
// get the event type that just happened based on the http request
event := ""
switch c.Request().Method {
case http.MethodPost:
event = ".create"
case http.MethodPatch:
event = ".update"
case http.MethodDelete:
event = ".destroy"
default:
c.Logger().Warnf("Unsupported request type, middleware should probably not be here: %v", c.Request().Method)
}
c.Set("event_type", m+event)
c.Set("resource", model)
}
func getAccountNumberFromEchoContext(c echo.Context) (string, error) {
id, ok := c.Get(h.ParsedIdentity).(*identity.XRHID)
if !ok {
return "", fmt.Errorf("failed to pull identity from context")
}
return id.Identity.AccountNumber, nil
}