-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
110 lines (90 loc) · 2.1 KB
/
session.go
File metadata and controls
110 lines (90 loc) · 2.1 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
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
package regonapi
import (
"encoding/xml"
"errors"
"regexp"
)
// Login starts a new session
func (c *Client) Login() error {
params := struct {
XMLName xml.Name `xml:"ns:Zaloguj"`
Key string `xml:"ns:pKluczUzytkownika"`
}{
Key: c.key,
}
body, err := xml.MarshalIndent(params, "", " ")
if err != nil {
return err
}
b, err := c.call(publicEnvelope, "Zaloguj", string(body))
if err != nil {
return err
}
// MTOM/XOP encoded, use regex
r := regexp.MustCompile("<ZalogujResult>(.*)</ZalogujResult>")
s := r.FindStringSubmatch(string(b))
if len(s) == 0 {
return ErrInvalidKey
}
// First matching group
c.sid = s[1]
return nil
}
// Logout ends session
func (c *Client) Logout() error {
if c.sid == "" {
return ErrSessionNotStarted
}
params := struct {
XMLName xml.Name `xml:"ns:Wyloguj"`
SID string `xml:"ns:pIdentyfikatorSesji"`
}{
SID: c.sid,
}
body, err := xml.MarshalIndent(params, "", " ")
if err != nil {
return err
}
b, err := c.call(publicEnvelope, "Wyloguj", string(body))
if err != nil {
return err
}
// MTOM/XOP encoded, use regex
r := regexp.MustCompile("<WylogujResult>(.*)</WylogujResult>")
s := r.FindStringSubmatch(string(b))
if len(s) == 0 {
return errors.New("session not active")
}
return nil
}
func (c *Client) getValue(paramName string) (string, error) {
if c.sid == "" {
return "", ErrSessionNotStarted
}
params := struct {
XMLName xml.Name `xml:"ns:GetValue"`
ParamName string `xml:"ns:pNazwaParametru"`
}{
ParamName: paramName,
}
body, err := xml.MarshalIndent(params, "", " ")
if err != nil {
return "", err
}
b, err := c.call(privateEnvelope, "GetValue", string(body))
if err != nil {
return "", err
}
// MTOM/XOP encoded, use regex-a.
r := regexp.MustCompile("<GetValueResult>(.*)</GetValueResult>")
s := r.FindStringSubmatch(string(b))
if len(s) == 0 {
return "", ErrEmptyResult
}
return s[1], nil
}
// SessionStatus returns current session status: 1 = session active, 0 = session
// no longer active
func (c *Client) SessionStatus() (string, error) {
return c.getValue("StatusSesji")
}