builder_request.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-tools/utils/http_client"
  4. "net/http"
  5. )
  6. type BuilderRequest interface {
  7. HttpMethod() string
  8. RequestUrl() string
  9. Request(request *http_client.Request) error
  10. Response() *http_client.Response
  11. }
  12. type PostRequest struct {
  13. Url string
  14. Headers map[string]string
  15. Body []byte
  16. response *http_client.Response
  17. }
  18. func (req *PostRequest) HttpMethod() string {
  19. return http.MethodPost
  20. }
  21. func (req *PostRequest) RequestUrl() string {
  22. return req.Url
  23. }
  24. func (req *PostRequest) Request(request *http_client.Request) error {
  25. response, err := request.Post(req.Url, req.Body,
  26. http_client.WithRequestHeaders(req.Headers))
  27. if err != nil {
  28. return err
  29. }
  30. req.response = response
  31. return nil
  32. }
  33. func (req *PostRequest) Response() *http_client.Response {
  34. return req.response
  35. }
  36. type DeleteRequest struct {
  37. Url string
  38. Headers map[string]string
  39. PathParams map[string]string
  40. QueryParams map[string]string
  41. response *http_client.Response
  42. }
  43. func (req *DeleteRequest) HttpMethod() string {
  44. return http.MethodDelete
  45. }
  46. func (req *DeleteRequest) RequestUrl() string {
  47. return req.Url
  48. }
  49. func (req *DeleteRequest) Request(request *http_client.Request) error {
  50. response, err := request.Delete(req.Url,
  51. http_client.WithRequestHeaders(req.Headers),
  52. http_client.WithRequestPathParams(req.PathParams),
  53. http_client.WithRequestQueryParams(req.QueryParams))
  54. if err != nil {
  55. return err
  56. }
  57. req.response = response
  58. return nil
  59. }
  60. func (req *DeleteRequest) Response() *http_client.Response {
  61. return req.response
  62. }
  63. type PutRequest struct {
  64. Url string
  65. Headers map[string]string
  66. Body []byte
  67. response *http_client.Response
  68. }
  69. func (req *PutRequest) HttpMethod() string {
  70. return http.MethodPut
  71. }
  72. func (req *PutRequest) RequestUrl() string {
  73. return req.Url
  74. }
  75. func (req *PutRequest) Request(request *http_client.Request) error {
  76. response, err := request.Put(req.Url, req.Body,
  77. http_client.WithRequestHeaders(req.Headers))
  78. if err != nil {
  79. return err
  80. }
  81. req.response = response
  82. return nil
  83. }
  84. func (req *PutRequest) Response() *http_client.Response {
  85. return req.response
  86. }
  87. type GetRequest struct {
  88. Url string
  89. Headers map[string]string
  90. PathParams map[string]string
  91. QueryParams map[string]string
  92. response *http_client.Response
  93. }
  94. func (req *GetRequest) HttpMethod() string {
  95. return http.MethodGet
  96. }
  97. func (req *GetRequest) RequestUrl() string {
  98. return req.Url
  99. }
  100. func (req *GetRequest) Request(request *http_client.Request) error {
  101. response, err := request.Get(req.Url,
  102. http_client.WithRequestHeaders(req.Headers),
  103. http_client.WithRequestPathParams(req.PathParams),
  104. http_client.WithRequestQueryParams(req.QueryParams))
  105. if err != nil {
  106. return err
  107. }
  108. req.response = response
  109. return nil
  110. }
  111. func (req *GetRequest) Response() *http_client.Response {
  112. return req.response
  113. }