/
session_rest.go
65 lines (55 loc) · 1.32 KB
/
session_rest.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package session
import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"net/http"
)
type restSessionRepository struct {
Endpoint string
client http.Client
}
func (sr *restSessionRepository) CreateSession(session Session) (Session, error) {
var newSession Session
sessBytes, err := json.Marshal(session)
if err != nil {
return newSession, err
}
buf := bytes.NewBuffer(sessBytes)
resp, err := sr.client.Post(sr.Endpoint, "application/json", buf)
if err != nil {
log.Printf("error creating session: %v", err)
return newSession, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("error creating session: %v", err)
return newSession, err
}
err = json.Unmarshal(body, &newSession)
if err != nil {
log.Printf("error creating session: %v", err)
return newSession, err
}
log.Printf("created new session: %v", newSession)
return newSession, err
}
func (sr *restSessionRepository) DeleteSession(id string) error {
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, sr.Endpoint+"/"+id, http.NoBody)
if err != nil {
return err
}
resp, err := sr.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return err
}
func (sr *restSessionRepository) UpdateSession(id string, session Session) error {
return nil
}