This repository was archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
76 lines (62 loc) · 1.47 KB
/
session.go
File metadata and controls
76 lines (62 loc) · 1.47 KB
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
package ot
import (
"context"
"fmt"
"io"
"reflect"
"strings"
"github.com/itcomusic/ot/internal/client"
"github.com/itcomusic/ot/internal/conn"
"github.com/itcomusic/ot/pkg/oscript"
)
var (
typeDialDebug = reflect.TypeOf(&conn.DialDebug{})
)
// Session a information about authentication user.
type Session struct {
ep *Endpoint
auth fmt.Stringer
}
func (s *Session) clone() *Session {
return &Session{
ep: &Endpoint{
dialer: s.ep.dialer,
},
auth: s.auth,
}
}
// Debug wraps dialer in debug.
func (s *Session) Debug(w io.Writer) *Session {
if reflect.TypeOf(s.ep.dialer) == typeDialDebug {
return s // ¯\_(ツ)_/¯
}
c := s.clone()
c.ep.dialer = &conn.DialDebug{Dial: s.ep.dialer, Out: w}
return c
}
func (s *Session) connect(ctx context.Context) (*client.Client, error) {
c, err := s.ep.dialer.DialContext(ctx)
if err != nil {
return nil, err
}
return client.New(c), nil
}
// Call invokes the service function, waits for it to complete, and returns its error status.
func (s *Session) Call(ctx context.Context, serviceMethod string, args oscript.M, reply interface{}) error {
dot := strings.LastIndex(serviceMethod, ".")
if dot <= 0 || dot == len(serviceMethod)-1 {
return errServiceMethod
}
if args == nil {
args = oscript.M{}
}
c, err := s.connect(ctx)
if err != nil {
return err
}
defer c.Close()
if err := errIn(c.Exec(serviceMethod[:dot], serviceMethod[dot+1:], s.auth, args, reply)); err != nil {
return err
}
return nil
}