mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-21 20:56:04 +03:00
add public methods for gettings allowed domains
This commit is contained in:
parent
7e21e73711
commit
fafd016fff
60
core/domains.go
Normal file
60
core/domains.go
Normal file
@ -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
|
||||
}
|
26
core/domains_test.go
Normal file
26
core/domains_test.go
Normal file
@ -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)
|
||||
}
|
@ -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"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user