Bläddra i källkod

crmd prefix for all crm related data structure

master
Patrick Peng Sun 8 år sedan
förälder
incheckning
0bdca13d05
4 ändrade filer med 58 tillägg och 39 borttagningar
  1. +36
    -37
      crmAttachment.go
  2. +1
    -1
      crmAttachment_test.go
  3. +1
    -1
      fileinfo.go
  4. +20
    -0
      sample_data/crm_attachment.json

+ 36
- 37
crmAttachment.go Visa fil

@@ -5,22 +5,41 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)

type crmFileInfo struct {
Name string `json:"name"`
Type string `json:"type"`
Role string `json:"role"`
Size int64 `json:"size"`
type crmdFileInfo struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Role string `json:"role,omitempty"`
Size int64 `json:"size,omitempty"`
}

type attachmentID struct {
type crmdAttachmentID struct {
ID string `json:"attachmentId"`
}

func crmUploadFile(path string) (fileID string, fileInfo crmFileInfo, err error) {
type crmdAttachment struct {
ID string `json:"id"` //"id": "591e55398345683ee",
Name string `json:"name"` //"name": "static_qr_code_without_logo.png",
Deleted bool `json:"deleted"` //"deleted": false,
Type string `json:"type"` //"type": "image/png",
Size int `json:"size"` //"size": 509,
SourceID string `json:"sourceId"` //"sourceId": null,
CreateAT string `json:"createAt"` //"createdAt": "2017-05-19 02:15:21",
Role string `json:"role"` //"role": "Attachment",
Storage string `json:"storage"` //"storage": null,
StorageFielPath string `json:"storageFilePath"` //"storageFilePath": null,
Global bool `json:"global"` // "global": false,
ParentID string `json:"parentId"` // "parentId": null,
ParentType string `json:"parentType"` // "parentType": null,
RelatedID string `json:"relatedId"` // "relatedId": null,
RelatedType string `json:"relatedType"` // "relatedType": null,
CreatedByID string `json:"createdById"` // "createdById": "1",
CreatedbyName string `json:"createdByName"` // "createdByName": "Admin"
}

func crmUploadFile(path string) (fileID string, fileInfo crmdFileInfo, err error) {

fileInfo, err = getFileInfo4CRM(path)
headers, err := crmPrepareAttachmentHTTPHeader(fileInfo)
@@ -28,8 +47,8 @@ func crmUploadFile(path string) (fileID string, fileInfo crmFileInfo, err error)
url := crmUploadAttachmentURL()
resp, err := postRAW([]byte(data), url, headers)
fileID, err = crmFileIDFromJSON(resp)
log.Println(fileID)
log.Println(err)
// log.Println(fileID)
// log.Println(err)
return
}

@@ -38,13 +57,13 @@ func crmUploadFile(path string) (fileID string, fileInfo crmFileInfo, err error)
// specifically used for one purpose only.
// i.e. upload attachment to EspoCRM
func crmFileIDFromJSON(jsonStr string) (fileID string, err error) {
attach := attachmentID{}
attach := crmdAttachmentID{}
err = json.Unmarshal([]byte(jsonStr), &attach)
return attach.ID, err
}

//crmPrepareAttachmentHTTPHeader when uploading a file, we need its mime, auth header, etc.
func crmPrepareAttachmentHTTPHeader(fileInfo crmFileInfo) (headers map[string]string, err error) {
func crmPrepareAttachmentHTTPHeader(fileInfo crmdFileInfo) (headers map[string]string, err error) {
headers = map[string]string{}
headers["Authorization"] = crmAuthHeader()
headers["Accept"] = "application/json"
@@ -68,7 +87,7 @@ func crmAuthHeader() string {
//"data:image/png;base64,iVB....="
//
//TODO: for big files, this might ave issue cause out of memory
func crmFileDataString(path string, fileInfo crmFileInfo) (encoded string, err error) {
func crmFileDataString(path string, fileInfo crmdFileInfo) (encoded string, err error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return "", err
@@ -80,7 +99,7 @@ func crmFileDataString(path string, fileInfo crmFileInfo) (encoded string, err e
return encoded, nil
}

func crmUploadAttachmentMeta(fileID string, fileInfo crmFileInfo) (resp string, err error) {
func crmUploadAttachmentMeta(fileID string, fileInfo crmdFileInfo) (resp string, err error) {
headers := map[string]string{}
headers["Authorization"] = crmAuthHeader()
headers["Accept"] = "application/json, text/javascript, */*; q=0.01"
@@ -88,7 +107,7 @@ func crmUploadAttachmentMeta(fileID string, fileInfo crmFileInfo) (resp string,

fileInfo.Role = "Attachment"
jb, _ := json.Marshal(fileInfo)
log.Println(string(jb))
//log.Println(string(jb))

URL := crmAttachmentMetaURL(fileID)
resp, err = putRAW(jb, URL, headers)
@@ -99,27 +118,7 @@ func crmAttachmentMetaURL(fileID string) string {
return CRMConfig.apiEntityURL("Attachment", fileID)
}

type attachmentInfo struct {
ID string `json:"id"` //"id": "591e55398345683ee",
Name string `json:"name"` //"name": "static_qr_code_without_logo.png",
Deleted bool `json:"deleted"` //"deleted": false,
Type string `json:"type"` // "type": "image\/png",
Size int `json:"size"` //"size": 509,
SourceID string `json:"sourceId"` //"sourceId": null,
CreateAT string `json:"createAt"` //"createdAt": "2017-05-19 02:15:21",
Role string `json:"role"` //"role": "Attachment",
Storage string `json:"storage"` //"storage": null,
StorageFielPath string `json:"storageFilePath"` //"storageFilePath": null,
Global bool `json:"global"` // "global": false,
ParentID string `json:"parentId"` // "parentId": null,
ParentType string `json:"parentType"` // "parentType": null,
RelatedID string `json:"relatedId"` // "relatedId": null,
RelatedType string `json:"relatedType"` // "relatedType": null,
CreatedByID string `json:"createdById"` // "createdById": "1",
CreatedbyName string `json:"createdByName"` // "createdByName": "Admin"
}

func crmCreateAttachment(path string) (result attachmentInfo, err error) {
func crmCreateAttachment(path string) (result crmdAttachment, err error) {
fileID, fileInfo, err := crmUploadFile(path)
aInfo, err := crmUploadAttachmentMeta(fileID, fileInfo)
if err != nil {
@@ -144,7 +143,7 @@ func crmDownloadAttachment(fileID string) (filename string, err error) {
headers := map[string]string{}
headers["Authorization"] = crmAuthHeader()
f, _, err := saveURLwithHTTPHeader(u, headers)
log.Println(f)
//log.Println(f)
return f, err
}


+ 1
- 1
crmAttachment_test.go Visa fil

@@ -31,7 +31,7 @@ func TestDecodeJsonResponse(t *testing.T) {
,"size":509,"sourceId":null,"createdAt":"2017-05-19 02:15:21","role":"Attachment","storage":null,"storageFilePath"
:null,"global":false,"parentId":null,"parentType":null,"relatedId":null,"relatedType":null,"createdById"
:"1","createdByName":"Admin"} `
info := attachmentInfo{}
info := crmdAttachment{}
err := json.Unmarshal([]byte(msg), &info)
AssertEqual(t, err, nil, "json decode shold be correct")
}

+ 1
- 1
fileinfo.go Visa fil

@@ -100,7 +100,7 @@ func isJPG(path string) bool {
}

//get file Info for uploading CRM attachment /files
func getFileInfo4CRM(path string) (info crmFileInfo, err error) {
func getFileInfo4CRM(path string) (info crmdFileInfo, err error) {
info.Name = filepath.Base(path) + filepath.Ext(path)
info.Type, _, err = getFileMime(path)
info.Size, err = getFileSize(path)

+ 20
- 0
sample_data/crm_attachment.json Visa fil

@@ -0,0 +1,20 @@
{
"id": "5959c71f7d0b5585a",
"name": "flag_us.png",
"deleted": false,
"type": "image\/png",
"size": 212,
"sourceId": null,
"createdAt": "2017-07-03 04:25:03",
"role": "Attachment",
"storage": null,
"storageFilePath": null,
"global": false,
"parentId": null,
"parentType": null,
"relatedId": "5959c7258c859fb92",
"relatedType": "Lead",
"relatedName": "test name",
"createdById": "1",
"createdByName": "Admin"
}

Laddar…
Avbryt
Spara