Просмотр исходного кода

crmd prefix for all crm related data structure

master
Patrick Peng Sun 8 лет назад
Родитель
Сommit
0bdca13d05
4 измененных файлов: 58 добавлений и 39 удалений
  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 Просмотреть файл

"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "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"` 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) fileInfo, err = getFileInfo4CRM(path)
headers, err := crmPrepareAttachmentHTTPHeader(fileInfo) headers, err := crmPrepareAttachmentHTTPHeader(fileInfo)
url := crmUploadAttachmentURL() url := crmUploadAttachmentURL()
resp, err := postRAW([]byte(data), url, headers) resp, err := postRAW([]byte(data), url, headers)
fileID, err = crmFileIDFromJSON(resp) fileID, err = crmFileIDFromJSON(resp)
log.Println(fileID)
log.Println(err)
// log.Println(fileID)
// log.Println(err)
return return
} }


// specifically used for one purpose only. // specifically used for one purpose only.
// i.e. upload attachment to EspoCRM // i.e. upload attachment to EspoCRM
func crmFileIDFromJSON(jsonStr string) (fileID string, err error) { func crmFileIDFromJSON(jsonStr string) (fileID string, err error) {
attach := attachmentID{}
attach := crmdAttachmentID{}
err = json.Unmarshal([]byte(jsonStr), &attach) err = json.Unmarshal([]byte(jsonStr), &attach)
return attach.ID, err return attach.ID, err
} }


//crmPrepareAttachmentHTTPHeader when uploading a file, we need its mime, auth header, etc. //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 = map[string]string{}
headers["Authorization"] = crmAuthHeader() headers["Authorization"] = crmAuthHeader()
headers["Accept"] = "application/json" headers["Accept"] = "application/json"
//"data:image/png;base64,iVB....=" //"data:image/png;base64,iVB....="
// //
//TODO: for big files, this might ave issue cause out of memory //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) content, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return "", err return "", err
return encoded, nil 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 := map[string]string{}
headers["Authorization"] = crmAuthHeader() headers["Authorization"] = crmAuthHeader()
headers["Accept"] = "application/json, text/javascript, */*; q=0.01" headers["Accept"] = "application/json, text/javascript, */*; q=0.01"


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


URL := crmAttachmentMetaURL(fileID) URL := crmAttachmentMetaURL(fileID)
resp, err = putRAW(jb, URL, headers) resp, err = putRAW(jb, URL, headers)
return CRMConfig.apiEntityURL("Attachment", fileID) 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) fileID, fileInfo, err := crmUploadFile(path)
aInfo, err := crmUploadAttachmentMeta(fileID, fileInfo) aInfo, err := crmUploadAttachmentMeta(fileID, fileInfo)
if err != nil { if err != nil {
headers := map[string]string{} headers := map[string]string{}
headers["Authorization"] = crmAuthHeader() headers["Authorization"] = crmAuthHeader()
f, _, err := saveURLwithHTTPHeader(u, headers) f, _, err := saveURLwithHTTPHeader(u, headers)
log.Println(f)
//log.Println(f)
return f, err return f, err
} }



+ 1
- 1
crmAttachment_test.go Просмотреть файл

,"size":509,"sourceId":null,"createdAt":"2017-05-19 02:15:21","role":"Attachment","storage":null,"storageFilePath" ,"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" :null,"global":false,"parentId":null,"parentType":null,"relatedId":null,"relatedType":null,"createdById"
:"1","createdByName":"Admin"} ` :"1","createdByName":"Admin"} `
info := attachmentInfo{}
info := crmdAttachment{}
err := json.Unmarshal([]byte(msg), &info) err := json.Unmarshal([]byte(msg), &info)
AssertEqual(t, err, nil, "json decode shold be correct") AssertEqual(t, err, nil, "json decode shold be correct")
} }

+ 1
- 1
fileinfo.go Просмотреть файл

} }


//get file Info for uploading CRM attachment /files //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.Name = filepath.Base(path) + filepath.Ext(path)
info.Type, _, err = getFileMime(path) info.Type, _, err = getFileMime(path)
info.Size, err = getFileSize(path) info.Size, err = getFileSize(path)

+ 20
- 0
sample_data/crm_attachment.json Просмотреть файл

{
"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"
}

Загрузка…
Отмена
Сохранить