index.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package httpclient
  2. import (
  3. "bytes"
  4. "crm-api/pkg/lfshook"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "time"
  10. "gopkg.in/ini.v1"
  11. )
  12. func Get(url string) *http.Response {
  13. if url == "" {
  14. lfshook.NewLogger().Errorf("http get url is empty")
  15. return nil
  16. }
  17. // 若认证存在,添加到 get url
  18. // if configs.ConfigGlobal.Key != "" {
  19. // url = fmt.Sprintf("%s&key=%s", url, configs.ConfigGlobal.Key)
  20. // }
  21. lfshook.NewLogger().Infof("get %s", url)
  22. client := http.Client{
  23. Timeout: 10 * time.Second,
  24. }
  25. resp, err := client.Get(url)
  26. if err != nil {
  27. lfshook.NewLogger().Errorf("Error: unable to connect to - %s\n", err.Error())
  28. } else if resp.Status != "200 OK" {
  29. lfshook.NewLogger().Errorf("Error: non 200 status from - %s\n", resp.Status)
  30. }
  31. return resp
  32. }
  33. func BasicAuthGet(url string) *http.Response {
  34. if url == "" {
  35. lfshook.NewLogger().Errorf("http get url is empty")
  36. return nil
  37. }
  38. lfshook.NewLogger().Infof("get %s", url)
  39. client := http.Client{
  40. Timeout: 10 * time.Second,
  41. }
  42. // resp, err := client.Get(url)
  43. req, err := http.NewRequest("GET", url, nil)
  44. if err != nil {
  45. return nil
  46. }
  47. // 读取vtiger配置文件中的用户密码
  48. // confPath := "/etc/asterisk/vtiger_api.conf"
  49. confPath := "/etc/asterisk/crm_api.conf"
  50. cfg, err := ini.Load(confPath)
  51. if err != nil {
  52. lfshook.NewLogger().Error(err)
  53. return nil
  54. }
  55. BasicAuthUser := cfg.Section("general").Key("vtigerBasicAuthUser").String()
  56. BasicAuthPWD := cfg.Section("general").Key("vtigerBasicAuthPWD").String()
  57. if BasicAuthUser == "" || BasicAuthPWD == "" {
  58. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set BasicAuthUser or BasicAuthPWD")
  59. return nil
  60. }
  61. // 认证
  62. // res.SetBasicAuth("juncheng.du@zycoo.com", "8DJ3O28MCZ4sPAk5")
  63. // req.SetBasicAuth(configs.ConfigGlobal.BasicAuthUser, configs.ConfigGlobal.BasicAuthPWD)
  64. req.SetBasicAuth(BasicAuthUser, BasicAuthPWD)
  65. resp, err := client.Do(req)
  66. if err != nil {
  67. lfshook.NewLogger().Errorf("Error: unable to connect to - %s\n", err.Error())
  68. } else if resp.Status != "200 OK" {
  69. lfshook.NewLogger().Errorf("Error: non 200 status from - %s\n", resp.Status)
  70. }
  71. return resp
  72. }
  73. func ApiKeyGet(url string) *http.Response {
  74. if url == "" {
  75. lfshook.NewLogger().Errorf("http get url is empty")
  76. return nil
  77. }
  78. lfshook.NewLogger().Infof("get %s", url)
  79. client := http.Client{
  80. Timeout: 10 * time.Second,
  81. }
  82. // resp, err := client.Get(url)
  83. req, err := http.NewRequest("GET", url, nil)
  84. if err != nil {
  85. return nil
  86. }
  87. // 读取vtiger配置文件中的用户密码
  88. // confPath := "/etc/asterisk/vtiger_api.conf"
  89. confPath := "/etc/asterisk/crm_api.conf"
  90. cfg, err := ini.Load(confPath)
  91. if err != nil {
  92. lfshook.NewLogger().Error(err)
  93. return nil
  94. }
  95. ApiKey := cfg.Section("general").Key("vtigerApiKey").String()
  96. ApiKeyValue := cfg.Section("general").Key("vtigerApiKeyValue").String()
  97. if ApiKey == "" || ApiKeyValue == "" {
  98. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set ApiKey or ApiKeyValue")
  99. return nil
  100. }
  101. // 认证
  102. // req.Header.Set(configs.ConfigGlobal.ApiKey, configs.ConfigGlobal.ApiKeyValue)
  103. req.Header.Set(ApiKey, ApiKeyValue)
  104. resp, err := client.Do(req)
  105. if err != nil {
  106. lfshook.NewLogger().Errorf("Error: unable to connect to - %s\n", err.Error())
  107. } else if resp.Status != "200 OK" {
  108. lfshook.NewLogger().Errorf("Error: non 200 status from - %s\n", resp.Status)
  109. }
  110. // 读取请求后的响应
  111. data, err := ioutil.ReadAll(resp.Body)
  112. if err != nil {
  113. fmt.Println("读取请求后的响应时发生错误:", err)
  114. return nil
  115. }
  116. // 打印请求后的响应
  117. fmt.Printf("data = %+v\n", string(data))
  118. return resp
  119. }
  120. func ZohoGet(url string) *http.Response {
  121. if url == "" {
  122. lfshook.NewLogger().Errorf("http get url is empty")
  123. return nil
  124. }
  125. lfshook.NewLogger().Infof("get %s", url)
  126. client := http.Client{
  127. Timeout: 10 * time.Second,
  128. }
  129. // resp, err := client.Get(url)
  130. req, err := http.NewRequest("POST", url, nil)
  131. if err != nil {
  132. return nil
  133. }
  134. // 读取vtiger配置文件中的用户密码
  135. // confPath := "/etc/asterisk/vtiger_api.conf"
  136. confPath := "/etc/asterisk/crm_api.conf"
  137. cfg, err := ini.Load(confPath)
  138. if err != nil {
  139. lfshook.NewLogger().Error(err)
  140. return nil
  141. }
  142. AccessToken := cfg.Section("general").Key("zohoAccessToken").String()
  143. if AccessToken == "" {
  144. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoAccessToken")
  145. return nil
  146. }
  147. // 认证
  148. req.Header.Set("Authorization", "Bearer "+AccessToken)
  149. resp, err := client.Do(req)
  150. if err != nil {
  151. lfshook.NewLogger().Errorf("Error: unable to connect to - %s\n", err.Error())
  152. } else if resp.Status != "200 OK" {
  153. lfshook.NewLogger().Errorf("Error: non 200 status from - %s\n", resp.Status)
  154. }
  155. // 读取请求后的响应
  156. data, err := ioutil.ReadAll(resp.Body)
  157. if err != nil {
  158. fmt.Println("读取请求后的响应时发生错误:", err)
  159. return nil
  160. }
  161. // 打印请求后的响应
  162. fmt.Printf("data = %+v\n", string(data))
  163. return resp
  164. }
  165. func Post(event interface{}, url string) *http.Response {
  166. eventByte, err := json.Marshal(event)
  167. if err != nil {
  168. lfshook.NewLogger().Errorf("event marshall error %+v", err)
  169. return nil
  170. }
  171. data := bytes.NewBuffer([]byte(eventByte))
  172. client := http.Client{
  173. Timeout: 20 * time.Second,
  174. }
  175. resp, err := client.Post(url, "application/json", data)
  176. if err != nil {
  177. lfshook.NewLogger().Errorf("Error: unable to connect to - %s\n", err.Error())
  178. } else if resp.Status != "200 OK" {
  179. lfshook.NewLogger().Errorf("Error: non 200 status from - %s\n, event: %+v", resp.Status, event)
  180. }
  181. return resp
  182. }