-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathai.go
69 lines (55 loc) · 1.58 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
package utils
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func GenerateTerraformCode(appCode string, generationEndpoint string, webhookSecret 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("X-Webhook-Secret", webhookSecret) // Replace with your webhook secret
// 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
}