-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathai.go
131 lines (104 loc) · 3.03 KB
/
ai.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 utils
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
func GenerateTerraformCode(appCode string, generationEndpoint string, apiToken string) (string, error) {
payload := map[string]string{
"code": appCode,
}
// Convert payload to JSON
jsonData, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("Error marshalling JSON: %v\n", err)
}
// Create request
req, err := http.NewRequest("POST", generationEndpoint, bytes.NewBuffer(jsonData))
if err != nil {
return "", fmt.Errorf("Error creating request: %v\n", err)
}
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiToken)
// Make the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("Error making request: %v\n", err)
}
defer resp.Body.Close()
// Read response
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("Error reading response: %v\n", err)
}
// Print response
if resp.StatusCode == 400 {
return "", fmt.Errorf("unable to generate terraform code from the code available, is it valid application code")
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("unexpected error occured while generating code")
}
type GeneratorResponse struct {
Result string `json:"result"`
Status string `json:"status"`
}
var response GeneratorResponse
err = json.Unmarshal(body, &response)
if err != nil {
return "", fmt.Errorf("unable to parse generator response: %v", err)
}
return response.Result, nil
}
func GetAiSummaryFromTerraformPlans(plans string, summaryEndpoint string, apiToken string) (string, error) {
payload := map[string]string{
"terraform_plans": plans,
}
// Convert payload to JSON
jsonData, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("Error marshalling JSON: %v\n", err)
}
// Create request
req, err := http.NewRequest("POST", summaryEndpoint, bytes.NewBuffer(jsonData))
if err != nil {
return "", fmt.Errorf("Error creating request: %v\n", err)
}
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiToken)
// Make the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("Error making request: %v\n", err)
}
defer resp.Body.Close()
// Read response
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("Error reading response: %v\n", err)
}
// Print response
if resp.StatusCode == 400 {
log.Printf("error while generating summary: %v", body)
return "", fmt.Errorf("unable to generate summary")
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("unexpected error occured while generating code")
}
type GeneratorResponse struct {
Result string `json:"result"`
Status string `json:"status"`
}
var response GeneratorResponse
err = json.Unmarshal(body, &response)
if err != nil {
return "", fmt.Errorf("unable to parse generator response: %v", err)
}
return response.Result, nil
}