diff --git a/configCRM.go b/configCRM.go index fced4e0..b4025ad 100644 --- a/configCRM.go +++ b/configCRM.go @@ -46,3 +46,15 @@ func readCRMConfig() error { return err } + +func (c EspoCRMAPIConfig) apiURL(entityType string) string { + return c.BaseURL + "api/v1/" + entityType +} + +func (c EspoCRMAPIConfig) apiEntityURL(entityType, id string) string { + return c.apiURL(entityType) + "/" + id +} + +func (c EspoCRMAPIConfig) apiLeadURL() string { + return c.apiURL("Lead") +} diff --git a/crmEntity.go b/crmEntity.go index a57b470..b9abfca 100644 --- a/crmEntity.go +++ b/crmEntity.go @@ -12,7 +12,7 @@ import ( //abstract CRUD operation for espoCRM Entity func crmCreateEntity(entityType string, jsonB []byte) (entity interface{}, err error) { - url := CRMConfig.BaseURL + "api/v1/" + entityType + url := CRMConfig.apiURL(entityType) jsonStr, err := postRAW(jsonB, url, crmBuildCommonAPIHeader()) if err != nil { entity, _ = crmRescueDuplicateCreate(err, entityType) @@ -22,7 +22,7 @@ func crmCreateEntity(entityType string, jsonB []byte) (entity interface{}, err e } func crmUpdateEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { - url := CRMConfig.BaseURL + "api/v1/" + entityType + "/" + id + url := CRMConfig.apiEntityURL(entityType, id) jsonStr, err := patchRAW(jsonB, url, crmBuildCommonAPIHeader()) if err != nil { log.Println(err) @@ -32,7 +32,7 @@ func crmUpdateEntity(entityType string, id string, jsonB []byte) (entity interfa } func crmReplaceEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { - url := CRMConfig.BaseURL + "api/v1/" + entityType + "/" + id + url := CRMConfig.apiEntityURL(entityType, id) jsonStr, err := putRAW(jsonB, url, crmBuildCommonAPIHeader()) if err != nil { log.Println(err) @@ -42,7 +42,7 @@ func crmReplaceEntity(entityType string, id string, jsonB []byte) (entity interf } func crmDeleteEntity(entityType string, id string) (deleted bool, err error) { - url := CRMConfig.BaseURL + "api/v1/" + entityType + "/" + id + url := CRMConfig.apiEntityURL(entityType, id) resp, err := deleteRAW(url, crmBuildCommonAPIHeader()) if err != nil { log.Println(err) @@ -54,7 +54,7 @@ func crmDeleteEntity(entityType string, id string) (deleted bool, err error) { //give an id, return json func crmGetEntity(entityType string, id string) (entity interface{}, err error) { - url := CRMConfig.BaseURL + "api/v1/" + entityType + "/" + id + url := CRMConfig.apiEntityURL(entityType, id) jsonStr, err := getRAW(url, crmBuildCommonAPIHeader()) if err != nil { log.Println(err) diff --git a/crmLead.go b/crmLead.go index 3d04372..4fab944 100644 --- a/crmLead.go +++ b/crmLead.go @@ -99,7 +99,7 @@ func (m *crmdLead) getTimeLayout() string { func crmFindOpenID(openID string) (info crmdLead, found bool, err error) { found = false - req, err := http.NewRequest("GET", "https://c.hitxy.org.au/api/v1/Lead", nil) + req, err := http.NewRequest("GET", CRMConfig.apiLeadURL(), nil) if err != nil { log.Println("crmGetRecord: cannot form good http Request") log.Print(err) @@ -158,15 +158,6 @@ func crmFindOpenID(openID string) (info crmdLead, found bool, err error) { return } -//crmPrepareAttachmentHTTPHeader when uploading a file, we need its mime, auth header, etc. -func crmPrepareLeadUploadHTTPHeader() (headers map[string]string) { - headers = map[string]string{} - headers["Authorization"] = crmAuthHeader() - headers["Accept"] = "application/json" - headers["Content-Type"] = "application/json" - return headers -} - func crmGetLead(id string) (r crmdLead, err error) { entity, err := crmGetEntity("Lead", id) r = entity.(crmdLead) diff --git a/crmLeadStream.go b/crmLeadStream.go index 1bb5ae7..e9b63bb 100644 --- a/crmLeadStream.go +++ b/crmLeadStream.go @@ -93,9 +93,9 @@ func add2Stream(parentID, parentType, content string, attachmentsIDs []string) ( s.ParentType = parentType s.Post = content s.Type = "Post" - url := "https://c.hitxy.org.au/api/v1/Note" + url := CRMConfig.apiURL("Note") - headers := crmPrepareLeadUploadHTTPHeader() + headers := crmBuildCommonAPIHeader() b, err := json.Marshal(s) if err != nil { diff --git a/crmLeadStream_test.go b/crmLeadStream_test.go index 0900e96..7dd1949 100644 --- a/crmLeadStream_test.go +++ b/crmLeadStream_test.go @@ -78,7 +78,6 @@ func TestDecodeStream(t *testing.T) { } func TestAddNewStream(t *testing.T) { - parentid := "595071f8450974b72" parentType := "Lead"