forked from nicolai86/couchdb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_service.go
47 lines (42 loc) · 971 Bytes
/
session_service.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
package couchdb
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
)
// ReplicationService exposes non-admin user management apis
type SessionService struct {
c *Client
}
type SessionInfo struct {
Database string `json:"authentication_db"`
Handlers []string `json:"authentication_handlers"`
Authenticated string `json:"authenticated"`
}
type Session struct {
OK bool `json:"ok"`
Context *UserContext `json:"userCtx"`
Info SessionInfo `json:"info"`
}
func (s *SessionService) Get(ctx context.Context) (*Session, error) {
req, err := http.NewRequest("GET", "/_session", nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
resp, err := s.c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
sess := Session{}
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(bs, &sess); err != nil {
return nil, err
}
return &sess, nil
}