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.
32 var MECMClusterIP = os.Getenv("MECMClusterIP")
33 var APPLCMPort = os.Getenv("MECMClusterPort")
34 var ONBOARDPACKAGEPATH = os.Getenv("ONBOARDPACKAGEPATH")
35 var client = http.Client{}
37 func httpEndPointBuider(uri string) string {
39 return "http://" + strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri
43 func HttpDeleteRequestBuilder(uri string, body []byte) {
45 uri = httpEndPointBuider(uri)
46 fmt.Println("Request URL :\t" + uri)
47 request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body))
48 request.Header.Set("Content-Type", "application/json")
53 response, err := client.Do(request)
57 defer response.Body.Close()
59 output, err := ioutil.ReadAll(response.Body)
65 fmt.Println("Response Data: \n" + string(output))
69 func HttpPostRequestBuilder(uri string, body []byte) error {
71 uri = httpEndPointBuider(uri)
72 fmt.Println("Request URL :\t" + uri)
73 fmt.Println("Request Body :\t" + string(body) + "\n")
74 request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body))
75 request.Header.Set("Content-Type", "application/json")
77 //fmt.Println(request)
82 response, err := client.Do(request)
86 defer response.Body.Close()
88 output, err := ioutil.ReadAll(response.Body)
94 fmt.Println("Response Data: \n\n" + string(output))
98 func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
100 filepath := getFilePathWithName(file)
101 fmt.Println("File Path :" + filepath)
102 uri = httpEndPointBuider(uri)
103 fmt.Println("Request URL :\t" + uri)
104 request, err := fileUploadRequest(uri, "file", filepath)
109 response, err := client.Do(request)
113 body := &bytes.Buffer{}
114 _, err := body.ReadFrom(response.Body)
118 response.Body.Close()
119 fmt.Println(response.StatusCode)
120 fmt.Println(response.Header)
123 // var result map[string]interface{}
124 // json.NewDecoder(response.Body).Decode(&result)
125 // fmt.Println(result)
131 func getFilePathWithName(file string) string {
133 return ONBOARDPACKAGEPATH + common.PATHSLASH + file
136 func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, error) {
138 file, err := os.Open(filepath)
143 //close the file later
146 //Buffer to store the request body as bytes
147 var requestBody bytes.Buffer
148 multiPartWriter := multipart.NewWriter(&requestBody)
150 fileWriter, err := multiPartWriter.CreateFormFile("file_field", filepath)
155 //Copy the actual file contents
156 _, err = io.Copy(fileWriter, file)
162 multiPartWriter.Close()
164 request, err := http.NewRequest(http.MethodPost, uri, &requestBody)
168 request.Header.Add("Content-Type", multiPartWriter.FormDataContentType())
169 //request.Header.Set("Content-Type", "multipart/form-data")