From b5a785bdedba0ddfa9f0c96ac83b37e89e3e3c2d Mon Sep 17 00:00:00 2001 From: ducnv Date: Wed, 28 Feb 2024 16:13:41 +0700 Subject: [PATCH] refactor: validate region name --- .gitignore | 5 +++++ client.go | 8 ++++++- constants/constants.go | 15 +++++++++++++ errors/errors.go | 49 ++++++++++++++++++++++++++++++++++++++++++ utils/parser.go | 17 +++++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 constants/constants.go create mode 100644 errors/errors.go create mode 100644 utils/parser.go diff --git a/.gitignore b/.gitignore index 7437dfe..89bd77f 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,8 @@ # End of https://www.gitignore.io/api/go .idea/ + +# Config VSCode IDE +.vscode + +build/ \ No newline at end of file diff --git a/client.go b/client.go index 34c5012..003a625 100644 --- a/client.go +++ b/client.go @@ -13,6 +13,8 @@ import ( "net/url" "path" "strings" + + "github.com/bizflycloud/gobizfly/utils" ) const ( @@ -117,7 +119,11 @@ func WithHTTPClient(client *http.Client) Option { // WithRegionName sets the client region for Bizfly client. func WithRegionName(region string) Option { return func(c *Client) error { - c.regionName = region + regionName, err := utils.ParseRegionName(region) + if err != nil { + return err + } + c.regionName = regionName return nil } } diff --git a/constants/constants.go b/constants/constants.go new file mode 100644 index 0000000..9d5c705 --- /dev/null +++ b/constants/constants.go @@ -0,0 +1,15 @@ +package constants + +const ( + HaNoiRegion = "HaNoi" + HoCHiMinhRegion = "HoChiMinh" + VcHaNoiRegion = "VC-HaNoi" +) + +var RegionMapping = map[string]string{ + "hn": HaNoiRegion, + "hanoi": HaNoiRegion, + "hcm": HoCHiMinhRegion, + "hochiminh": HoCHiMinhRegion, + "vc-hanoi": VcHaNoiRegion, +} diff --git a/errors/errors.go b/errors/errors.go new file mode 100644 index 0000000..977e1b9 --- /dev/null +++ b/errors/errors.go @@ -0,0 +1,49 @@ +package gobizflyErr + +import ( + "bytes" + "html/template" +) + +var tmpl = template.New("GobizflyTemplate") + +type GobizflyErr struct { + Message string + Code string + Metadata map[string]interface{} +} + +func (err GobizflyErr) Error() string { + message := err.GetMessage() + return message +} + +func (err GobizflyErr) String() string { + message := err.GetMessage() + return message +} + +func (err GobizflyErr) GetMessage() string { + newTmpl, tmplErr := tmpl.Parse(err.Message) + if tmplErr != nil { + return err.Message + } + var result bytes.Buffer + tmplErr = newTmpl.Execute(&result, err.Metadata) + if tmplErr != nil { + return err.Message + } + return result.String() +} + +func (err GobizflyErr) SetMetadata(metadata map[string]interface{}) GobizflyErr { + err.Metadata = metadata + return err +} + +var ( + InvalidRegion = GobizflyErr{ + Message: "Invalid region {{.Region}}", + Code: "InvalidRegion", + } +) diff --git a/utils/parser.go b/utils/parser.go new file mode 100644 index 0000000..fffd9bf --- /dev/null +++ b/utils/parser.go @@ -0,0 +1,17 @@ +package utils + +import ( + "strings" + + "github.com/bizflycloud/gobizfly/constants" + gobizflyErr "github.com/bizflycloud/gobizfly/errors" +) + +func ParseRegionName(region string) (string, error) { + lowerRegion := strings.ToLower(region) + regionName, ok := constants.RegionMapping[lowerRegion] + if !ok { + return "", gobizflyErr.InvalidRegion.SetMetadata(map[string]interface{}{"Region": region}) + } + return regionName, nil +}