base.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_analysis/utils"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. )
  10. // PostEdbLib 调用指标接口
  11. func PostEdbLib(param map[string]interface{}, method string) (result []byte, err error) {
  12. postUrl := utils.EDB_LIB_URL + method
  13. postData, err := json.Marshal(param)
  14. if err != nil {
  15. return
  16. }
  17. result, err = HttpPost(postUrl, string(postData), "application/json")
  18. if err != nil {
  19. return
  20. }
  21. return
  22. }
  23. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  24. fmt.Println("HttpPost Url:" + url)
  25. body := ioutil.NopCloser(strings.NewReader(postData))
  26. client := &http.Client{}
  27. req, err := http.NewRequest("POST", url, body)
  28. if err != nil {
  29. return nil, err
  30. }
  31. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  32. if len(params) > 0 && params[0] != "" {
  33. contentType = params[0]
  34. }
  35. req.Header.Set("Content-Type", contentType)
  36. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  37. resp, err := client.Do(req)
  38. if err != nil {
  39. fmt.Println("client.Do err:" + err.Error())
  40. return nil, err
  41. }
  42. defer resp.Body.Close()
  43. b, err := ioutil.ReadAll(resp.Body)
  44. if err != nil {
  45. fmt.Println("HttpPost:" + string(b))
  46. }
  47. return b, err
  48. }
  49. func HttpGet(url string) ([]byte, error) {
  50. res, err := http.Get(url)
  51. if err != nil {
  52. return nil, err
  53. }
  54. defer res.Body.Close()
  55. return ioutil.ReadAll(res.Body)
  56. }