Parcourir la source

centralize URL building for CRM API

master
Patrick Peng Sun il y a 8 ans
Parent
révision
a16881899b
5 fichiers modifiés avec 20 ajouts et 18 suppressions
  1. +12
    -0
      configCRM.go
  2. +5
    -5
      crmEntity.go
  3. +1
    -10
      crmLead.go
  4. +2
    -2
      crmLeadStream.go
  5. +0
    -1
      crmLeadStream_test.go

+ 12
- 0
configCRM.go Voir le fichier

@@ -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")
}

+ 5
- 5
crmEntity.go Voir le fichier

@@ -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)

+ 1
- 10
crmLead.go Voir le fichier

@@ -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)

+ 2
- 2
crmLeadStream.go Voir le fichier

@@ -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 {

+ 0
- 1
crmLeadStream_test.go Voir le fichier

@@ -78,7 +78,6 @@ func TestDecodeStream(t *testing.T) {
}

func TestAddNewStream(t *testing.T) {

parentid := "595071f8450974b72"
parentType := "Lead"

Chargement…
Annuler
Enregistrer