github.com/kaydxh/golang@v0.0.131/go/net/http/http_client.repository.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package http 23 24 import ( 25 "context" 26 "fmt" 27 28 context_ "github.com/kaydxh/golang/go/context" 29 "github.com/kaydxh/golang/go/encoding/protojson" 30 reflect_ "github.com/kaydxh/golang/go/reflect" 31 time_ "github.com/kaydxh/golang/go/time" 32 logs_ "github.com/kaydxh/golang/pkg/logs" 33 "google.golang.org/protobuf/proto" 34 ) 35 36 func (r *Repository[REQ, RESP]) PostPbJson(ctx context.Context, req *REQ) (resp *RESP, err error) { 37 return r.PostPbJsonWithUrl(ctx, r.Url, req) 38 } 39 40 func (r *Repository[REQ, RESP]) PostPbJsonWithUrl(ctx context.Context, url string, req *REQ) (resp *RESP, err error) { 41 42 logger := logs_.GetLogger(ctx) 43 tc := time_.New(true) 44 summary := func() { 45 tc.Tick("PostPbJson") 46 respProto, ok := any(resp).(proto.Message) 47 if ok { 48 logger.WithField("response", reflect_.TruncateBytes(proto.Clone(respProto))). 49 WithField("cost", tc.String()). 50 Info("recv") 51 } 52 } 53 defer summary() 54 55 reqProto, ok := any(req).(proto.Message) 56 if !ok { 57 return nil, fmt.Errorf("req is not proto message type") 58 } 59 logger.WithField("request", reflect_.TruncateBytes(proto.Clone(reqProto))).Info("send") 60 61 reqData, err := protojson.Marshal(reqProto) 62 if err != nil { 63 logger.WithError(err).WithField("req", req).Errorf("failed to marshal request") 64 return resp, err 65 } 66 67 var respData []byte 68 err = time_.RetryWithContext(ctx, func(ctx context.Context) error { 69 ctx, cancel := context_.WithTimeout(ctx, r.Timeout) 70 defer cancel() 71 72 respData, err = r.Client.PostJson(ctx, url, nil, reqData) 73 if err != nil { 74 logger.WithError(err).Errorf("failed to post json") 75 return err 76 } 77 return nil 78 }, r.RetryInterval, r.RetryTimes) 79 if err != nil { 80 return resp, err 81 } 82 83 var zeroResp RESP 84 resp = &zeroResp 85 err = protojson.Unmarshal(respData, any(resp).(proto.Message)) 86 if err != nil { 87 logger.WithError(err).Errorf("failed to unmarshal post response data") 88 return nil, err 89 } 90 91 return resp, nil 92 } 93 94 func (r *Repository[REQ, RESP]) PostPb(ctx context.Context, req *REQ) (resp *RESP, err error) { 95 96 logger := logs_.GetLogger(ctx) 97 tc := time_.New(true) 98 summary := func() { 99 tc.Tick("PostPbJson") 100 respProto, ok := any(resp).(proto.Message) 101 if ok { 102 logger.WithField("response", reflect_.TruncateBytes(proto.Clone(respProto))). 103 WithField("cost", tc.String()). 104 Info("recv") 105 } 106 } 107 defer summary() 108 109 reqProto, ok := any(req).(proto.Message) 110 if !ok { 111 return nil, fmt.Errorf("req is not proto message type") 112 } 113 logger.WithField("request", reflect_.TruncateBytes(proto.Clone(reqProto))).Info("recv") 114 115 reqData, err := proto.Marshal(reqProto) 116 if err != nil { 117 logger.WithError(err).WithField("req", req).Errorf("failed to marshal request") 118 return resp, err 119 } 120 121 var respData []byte 122 err = time_.RetryWithContext(ctx, func(ctx context.Context) error { 123 ctx, cancel := context_.WithTimeout(ctx, r.Timeout) 124 defer cancel() 125 126 respData, err = r.Client.PostPb(ctx, r.Url, nil, reqData) 127 if err != nil { 128 logger.WithError(err).Errorf("failed to post json, url: %v", r.Url) 129 return err 130 } 131 return nil 132 }, r.RetryInterval, r.RetryTimes) 133 if err != nil { 134 return resp, err 135 } 136 137 var zeroResp RESP 138 resp = &zeroResp 139 err = proto.Unmarshal(respData, any(resp).(proto.Message)) 140 if err != nil { 141 logger.WithError(err).Errorf("failed to unmarshal post response data") 142 return nil, err 143 } 144 145 return resp, nil 146 } 147 148 /* 149 func (r *Repository[REQ, RESP]) Do(ctx context.Context, method string, contentType string, req *REQ) (resp *RESP, err error) { 150 151 logger := logs_.GetLogger(ctx) 152 tc := time_.New(true) 153 summary := func() { 154 tc.Tick("PostPbJson") 155 respProto, ok := any(resp).(proto.Message) 156 if ok { 157 logger.WithField("response", reflect_.TruncateBytes(proto.Clone(respProto))). 158 WithField("cost", tc.String()). 159 Info("recv") 160 } 161 } 162 defer summary() 163 164 reqProto, ok := any(req).(proto.Message) 165 if !ok { 166 return nil, fmt.Errorf("req is not proto message type") 167 } 168 logger.WithField("request", reflect_.TruncateBytes(proto.Clone(reqProto))).Info("recv") 169 170 var ( 171 reqData []byte 172 respData []byte 173 ) 174 switch contentType { 175 case binding.MIMEJSON: 176 reqData, err = protojson.Marshal(reqProto) 177 case binding.MIMEPROTOBUF: 178 reqData, err = proto.Marshal(reqProto) 179 180 default: 181 reqData, err = protojson.Marshal(reqProto) 182 } 183 if err != nil { 184 logger.WithError(err).WithField("req", req).Errorf("failed to marshal request") 185 return resp, err 186 } 187 188 err = time_.RetryWithContext(ctx, func(ctx context.Context) error { 189 ctx, cancel := context_.WithTimeout(ctx, r.Timeout) 190 defer cancel() 191 192 respData, err = r.Client.PostPb(r.Url, nil, reqData) 193 if err != nil { 194 logger.WithError(err).Errorf("failed to post json") 195 return err 196 } 197 return nil 198 }, r.RetryInterval, r.RetryTimes) 199 if err != nil { 200 return resp, err 201 } 202 203 var zeroResp RESP 204 resp = &zeroResp 205 err = proto.Unmarshal(respData, any(resp).(proto.Message)) 206 if err != nil { 207 logger.WithError(err).Errorf("failed to unmarshal post response data") 208 return nil, err 209 } 210 211 return resp, nil 212 } 213 */