-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlogout.go
More file actions
71 lines (57 loc) · 1.31 KB
/
Copy pathlogout.go
File metadata and controls
71 lines (57 loc) · 1.31 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
package gofair
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"time"
)
type logoutResult struct {
Token string `json:"token"`
Product string `json:"product"`
Status string `json:"status"`
Error string `json:"error"`
}
func (c *Client) Logout() (logoutResult, error) {
// build url
url := createUrl(identity_url, "logout")
// make request
resp, err := logoutRequest(c, url)
if err != nil {
return *new(logoutResult), err
}
var result logoutResult
// parse json
err = json.Unmarshal(resp, &result)
if err != nil {
return result, err
}
c.session.SessionToken = ""
c.session.LoginTime = time.Time{}
return result, nil
}
func logoutRequest(c *Client, url string) ([]byte, error) {
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return nil, err
}
// set headers
req.Header.Set("Accept", "application/json")
req.Header.Set("X-Application", c.config.AppKey)
req.Header.Set("X-Authentication", c.session.SessionToken)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}