|
|
|
|
|
|
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
import "log" |
|
|
import "log" |
|
|
|
|
|
import "encoding/json" |
|
|
|
|
|
import "strings" |
|
|
|
|
|
|
|
|
//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 createEntity(entityType string, jsonB []byte) (entity interface{}, err error) { |
|
|
url := crmSite + "api/v1/" + entityType |
|
|
url := crmSite + "api/v1/" + entityType |
|
|
json, err := postRAW(jsonB, url, crmBuildCommonAPIHeader()) |
|
|
|
|
|
|
|
|
jsonStr, err := postRAW(jsonB, url, crmBuildCommonAPIHeader()) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println(err) |
|
|
log.Println(err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
return json, err |
|
|
|
|
|
|
|
|
return json2Entity(entityType, jsonStr) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func updateEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { |
|
|
func updateEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { |
|
|
url := crmSite + "api/v1/" + entityType + "/" + id |
|
|
url := crmSite + "api/v1/" + entityType + "/" + id |
|
|
json, 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 json, err |
|
|
|
|
|
|
|
|
return json2Entity(entityType, jsonStr) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func replaceEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { |
|
|
func replaceEntity(entityType string, id string, jsonB []byte) (entity interface{}, err error) { |
|
|
url := crmSite + "api/v1/" + entityType + "/" + id |
|
|
url := crmSite + "api/v1/" + entityType + "/" + id |
|
|
json, 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 json, err |
|
|
|
|
|
|
|
|
return json2Entity(entityType, jsonStr) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func deleteEntity(entityType string, id string) (json string, err error) { |
|
|
|
|
|
|
|
|
func deleteEntity(entityType string, id string) (deleted bool, err error) { |
|
|
url := crmSite + "api/v1/" + entityType + "/" + id |
|
|
url := crmSite + "api/v1/" + entityType + "/" + id |
|
|
json, err = deleteRAW(url, crmBuildCommonAPIHeader()) |
|
|
|
|
|
|
|
|
resp, err := deleteRAW(url, crmBuildCommonAPIHeader()) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println(err) |
|
|
log.Println(err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
deleted = strings.ToLower(resp) == "true" |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//give an id, return json |
|
|
//give an id, return json |
|
|
func crmGetEntity(entityType string, id string) (json string, err error) { |
|
|
|
|
|
|
|
|
func crmGetEntity(entityType string, id string) (entity interface{}, err error) { |
|
|
url := crmSite + "api/v1/" + entityType + "/" + id |
|
|
url := crmSite + "api/v1/" + entityType + "/" + id |
|
|
json, err = getRAW(url, crmBuildCommonAPIHeader()) |
|
|
|
|
|
|
|
|
jsonStr, err := getRAW(url, crmBuildCommonAPIHeader()) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println(err) |
|
|
log.Println(err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
return json, err |
|
|
|
|
|
|
|
|
return json2Entity(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, json string) (r interface{}) { |
|
|
|
|
|
|
|
|
func json2Entity(entityType string, data string) (r interface{}, err error) { |
|
|
switch entityType { |
|
|
switch entityType { |
|
|
case "Lead": |
|
|
case "Lead": |
|
|
r = crmdLeadInfo |
|
|
|
|
|
|
|
|
e := crmdLead{} |
|
|
|
|
|
err = json.Unmarshal([]byte(data), &e) |
|
|
|
|
|
r = e |
|
|
case "Account": |
|
|
case "Account": |
|
|
|
|
|
//r = crmdAccount{} |
|
|
|
|
|
default: |
|
|
|
|
|
log.Fatalf("json2Entity: Unknown EntityType %s", entityType) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println(err) |
|
|
} |
|
|
} |
|
|
return |
|
|
return |
|
|
} |
|
|
} |