-
Notifications
You must be signed in to change notification settings - Fork 6
/
lincub_request.go
70 lines (58 loc) · 1.43 KB
/
lincub_request.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
package eas
import (
"bytes"
json "encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
type LincubRequestData struct {
RequestId string `json:"request_id"`
Scene string `json:"scene"`
AlgoName string `json:"algo"`
UserId string `json:"user_id"`
Items []string `json:"items"`
Limit int `json:"limit"`
UserFeature map[string]interface{} `json:"user_feature"`
ItemFeatures []map[string]interface{} `json:"item_features"`
}
type LincubRequest struct {
EasRequest
}
func (r *LincubRequest) Invoke(requestData interface{}) (body interface{}, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("panic error:%v", e)
}
}()
request, ok := requestData.(*LincubRequestData)
if !ok {
err = errors.New("requestData is not LincubRequestData type")
return
}
data, _ := json.Marshal(request)
req, err := http.NewRequest("POST", r.url, bytes.NewBuffer(data))
if err != nil {
return
}
headers := map[string][]string{
"Authorization": {r.auth},
// "Content-Encoding": {"gzip"},
}
req.Header = headers
if easClient.Timeout == 0 {
easClient.Timeout = r.timeout
}
response, err := easClient.Do(req)
if err != nil {
return
}
defer response.Body.Close()
reqBody, err := io.ReadAll(response.Body)
if err != nil {
return
}
body = string(reqBody)
return
}