|
- package main
-
- import (
- "encoding/json"
- "errors"
- "io/ioutil"
- "log"
- )
-
- //EspoCRMAPIConfig CRM configuration for API accessing
- type EspoCRMAPIConfig struct {
-
- //where the EspoCRM is installed
- BaseURL string `json:"BaseURL"`
-
- //access UserName
- UserName string `json:"UserName"`
-
- //access Password
- UserPass string `json:"UserPass"`
-
- //where attachment is cached
- AttachmentCache string `json:"AttachmentCache"`
-
- //where cache site is provding attachment access
- //temporary
- CacheSiteURL string `json:"CacheSiteURL"`
- }
-
- //CRMConfig contains secrets that cannot store in source file
- //
- var CRMConfig EspoCRMAPIConfig
-
- func readCRMConfig() error {
- log.Printf("read CRM config from %s\r\n", "crm_config.json")
- body, err := ioutil.ReadFile("crm_config.json")
- if err != nil {
- log.Fatal("Cannot read config from crm_config.json")
- return err
- }
- err = json.Unmarshal(body, &CRMConfig)
- if CRMConfig.BaseURL == "" {
- return errors.New("CRM BaseURL not available not available")
- }
-
- if CRMConfig.UserName == "" {
- return errors.New("CRM UserName not available not available")
- }
-
- if CRMConfig.UserPass == "" {
- return errors.New("CRM UserPass not available not available")
- }
-
- 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")
- }
-
- func (c EspoCRMAPIConfig) apiAuthURL() string {
- return c.apiURL("App/user")
- }
-
- func (c EspoCRMAPIConfig) apiUploadAttachmentURL() string {
- return c.apiURL("Attachment/action/upload")
- }
-
- func (c EspoCRMAPIConfig) apiConvertLeadURL() string {
- return c.apiURL("Lead/action/convert")
- }
|