This repository has been archived by the owner on Dec 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
subscription.go
82 lines (72 loc) · 2.25 KB
/
subscription.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
// Copyright 2018 Diego Bernardes. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flare
import (
"context"
"net/http"
"net/url"
"time"
)
// Subscription has all the information needed to notify the clients from changes on documents.
type Subscription struct {
ID string
Endpoint SubscriptionEndpoint
Delivery SubscriptionDelivery
Resource Resource
Partition string
Data map[string]interface{}
Content SubscriptionContent
CreatedAt time.Time
}
// SubscriptionContent configure the content delived by the subscription.
type SubscriptionContent struct {
Document bool
Envelope bool
}
// SubscriptionEndpoint has the address information to notify the clients.
type SubscriptionEndpoint struct {
URL *url.URL
Method string
Headers http.Header
Action map[string]SubscriptionEndpoint
}
// SubscriptionDelivery is used to control whenever the notification can be considered successful
// or not.
type SubscriptionDelivery struct {
Success []int
Discard []int
}
// All kinds of actions a subscription trigger has.
const (
SubscriptionTriggerCreate = "create"
SubscriptionTriggerUpdate = "update"
SubscriptionTriggerDelete = "delete"
)
// SubscriptionRepositorier is used to interact with the subscription data storage.
type SubscriptionRepositorier interface {
Find(context.Context, *Pagination, string) ([]Subscription, *Pagination, error)
FindByID(ctx context.Context, resourceID, id string) (*Subscription, error)
FindByPartition(
ctx context.Context, resourceID, partition string,
) (<-chan Subscription, <-chan error, error)
Create(context.Context, *Subscription) error
Delete(ctx context.Context, resourceID, id string) error
Trigger(
ctx context.Context,
action string,
document *Document,
subscription *Subscription,
fn func(context.Context, *Document, *Subscription, string) error,
) error
}
// SubscriptionTrigger is used to trigger the change on documents.
type SubscriptionTrigger interface {
Push(ctx context.Context, document *Document, action string) error
}
// SubscriptionRepositoryError represents all the errors the repository can return.
type SubscriptionRepositoryError interface {
error
NotFound() bool
AlreadyExists() bool
}