-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
103 lines (91 loc) · 2.23 KB
/
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
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
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"github.com/getsentry/sentry-go"
"io/ioutil"
"log"
"net/http"
"time"
)
type Request struct {
Payload []byte
StoreEndpoint string
Kind string
Platform string
}
func NewRequest(event Event) *Request {
r := new(Request)
var bodyBytes []byte
var err error
if event.Kind == ERROR || event.Kind == DEFAULT {
bodyBytes, err = json.Marshal(event.Error)
r.Kind = ERROR
}
if event.Kind == TRANSACTION {
bodyBytes, err = json.Marshal(event.Transaction)
r.Kind = TRANSACTION
}
if err != nil {
sentry.CaptureException(err)
fmt.Println(err)
}
r.Platform = event.getPlatform()
r.Payload = bodyBytes
r.StoreEndpoint = event.DSN.storeEndpoint()
if r.StoreEndpoint == "" || r.Payload == nil {
sentry.CaptureException(errors.New("missing StoreEndpoint or Payload"))
log.Fatal("missing StoreEndpoint or Payload")
}
return r
}
func (r Request) send() {
time.Sleep(200 * time.Millisecond)
var payload []byte
size := len(r.Payload)
HUNDRED_KILOBYTES := 100000
if size > HUNDRED_KILOBYTES {
var buf bytes.Buffer
gw := gzip.NewWriter(&buf)
_, err := gw.Write(r.Payload)
if err != nil {
log.Fatal(err)
}
err = gw.Close()
if err != nil {
log.Fatal(err)
}
payload = buf.Bytes()
} else {
payload = r.Payload
}
request, errNewRequest := http.NewRequest("POST", r.StoreEndpoint, bytes.NewReader(payload)) // &buf
if errNewRequest != nil {
sentry.CaptureException(errNewRequest)
log.Fatalln(errNewRequest)
}
request.Header.Set("content-type", "application/json")
if size > HUNDRED_KILOBYTES {
request.Header.Set("Content-Encoding", "gzip")
}
// fmt.Printf("\n> storeEndpoint %v\n", r.StoreEndpoint)
if *ignore == false {
response, requestErr := httpClient.Do(request)
if requestErr != nil {
sentry.CaptureException(requestErr)
log.Fatal(requestErr)
}
responseData, responseDataErr := ioutil.ReadAll(response.Body)
if responseDataErr != nil {
sentry.CaptureException(responseDataErr)
log.Fatal(responseDataErr)
}
counter++
fmt.Printf("> Kind: %v | %v | Response: %v \n", r.Kind, r.Platform, string(responseData))
} else {
fmt.Printf("> event IGNORED %v | %v \n", r.Kind, r.Platform)
}
}