utils.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sender
  15. import (
  16. "context"
  17. "net/http"
  18. "net/url"
  19. "yunion.io/x/jsonutils"
  20. utilerr "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/httputils"
  22. "yunion.io/x/onecloud/pkg/notify/options"
  23. )
  24. const (
  25. LARK_MESSAGE_SUCCESS = "success"
  26. )
  27. var (
  28. cli = &http.Client{
  29. Transport: httputils.GetTransport(true),
  30. }
  31. )
  32. // 通知请求
  33. func sendRequest(ctx context.Context, url string, method httputils.THttpMethod, header http.Header, params url.Values, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  34. if header == nil {
  35. header = http.Header{}
  36. }
  37. if len(header.Values("Content-Type")) == 0 {
  38. header.Set("Content-Type", "application/json")
  39. }
  40. if params != nil {
  41. url += params.Encode()
  42. }
  43. _, resp, err := httputils.JSONRequest(cli, ctx, method, url, header, body, options.Options.DebugRequest)
  44. if err != nil {
  45. return resp, utilerr.Wrap(err, "http request")
  46. }
  47. return resp, nil
  48. }