-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathinsightServiceRequests.go
More file actions
84 lines (70 loc) · 1.98 KB
/
insightServiceRequests.go
File metadata and controls
84 lines (70 loc) · 1.98 KB
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
package libs
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/rs/zerolog/log"
config "github.com/thirdweb-dev/indexer/configs"
)
type DeployS3CommitterRequest struct {
ZeetDeploymentId string `json:"zeetDeploymentId"`
}
func DisableIndexerMaybeStartCommitter() {
makeS3CommitterRequest("deploy-s3-committer")
}
func RightsizeS3Committer() {
makeS3CommitterRequest("rightsize-s3-committer")
}
// makeS3CommitterRequest is a common function to make HTTP requests to the insight service
func makeS3CommitterRequest(endpoint string) {
serviceURL := config.Cfg.InsightServiceUrl
apiKey := config.Cfg.InsightServiceApiKey
zeetDeploymentId := config.Cfg.ZeetDeploymentId
// Prepare request payload
requestBody := DeployS3CommitterRequest{
ZeetDeploymentId: zeetDeploymentId,
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
log.Error().Err(err).Msg("Failed to marshal request body")
return
}
// Create HTTP request
url := fmt.Sprintf("%s/service/chains/%s/%s", serviceURL, ChainIdStr, endpoint)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
log.Error().Err(err).Msg("Failed to create HTTP request")
return
}
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-service-api-key", apiKey)
// Create HTTP client with timeout
client := &http.Client{
Timeout: 30 * time.Second,
}
// Send request
log.Info().
Str("url", url).
Str("endpoint", endpoint).
Str("zeetDeploymentId", zeetDeploymentId).
Msgf("Sending %s request", endpoint)
resp, err := client.Do(req)
if err != nil {
log.Error().Err(err).Msgf("Failed to send %s request", endpoint)
return
}
defer resp.Body.Close()
// Check response status
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
log.Info().
Int("statusCode", resp.StatusCode).
Msgf("Successfully sent %s request", endpoint)
} else {
log.Error().
Int("statusCode", resp.StatusCode).
Msgf("%s request failed", endpoint)
}
}