From fafd016fff44172bce6d7b06bafe8e5c3cdbdd4f Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Tue, 21 Dec 2021 14:29:44 +0300 Subject: [PATCH] add public methods for gettings allowed domains --- core/domains.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ core/domains_test.go | 26 +++++++++++++++++++ core/validator.go | 56 +++-------------------------------------- 3 files changed, 89 insertions(+), 53 deletions(-) create mode 100644 core/domains.go create mode 100644 core/domains_test.go diff --git a/core/domains.go b/core/domains.go new file mode 100644 index 0000000..2b14d63 --- /dev/null +++ b/core/domains.go @@ -0,0 +1,60 @@ +package core + +import ( + "encoding/json" + "io/ioutil" + "net/http" +) + +const crmDomainsURL string = "https://infra-data.retailcrm.tech/crm-domains.json" +const boxDomainsURL string = "https://infra-data.retailcrm.tech/box-domains.json" + +type Domain struct { + Domain string `json:"domain"` +} + +type CrmDomains struct { + CreateDate string `json:"createDate"` + Domains []Domain `json:"domains"` +} + +func GetSaasDomains() []Domain { + return getDomainsByStore(crmDomainsURL) +} + +func GetBoxDomains() []Domain { + return getDomainsByStore(boxDomainsURL) +} + +func getDomainsByStore(store string) []Domain { + req, reqErr := http.NewRequest(http.MethodGet, store, nil) + + if reqErr != nil { + return nil + } + + req.Header.Add("Accept", "application/json") + resp, respErr := http.DefaultClient.Do(req) + + if respErr != nil { + return nil + } + + respBody, readErr := ioutil.ReadAll(resp.Body) + + if readErr != nil { + return nil + } + + var crmDomains CrmDomains + + err := json.Unmarshal(respBody, &crmDomains) + + if err != nil { + return nil + } + + _ = resp.Body.Close() + + return crmDomains.Domains +} diff --git a/core/domains_test.go b/core/domains_test.go new file mode 100644 index 0000000..fc062f6 --- /dev/null +++ b/core/domains_test.go @@ -0,0 +1,26 @@ +package core + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_GetSaasDomains(t *testing.T) { + domains := GetSaasDomains() + + if domains == nil { + t.Fail() + } + + assert.NotEmpty(t, domains) +} + +func Test_GetBoxDomains(t *testing.T) { + domains := GetBoxDomains() + + if domains == nil { + t.Fail() + } + + assert.NotEmpty(t, domains) +} diff --git a/core/validator.go b/core/validator.go index 127d024..61e93d4 100644 --- a/core/validator.go +++ b/core/validator.go @@ -1,9 +1,6 @@ package core import ( - "encoding/json" - "io/ioutil" - "net/http" "net/url" "strings" @@ -11,9 +8,6 @@ import ( "github.com/go-playground/validator/v10" ) -const crmDomainsURL string = "https://infra-data.retailcrm.tech/crm-domains.json" -const boxDomainsURL string = "https://infra-data.retailcrm.tech/box-domains.json" - // init here will register `validateCrmURL` function for gin validator. func init() { if v, ok := binding.Validator.Engine().(*validator.Validate); ok { @@ -39,20 +33,18 @@ func isDomainValid(crmURL string) bool { mainDomain := getMainDomain(parseURL.Hostname()) - if checkDomains(crmDomainsURL, mainDomain) { + if checkDomains(GetSaasDomains(), mainDomain) { return true } - if checkDomains(boxDomainsURL, parseURL.Hostname()) { + if checkDomains(GetBoxDomains(), parseURL.Hostname()) { return true } return false } -func checkDomains(domainsStoreURL string, domain string) bool { - crmDomains := getDomainsByStore(domainsStoreURL) - +func checkDomains(crmDomains []Domain, domain string) bool { if nil == crmDomains { return false } @@ -86,45 +78,3 @@ func checkURLString(parseURL *url.URL) bool { return true } - -func getDomainsByStore(store string) []Domain { - req, reqErr := http.NewRequest(http.MethodGet, store, nil) - - if reqErr != nil { - return nil - } - - req.Header.Add("Accept", "application/json") - resp, respErr := http.DefaultClient.Do(req) - - if respErr != nil { - return nil - } - - respBody, readErr := ioutil.ReadAll(resp.Body) - - if readErr != nil { - return nil - } - - var crmDomains CrmDomains - - err := json.Unmarshal(respBody, &crmDomains) - - if err != nil { - return nil - } - - _ = resp.Body.Close() - - return crmDomains.Domains -} - -type Domain struct { - Domain string `json:"domain"` -} - -type CrmDomains struct { - CreateDate string `json:"createDate"` - Domains []Domain `json:"domains"` -}