github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/webservice.go (about) 1 package funcs 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "time" 11 ) 12 13 type WebServiceCallFunc struct{} 14 15 type Response struct { 16 Data interface{} `json:"data"` 17 } 18 19 func (w *WebServiceCallFunc) Execute(f *Funcs) { 20 // function execution start time 21 startTime := time.Now() 22 defer func() { 23 // calculate elapsed time 24 elapsed := time.Since(startTime) 25 // log performance with duration 26 f.iLog.PerformanceWithDuration("engine.funcs.WebServiceCallFunc.Execute", elapsed) 27 }() 28 29 defer func() { 30 // recover from any panics 31 if err := recover(); err != nil { 32 // log the error 33 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.WebServiceCallFunc.Execute with error: %s", err)) 34 // cancel execution and set error message 35 f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WebServiceCallFunc.Execute with error: %s", err)) 36 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.WebServiceCallFunc.Execute with error: %s", err) 37 } 38 }() 39 40 f.iLog.Debug(fmt.Sprintf("Start process %s : %s", "WebServiceCallFunc.Execute", f.Fobj.Name)) 41 42 namelist, valuelist, _ := f.SetInputs() 43 44 url := "" 45 method := "" 46 body := make(map[string]interface{}) 47 timeout := 30 48 for i, name := range namelist { 49 namestr := strings.ToUpper(name) 50 if namestr == "URL" { 51 url = valuelist[i] 52 } else if namestr == "METHOD" { 53 method = valuelist[i] 54 } else { 55 body[name] = valuelist[i] 56 } 57 } 58 59 if url == "" { 60 f.iLog.Error(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", "URL is empty")) 61 f.CancelExecution(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", "URL is empty")) 62 f.ErrorMessage = fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", "URL is empty") 63 return 64 } 65 66 if method == "" { 67 method = "GET" 68 } 69 70 if timeout == 0 { 71 timeout = 30 72 } 73 74 jsonBody, err := json.Marshal(body) 75 if err != nil { 76 f.iLog.Error(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 77 f.CancelExecution(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 78 f.ErrorMessage = fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err) 79 return 80 } 81 82 client := &http.Client{} 83 req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonBody)) 84 if err != nil { 85 f.iLog.Error(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 86 f.CancelExecution(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 87 f.ErrorMessage = fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err) 88 return 89 } 90 req.Header.Set("Content-Type", "application/json") 91 92 resp, err := client.Do(req) 93 if err != nil { 94 f.iLog.Error(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 95 f.CancelExecution(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 96 f.ErrorMessage = fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err) 97 return 98 } 99 100 defer resp.Body.Close() 101 102 respBody, err := ioutil.ReadAll(resp.Body) 103 if err != nil { 104 f.iLog.Error(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 105 f.CancelExecution(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 106 f.ErrorMessage = fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err) 107 return 108 } 109 110 if resp.StatusCode != http.StatusOK { 111 f.iLog.Error(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", string(resp.StatusCode))) 112 f.CancelExecution(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", string(resp.StatusCode))) 113 f.ErrorMessage = fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", string(resp.StatusCode)) 114 return 115 } 116 117 var response Response 118 119 err = json.Unmarshal(respBody, &response) 120 if err != nil { 121 f.iLog.Error(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 122 f.CancelExecution(fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err)) 123 f.ErrorMessage = fmt.Sprintf("Error in WebServiceCallFunc.Execute: %s", err) 124 return 125 } 126 f.iLog.Debug(fmt.Sprintf("WebServiceCallFunc response: %v", response)) 127 128 outputs := make(map[string]interface{}) 129 outputs["Response"] = response.Data 130 outputs["StatusCode"] = resp.StatusCode 131 132 f.SetOutputs(outputs) 133 }