9f9107941e67f91e4dcda1292239d291f8fbca8b
[ealt-edge.git] / ocd / cli / ealt / cmd / adapter / httphelper.go
1 /*
2 Copyright 2020 Huawei Technologies Co., Ltd.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package adapter
18
19 import (
20         "bytes"
21         "ealt/cmd/common"
22         "fmt"
23         "io"
24         "io/ioutil"
25         "log"
26         "mime/multipart"
27         "net/http"
28         "os"
29         "strings"
30 )
31
32 var MECMClusterIP = os.Getenv("MECMClusterIP")
33 var APPLCMPort = os.Getenv("MECMClusterPort")
34 var ONBOARDPACKAGEPATH = os.Getenv("ONBOARDPACKAGEPATH")
35 var client = http.Client{}
36
37 func httpEndPointBuider(uri string) string {
38
39         return "http://" + strings.TrimSpace(MECMClusterIP) + ":" + strings.TrimSpace(APPLCMPort) + uri
40
41 }
42
43 func HttpDeleteRequestBuilder(uri string, body []byte) {
44
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")
49
50         if err != nil {
51                 log.Fatalln(err)
52         }
53         response, err := client.Do(request)
54         if err != nil {
55                 log.Fatalln(err)
56         }
57         defer response.Body.Close()
58
59         output, err := ioutil.ReadAll(response.Body)
60
61         if err != nil {
62                 log.Fatalln(err)
63         }
64
65         fmt.Println("Response Data: \n" + string(output))
66
67 }
68
69 func HttpPostRequestBuilder(uri string, body []byte) error {
70
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")
76
77         //fmt.Println(request)
78
79         if err != nil {
80                 log.Fatalln(err)
81         }
82         response, err := client.Do(request)
83         if err != nil {
84                 log.Fatalln(err)
85         }
86         defer response.Body.Close()
87
88         output, err := ioutil.ReadAll(response.Body)
89
90         if err != nil {
91                 log.Fatalln(err)
92         }
93
94         fmt.Println("Response Data: \n\n" + string(output))
95         return nil
96 }
97
98 func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
99
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)
105         if err != nil {
106                 log.Fatalln(err)
107         }
108
109         response, err := client.Do(request)
110         if err != nil {
111                 log.Fatalln(err)
112         } else {
113                 body := &bytes.Buffer{}
114                 _, err := body.ReadFrom(response.Body)
115                 if err != nil {
116                         log.Fatal(err)
117                 }
118                 response.Body.Close()
119                 fmt.Println(response.StatusCode)
120                 fmt.Println(response.Header)
121
122                 // fmt.Println(body)
123                 // var result map[string]interface{}
124                 // json.NewDecoder(response.Body).Decode(&result)
125                 // fmt.Println(result)
126         }
127
128         return nil
129 }
130
131 func getFilePathWithName(file string) string {
132
133         return ONBOARDPACKAGEPATH + common.PATHSLASH + file
134 }
135
136 func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, error) {
137
138         file, err := os.Open(filepath)
139         if err != nil {
140                 return nil, err
141         }
142
143         //close the file later
144         defer file.Close()
145
146         //Buffer to store the request body as bytes
147         var requestBody bytes.Buffer
148         multiPartWriter := multipart.NewWriter(&requestBody)
149
150         fileWriter, err := multiPartWriter.CreateFormFile("file_field", filepath)
151         if err != nil {
152                 log.Fatalln(err)
153         }
154
155         //Copy the actual file contents
156         _, err = io.Copy(fileWriter, file)
157         if err != nil {
158                 log.Fatalln(err)
159         }
160
161         //Close multiwriter
162         multiPartWriter.Close()
163
164         request, err := http.NewRequest(http.MethodPost, uri, &requestBody)
165         if err != nil {
166                 log.Fatalln(err)
167         }
168         request.Header.Add("Content-Type", multiPartWriter.FormDataContentType())
169         //request.Header.Set("Content-Type", "multipart/form-data")
170         return request, err
171 }