-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
131 lines (98 loc) · 2.82 KB
/
http.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package resharmonics
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/JoseFMP/resharmonics/auth"
"github.com/JoseFMP/resharmonics/urls"
"github.com/JoseFMP/resharmonics/utils"
)
var baseUrl = urls.GetBaseUrl()
func (clt *client) ensureTokenNotEmpty() {
if clt.token == "" {
clt.reqTokenChan <- &auth.AuthTask{}
newToken := <-clt.tokensChan
clt.token = newToken
}
}
func (clt *client) DoGet(subPath string, params map[string]interface{}) ([]byte, error) {
clt.ensureTokenNotEmpty()
endpoint := fmt.Sprintf("%s/%s", baseUrl, subPath)
req, errCreatingReq := utils.CreateGetReq(endpoint, params, clt.token)
if errCreatingReq != nil {
return nil, errCreatingReq
}
resPayload, errReadingBody := clt.doReq(req)
if errReadingBody != nil {
return nil, errReadingBody
}
return resPayload, nil
}
func (clt *client) doReq(req *http.Request) ([]byte, error) {
var res *http.Response
var errDoingReq error
for {
httpClient := http.Client{}
log.Printf("[doReq] Doing req: %s %s %s", req.Method, req.URL, req.URL.Query().Encode())
res, errDoingReq = httpClient.Do(req)
if errDoingReq != nil {
return nil, errDoingReq
}
if res.StatusCode == http.StatusOK {
break
}
if res.StatusCode != http.StatusForbidden {
return nil, fmt.Errorf("[doReq] Req result: %d -- %s %s", res.StatusCode, req.Method, req.URL.Path)
}
log.Println("[doReq] Needs to authenticate...")
usedToken := getUsedToken(req.Header)
clt.reqTokenChan <- &auth.AuthTask{WrongToken: usedToken}
newToken := <-clt.tokensChan
clt.token = newToken
injectToken(clt.token, req.Header)
log.Println("Authentication returned no error, should have new token")
}
respPayload, errReadingBody := ioutil.ReadAll(res.Body)
if errReadingBody != nil {
return nil, errReadingBody
}
return respPayload, nil
}
func (clt *client) DoPost(subPath string, params map[string]string) ([]byte, error) {
clt.ensureTokenNotEmpty()
endpoint := fmt.Sprintf("%s/%s", baseUrl, subPath)
req, errCreatingReq := utils.CreatePostReq(endpoint, params, clt.token)
if errCreatingReq != nil {
return nil, errCreatingReq
}
resPayload, errReadingBody := clt.doReq(req)
if errReadingBody != nil {
return nil, errReadingBody
}
return resPayload, nil
}
func getUsedToken(header http.Header) string {
if header == nil {
return ""
}
authHeader := header.Get(authHeaderName)
if authHeader == "" {
return ""
}
//cleaned := strings.ToLower(authHeader)
cleaned := strings.ReplaceAll(authHeader, "Bearer", "")
cleaned = strings.ReplaceAll(cleaned, " ", "")
return cleaned
}
func injectToken(token string, header http.Header) error {
if header == nil {
return fmt.Errorf("Header is nil")
}
header.Del(authHeaderName)
header.Add(authHeaderName, token)
return nil
}
const authHeaderName = `Authentication`
const bearerKeyword = `Bearer`