| //abstract CRUD operation for espoCRM Entity | //abstract CRUD operation for espoCRM Entity | ||||
| var crmSite = "https://c.hitxy.org.au/" | var crmSite = "https://c.hitxy.org.au/" | ||||
| func createEntity(entityType string, jsonB []byte) (entity interface{}, err error) { | |||||
| func crmCreateEntity(entityType string, jsonB []byte) (entity interface{}, err error) { | |||||
| url := crmSite + "api/v1/" + entityType | url := crmSite + "api/v1/" + entityType | ||||
| jsonStr, err := postRAW(jsonB, url, crmBuildCommonAPIHeader()) | jsonStr, err := postRAW(jsonB, url, crmBuildCommonAPIHeader()) | ||||
| if err != nil { | if err != nil { | ||||
| entity, _ = rescueDuplicate(err, entityType) | |||||
| entity, _ = crmRescueDuplicateCreate(err, entityType) | |||||
| return | return | ||||
| } | } | ||||
| return json2Entity(entityType, jsonStr) | |||||
| return crmJSON2Entity(entityType, jsonStr) | |||||
| } | } | ||||
| func updateEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { | |||||
| func crmUpdateEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { | |||||
| url := crmSite + "api/v1/" + entityType + "/" + id | url := crmSite + "api/v1/" + entityType + "/" + id | ||||
| jsonStr, err := patchRAW(jsonB, url, crmBuildCommonAPIHeader()) | jsonStr, err := patchRAW(jsonB, url, crmBuildCommonAPIHeader()) | ||||
| if err != nil { | if err != nil { | ||||
| log.Println(err) | log.Println(err) | ||||
| return | return | ||||
| } | } | ||||
| return json2Entity(entityType, jsonStr) | |||||
| return crmJSON2Entity(entityType, jsonStr) | |||||
| } | } | ||||
| func replaceEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { | |||||
| func crmReplaceEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { | |||||
| url := crmSite + "api/v1/" + entityType + "/" + id | url := crmSite + "api/v1/" + entityType + "/" + id | ||||
| jsonStr, err := putRAW(jsonB, url, crmBuildCommonAPIHeader()) | jsonStr, err := putRAW(jsonB, url, crmBuildCommonAPIHeader()) | ||||
| if err != nil { | if err != nil { | ||||
| log.Println(err) | log.Println(err) | ||||
| return | return | ||||
| } | } | ||||
| return json2Entity(entityType, jsonStr) | |||||
| return crmJSON2Entity(entityType, jsonStr) | |||||
| } | } | ||||
| func deleteEntity(entityType string, id string) (deleted bool, err error) { | |||||
| func crmDeleteEntity(entityType string, id string) (deleted bool, err error) { | |||||
| url := crmSite + "api/v1/" + entityType + "/" + id | url := crmSite + "api/v1/" + entityType + "/" + id | ||||
| resp, err := deleteRAW(url, crmBuildCommonAPIHeader()) | resp, err := deleteRAW(url, crmBuildCommonAPIHeader()) | ||||
| if err != nil { | if err != nil { | ||||
| log.Println(err) | log.Println(err) | ||||
| return | return | ||||
| } | } | ||||
| return json2Entity(entityType, jsonStr) | |||||
| return crmJSON2Entity(entityType, jsonStr) | |||||
| } | } | ||||
| func crmBuildCommonAPIHeader() (headers map[string]string) { | func crmBuildCommonAPIHeader() (headers map[string]string) { | ||||
| } | } | ||||
| //given a json string, convert it to Typed structure | //given a json string, convert it to Typed structure | ||||
| func json2Entity(entityType string, data string) (r interface{}, err error) { | |||||
| func crmJSON2Entity(entityType string, data string) (r interface{}, err error) { | |||||
| switch entityType { | switch entityType { | ||||
| case "Lead": | case "Lead": | ||||
| e := crmdLead{} | e := crmdLead{} | ||||
| case "Account": | case "Account": | ||||
| //r = crmdAccount{} | //r = crmdAccount{} | ||||
| default: | default: | ||||
| log.Fatalf("json2Entity: Unknown EntityType %s", entityType) | |||||
| log.Fatalf("crmJSON2Entity: Unknown EntityType %s", entityType) | |||||
| } | } | ||||
| if err != nil { | if err != nil { | ||||
| return | return | ||||
| } | } | ||||
| func rescueDuplicate(e error, entityType string) (entity interface{}, success bool) { | |||||
| func crmRescueDuplicateCreate(e error, entityType string) (entity interface{}, success bool) { | |||||
| if !isErrIndicateDuplicate(e) { | if !isErrIndicateDuplicate(e) { | ||||
| return | return | ||||
| } | } | ||||
| yes = strings.ToLower(reason.Reason) == "duplicate" | yes = strings.ToLower(reason.Reason) == "duplicate" | ||||
| return | return | ||||
| } | } | ||||
| func searchEntity |