Newer
Older
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
package jrpc
import (
"encoding/json"
"errors"
"net/http"
)
type ResponseWriterMsg struct {
r *Request
n *Notifier
s *Subscription
msg *jsonrpcMessage
//TODO: add options
// currently there are no useful options so i havent added any
// find a use case to add
options options
}
type options struct {
}
func UpgradeToSubscription(w ResponseWriter, r *Request) (*Subscription, error) {
not, ok := NotifierFromContext(r.ctx)
if !ok || not == nil {
return nil, errors.New("subscription not supported")
}
return not.CreateSubscription(), nil
}
func NewReaderResponseWriterMsg(r *Request) *ResponseWriterMsg {
rw := &ResponseWriterMsg{
r: r,
}
rw.n, _ = NotifierFromContext(r.ctx)
return rw
}
func (w *ResponseWriterMsg) Header() http.Header {
wh := w.r.Peer().HTTP.WriteHeaders
return wh
}
func (w *ResponseWriterMsg) Option(k string, v any) {
}
func (w *ResponseWriterMsg) Send(args any, e error) (err error) {
cm := w.r.Msg()
if e != nil {
w.msg = cm.errorResponse(e)
return nil
}
switch c := args.(type) {
case *Subscription:
w.s = c
default:
}
w.msg = cm.response(args)
return nil
}
func (w *ResponseWriterMsg) Notify(args any) (err error) {
if w.s == nil || w.n == nil {
return ErrSubscriptionNotFound
}
bts, _ := json.Marshal(args)
err = w.n.send(w.s, bts)
if err != nil {
return err
}
return nil
}
func (w *ResponseWriterMsg) Result() *jsonrpcMessage {
return w.msg
}