-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
81 lines (69 loc) · 1.77 KB
/
api.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
package main
import (
"bytes"
"encoding/json"
"strings"
"fmt"
"io/ioutil"
"log"
"errors"
"net/http"
)
var (
APIEndpoint = "http://172.16.1.121:8000"
APIKey = "yeet"
)
const (
INVALID_USER = "Invalid user"
)
func apiCheckUser(username string) error {
request, err := http.NewRequest("GET", APIEndpoint+"/user/" + username, nil)
request.Header.Set("X-Api-Key", "yeet")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
log.Println("ERROR:", err)
return err
}
defer response.Body.Close()
fmt.Println("response Status:", response.Status)
fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
if strings.Contains(string(body), "valid\":false") {
return errors.New(INVALID_USER)
}
return nil
}
func apiDeploy(template, catalog string, variants []string) error {
deployObj := DeployRequest{
Template: template,
Catalog: catalog,
Variants: variants,
MakeOwner: true,
}
jsonData, err := json.Marshal(deployObj)
if err != nil {
log.Println("ERROR:", err)
return err
}
fmt.Println("json data is", string(jsonData))
request, err := http.NewRequest("POST", APIEndpoint+"/deploy", bytes.NewBuffer(jsonData))
request.Header.Set("Content-Type", "application/json")
request.Header.Set("X-Api-Key", "yeet")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
log.Println("ERROR:", err)
return err
}
defer response.Body.Close()
fmt.Println("response Status:", response.Status)
fmt.Println("response Headers:", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(body))
if response.StatusCode != 200 {
return errors.New("Deploy failed: " + string(body))
}
return nil
}