Created
November 15, 2018 12:06
-
-
Save abhirockzz/62e67ad8f76ba23cae414d9f27598a41 to your computer and use it in GitHub Desktop.
Invoke a Function using OCI signature using OCI Go SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"time" | |
"github.com/oracle/oci-go-sdk/common" | |
) | |
func main() { | |
functionEndpoint := flag.String("http_trigger_endpoint", "", "Function HTTP Trigger endpoint") | |
flag.Parse() | |
if *functionEndpoint == "" { | |
fmt.Println("please specify function http trigger endpoint using --http_trigger_endpoint") | |
return | |
} | |
fmt.Println("Invoking function endpoint", *functionEndpoint) | |
client := http.Client{Timeout: 1 * time.Minute} | |
request, err := http.NewRequest("GET", *functionEndpoint, nil) | |
if err != nil { | |
fmt.Println("failed to create HTTP request", err) | |
return | |
} | |
request.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) | |
provider := common.DefaultConfigProvider() | |
signer := common.DefaultRequestSigner(provider) | |
err = signer.Sign(request) | |
if err != nil { | |
fmt.Println("Error signing request", err) | |
} | |
fmt.Println("Request signing done. Invoking function...") | |
var resp *http.Response | |
resp, err = client.Do(request) | |
if err != nil { | |
fmt.Println("error invoking function", err) | |
return | |
} | |
defer resp.Body.Close() | |
fmt.Println("function response status -", resp.Status) | |
var bytesResp []byte | |
if resp.StatusCode == http.StatusOK { | |
bytesResp, err = ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println("could not read response from response body", err) | |
return | |
} | |
functionResponse := string(bytesResp) | |
fmt.Println("function response -", functionResponse) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment