-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
59 lines (45 loc) · 1.01 KB
/
util.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
package betfair
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"io"
"net/http"
)
func ReadJson(res *http.Response, response any) error {
defer res.Body.Close()
if body, err := io.ReadAll(res.Body); err == nil {
return json.Unmarshal(body, &response)
}
return nil
}
func ReadStream[T any](connection *tls.Conn, reads chan<- T) (err error) {
defer close(reads)
if connection == nil {
return errors.New("connection not established: please authenticate")
}
dec := json.NewDecoder(connection)
for dec.More() {
var x T
if err = dec.Decode(&x); err != nil && err != io.EOF {
break
}
reads <- x
}
return err
}
func GetTLSConfig(certFilePath string, keyFilePath string) (*tls.Config, error) {
certPool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
clientTLSCert, err := tls.LoadX509KeyPair(certFilePath, keyFilePath)
if err != nil {
return nil, err
}
return &tls.Config{
RootCAs: certPool,
Certificates: []tls.Certificate{clientTLSCert},
}, nil
}