forked from Azure/buffalo-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.go
74 lines (61 loc) · 2.42 KB
/
subscriber.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
package eventgrid
import (
"encoding/json"
"errors"
"net/http"
"github.com/gobuffalo/buffalo"
)
// MaxPayloadSize is the largest number of bytes permitted in the body of
// a request sent to an Event Grid Topic.
const MaxPayloadSize = 1024 * 1024
// MaxEventSize is the largest number of bytes permitted for an individual
// Event Grid Event.
const MaxEventSize = 64 * 1024
// Event allows for easy processing of Event Grid Events.
//
// External documentation on Event Grid Events can be found here:
// https://docs.microsoft.com/en-us/azure/event-grid/event-schema
type Event struct {
ID string `json:"id"`
Topic string `json:"topic"`
Subject string `json:"subject"`
Data json.RawMessage `json:"data"`
EventType string `json:"eventType"`
EventTime string `json:"eventTime"`
MetadataVersion string `json:"metadataVersion"`
DataVersion string `json:"dataVersion"`
}
// UnmarshalData attempts to read the value associated with the "data" property
// into the value pointed to by v.
func (e Event) UnmarshalData(v interface{}) error {
return json.Unmarshal(e.Data, v)
}
// EventHandler extends the definition of buffalo.Handler to include
// an `Event`.
type EventHandler func(buffalo.Context, Event) error
// Subscriber allows for quick implementation of RESTful actions while
// working with Event Grid events.
type Subscriber interface {
List(buffalo.Context) error
New(buffalo.Context) error
Receive(buffalo.Context) error
Show(buffalo.Context) error
}
// BaseSubscriber will always respond to request by returning an HTTP 404 status.
type BaseSubscriber struct{}
// List responds with an HTTP NotFound Status Code.
func (s BaseSubscriber) List(c buffalo.Context) error {
return c.Error(http.StatusNotFound, errors.New("subscriber not implemented"))
}
// New responds with an HTTP NotFound Status Code.
func (s BaseSubscriber) New(c buffalo.Context) error {
return c.Error(http.StatusNotFound, errors.New("subscriber not implemented"))
}
// Receive responds with an HTTP NotFound Status Code.
func (s BaseSubscriber) Receive(c buffalo.Context) error {
return c.Error(http.StatusNotFound, errors.New("subscriber not implemented"))
}
// Show responds with an HTTP NotFound Status Code.
func (s BaseSubscriber) Show(c buffalo.Context) error {
return c.Error(http.StatusNotFound, errors.New("subscriber not implemented"))
}