Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

76 lines
1.6KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "log"
  7. )
  8. //EspoCRMAPIConfig CRM configuration for API accessing
  9. type EspoCRMAPIConfig struct {
  10. //where the EspoCRM is installed
  11. BaseURL string `json:"BaseURL"`
  12. //access UserName
  13. UserName string `json:"UserName"`
  14. //access Password
  15. UserPass string `json:"UserPass"`
  16. //where attachment is cached
  17. AttachmentCache string `json:"AttachmentCache"`
  18. }
  19. //CRMConfig contains secrets that cannot store in source file
  20. //
  21. var CRMConfig EspoCRMAPIConfig
  22. func readCRMConfig() error {
  23. log.Printf("read CRM config from %s\r\n", "crm_config.json")
  24. body, err := ioutil.ReadFile("crm_config.json")
  25. if err != nil {
  26. log.Fatal("Cannot read config from crm_config.json")
  27. return err
  28. }
  29. err = json.Unmarshal(body, &CRMConfig)
  30. if CRMConfig.BaseURL == "" {
  31. return errors.New("CRM BaseURL not available not available")
  32. }
  33. if CRMConfig.UserName == "" {
  34. return errors.New("CRM UserName not available not available")
  35. }
  36. if CRMConfig.UserPass == "" {
  37. return errors.New("CRM UserPass not available not available")
  38. }
  39. return err
  40. }
  41. func (c EspoCRMAPIConfig) apiURL(entityType string) string {
  42. return c.BaseURL + "api/v1/" + entityType
  43. }
  44. func (c EspoCRMAPIConfig) apiEntityURL(entityType, id string) string {
  45. return c.apiURL(entityType) + "/" + id
  46. }
  47. func (c EspoCRMAPIConfig) apiLeadURL() string {
  48. return c.apiURL("Lead")
  49. }
  50. func (c EspoCRMAPIConfig) apiAuthURL() string {
  51. return c.apiURL("App/user")
  52. }
  53. func (c EspoCRMAPIConfig) apiUploadAttachmentURL() string {
  54. return c.apiURL("Attachment/action/upload")
  55. }
  56. func (c EspoCRMAPIConfig) apiConvertLeadURL() string {
  57. return c.apiURL("Lead/action/convert")
  58. }