2 Copyright 2020 Huawei Technologies Co., Ltd.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
33 var MECMClusterIP = os.Getenv("MECMClusterIP")
34 var APPLCMPort = os.Getenv("MECMClusterPort")
35 var ONBOARDPACKAGEPATH = os.Getenv("ONBOARDPACKAGEPATH")
36 var client = http.Client{}
38 func httpEndPointBuider(uri string) string {
40 return "http://" + strings.TrimSpace(MECMClusterIP) + strings.TrimSpace(APPLCMPort) + uri
44 func HttpDeleteRequestBuilder(uri string, body []byte) {
46 uri = httpEndPointBuider(uri)
47 request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body))
48 request.Header.Set("Content-Type", "application/json")
54 response, err := client.Do(request)
58 defer response.Body.Close()
60 output, err := ioutil.ReadAll(response.Body)
66 fmt.Println(string(output))
70 func HttpPostRequestBuilder(uri string, body []byte) error {
72 uri = httpEndPointBuider(uri)
73 request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body))
74 request.Header.Set("Content-Type", "application/json")
82 response, err := client.Do(request)
86 defer response.Body.Close()
88 output, err := ioutil.ReadAll(response.Body)
94 fmt.Println(string(output))
98 func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
100 filepath := getFilePathWithName(file)
101 uri = httpEndPointBuider(uri)
103 request, err := fileUploadRequest(uri, "file", filepath)
105 response, err := client.Do(request)
110 var result map[string]interface{}
111 json.NewDecoder(response.Body).Decode(&result)
117 func getFilePathWithName(file string) string {
119 return ONBOARDPACKAGEPATH + common.PATHSLASH + file
122 func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, error) {
124 file, err := os.Open(filepath)
129 //close the file later
132 //Buffer to store the request body as bytes
133 var requestBody bytes.Buffer
134 multiPartWriter := multipart.NewWriter(&requestBody)
136 fileWriter, err := multiPartWriter.CreateFormFile("file_field", filepath)
141 //Copy the actual file contents
142 _, err = io.Copy(fileWriter, file)
148 multiPartWriter.Close()
150 request, err := http.NewRequest(http.MethodPost, uri, &requestBody)
154 request.Header.Set("Content-Type", "multipart/form-data")