From 861ab17a07c44be2d299463490d6f0fe96b070ef Mon Sep 17 00:00:00 2001 From: Diogo De Santana Jacome Date: Thu, 21 Dec 2023 19:56:18 -0300 Subject: [PATCH] add readme --- .gitignore | 3 ++- Readme.md | 4 ++++ pkg/apiRequest/apiRequest.go | 20 ++++++++++---------- pkg/load/load.go | 19 ++++++++++++++----- 4 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 Readme.md diff --git a/.gitignore b/.gitignore index 432bd49..1db2103 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ # Go workspace file go.work -go.sum \ No newline at end of file +go.sum +response.json \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..8bfcabf --- /dev/null +++ b/Readme.md @@ -0,0 +1,4 @@ +VeracodeAPi +=========== + + \ No newline at end of file diff --git a/pkg/apiRequest/apiRequest.go b/pkg/apiRequest/apiRequest.go index d3c1701..395921c 100644 --- a/pkg/apiRequest/apiRequest.go +++ b/pkg/apiRequest/apiRequest.go @@ -1,37 +1,38 @@ package apirequest -import( - "net/http" - "net/url" +import ( + "bytes" "github.com/Didjacome/hmac-veracode/hmac" "io" + "net/http" + "net/url" "os" - "bytes" + "log" ) -func ApiRequest(apiKeyID, apiKeySecret, apiUrl, httpMethod string) string { +func ApiRequest(apiKeyID, apiKeySecret, apiUrl, httpMethod string) (string) { parsedUrl, err := url.Parse(apiUrl) if err != nil { - panic(err) + log.Fatalf("Error for Url Parse: %v", err) } client := &http.Client{} req, err := http.NewRequest(httpMethod, parsedUrl.String(), nil) if err != nil { - panic(err) + log.Fatalf("Error creating Request: %v", err) } authorizationHeader, err := hmac.CalculateAuthorizationHeader(parsedUrl, httpMethod, apiKeyID, apiKeySecret) if err != nil { - panic(err) + log.Fatalf("Error for calculating hmac: %v", err) } req.Header.Add("Authorization", authorizationHeader) resp, err := client.Do(req) if err != nil { - panic(err) + log.Fatalf("api response error validate your credentials or uri: %v", err) } if resp.StatusCode != http.StatusOK { panic("Expected status 200. Status was: " + resp.Status) @@ -42,7 +43,6 @@ func ApiRequest(apiKeyID, apiKeySecret, apiUrl, httpMethod string) string { panic(err) } - file, err := os.Create("response.json") if err != nil { panic(err) diff --git a/pkg/load/load.go b/pkg/load/load.go index 9e6afb2..10900ba 100644 --- a/pkg/load/load.go +++ b/pkg/load/load.go @@ -1,10 +1,12 @@ package load import ( - "strings" "github.com/Didjacome/varacode-api/schemas" "github.com/spf13/viper" + "log" + "strings" ) + var config *schemas.Veracode func Load() error { @@ -14,10 +16,18 @@ func Load() error { viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() + keys := []string{"Credetial.apiKeyID", "Credetial.apiKeySecret", "Credetial.apiUrl"} + err := viper.ReadInConfig() if err != nil { - if _, ok := err.(viper.ConfigFileNotFoundError); !ok { - return err + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + for _, key := range keys { + if !viper.IsSet(key) { + log.Fatalf("Error: %v \n Environment Not Foud: %v", err, key) + } + } + } else { + log.Fatalf("Error: %v", err) } } @@ -29,10 +39,9 @@ func Load() error { ApiUrl: viper.GetString("Credetial.apiUrl"), } - return err + return nil } - func GetApiCredetial() schemas.Auth { return config.ApiCredetial } \ No newline at end of file