...
1
18
19 package faas_containers
20
21 import (
22 "io/ioutil"
23 "scheduler/log"
24 "scheduler/types"
25 "scheduler/utils"
26 )
27
28 func GenFunctionExecute(functionName string, payload []byte, contentType string) (*types.FaasApiResponse, error) {
29 var res *types.FaasApiResponse
30 var err error
31
32 if payload == nil {
33 res, err = functionExecuteApiCall(functionName)
34 } else {
35 res, err = functionExecutePostApiCall(functionName, payload, contentType)
36 }
37
38 if err != nil {
39 return nil, err
40 }
41 if res.StatusCode == 404 {
42 return res, ErrorFunctionNotFound{}
43 }
44 if res.StatusCode >= 500 {
45 return res, ErrorInternal{string(res.Body)}
46 }
47 if res.StatusCode < 200 || res.StatusCode >= 300 {
48 return res, ErrorGeneric{string(res.Body)}
49 }
50
51 return res, nil
52 }
53
54
57
58 func functionExecuteApiCall(functionName string) (*types.FaasApiResponse, error) {
59 res, err := utils.HttpGet(GetApiFunctionUrl(functionName))
60 if err != nil {
61 log.Log.Debugf("Cannot create GET request to %s", err.Error(), GetApiFunctionUrl(functionName))
62 return nil, err
63 }
64
65 body, _ := ioutil.ReadAll(res.Body)
66 _ = res.Body.Close()
67
68 response := types.FaasApiResponse{
69 Headers: res.Header,
70 Body: body,
71 StatusCode: res.StatusCode,
72 }
73
74 return &response, err
75 }
76
77 func functionExecutePostApiCall(functionName string, payload []byte, contentType string) (*types.FaasApiResponse, error) {
78 res, err := utils.HttpPost(GetApiFunctionUrl(functionName), payload, contentType)
79 if err != nil {
80 log.Log.Debugf("Cannot create POST request to %s", err.Error(), GetApiFunctionUrl(functionName))
81 return nil, err
82 }
83
84 body, _ := ioutil.ReadAll(res.Body)
85 _ = res.Body.Close()
86
87 response := types.FaasApiResponse{
88 Headers: res.Header,
89 Body: body,
90 StatusCode: res.StatusCode,
91 }
92
93 return &response, err
94 }
95
View as plain text