-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription.go
127 lines (100 loc) · 3.75 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
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
package elation
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type SubscriptionServicer interface {
Find(ctx context.Context) ([]*Subscription, *http.Response, error)
Subscribe(ctx context.Context, opts *Subscribe) (*Subscription, *http.Response, error)
Delete(ctx context.Context, id int64) (*http.Response, error)
}
var _ SubscriptionServicer = (*SubscriptionService)(nil)
type SubscriptionService struct {
client *HTTPClient
}
type Subscription struct {
ID int64 `json:"id"`
Resource Resource `json:"resource"`
Target string `json:"target"`
CreatedDate SubscriptionJSONDate `json:"created_date"`
DeletedDate *SubscriptionJSONDate `json:"deleted_date"`
SigningPubKey string `json:"signing_pub_key"`
}
type Resource string
const (
ResourceAppointments Resource = "appointments"
ResourceDiscontinuedMedications Resource = "discontinued_medications"
ResourceMedications Resource = "medications"
ResourcePatients Resource = "patients"
ResourcePhysicians Resource = "physicians"
ResourceProblems Resource = "problems"
)
func (r Resource) String() string {
return string(r)
}
type SubscriptionJSONDate time.Time
func (s *SubscriptionJSONDate) MarshalJSON() ([]byte, error) {
return []byte("\"" + time.Time(*s).Format("2006-01-02T15:04:05") + "\""), nil
}
func (s *SubscriptionJSONDate) UnmarshalJSON(b []byte) error {
t, err := time.Parse("2006-01-02T15:04:05", strings.Trim(string(b), "\""))
if err != nil {
return err
}
*s = SubscriptionJSONDate(t)
return nil
}
func (s *SubscriptionService) Find(ctx context.Context) ([]*Subscription, *http.Response, error) {
ctx, span := s.client.tracer.Start(ctx, "find subscriptions", trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
out := &Response[[]*Subscription]{}
// The trailing slash in the path is required.
res, err := s.client.request(ctx, http.MethodGet, "/app/subscriptions/", nil, nil, &out)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "error making request")
return nil, res, fmt.Errorf("making request: %w", err)
}
return out.Results, res, nil
}
type Subscribe struct {
Resource Resource `json:"resource"`
Target string `json:"target"`
Properties json.RawMessage `json:"properties"`
}
func (s *SubscriptionService) Subscribe(ctx context.Context, subscribe *Subscribe) (*Subscription, *http.Response, error) {
ctx, span := s.client.tracer.Start(ctx, "subscribe", trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
out := &Subscription{}
// The trailing slash in the path is required.
res, err := s.client.request(ctx, http.MethodPost, "/app/subscriptions/", nil, subscribe, out)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "error making request")
return nil, res, fmt.Errorf("making request: %w", err)
}
return out, res, nil
}
type DeleteSubscriptionOptions struct {
ID int64 `url:"id"`
}
func (s *SubscriptionService) Delete(ctx context.Context, id int64) (*http.Response, error) {
ctx, span := s.client.tracer.Start(ctx, "delete subscription", trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(attribute.Int64("elation.subscription_id", id)))
defer span.End()
// The trailing slash in the path is required.
res, err := s.client.request(ctx, http.MethodDelete, "/app/subscriptions/"+strconv.FormatInt(id, 10)+"/", nil, nil, nil)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, "error making request")
return res, fmt.Errorf("making request: %w", err)
}
return res, nil
}