package notice import ( "context" "errors" "net/http" "time" "git.zhangdeman.cn/zhangdeman/network/httpclient" "git.zhangdeman.cn/zhangdeman/network/httpclient/define" ) type networkRequest struct { Context context.Context `json:"-" dc:"请求上下文"` URL string `json:"url" dc:"请求地址"` Body []byte `json:"body" dc:"JSON 请求体"` Timeout time.Duration `json:"timeout" dc:"请求超时时间"` } type networkResponse struct { StatusCode int `json:"status_code" dc:"HTTP 响应状态码"` Body []byte `json:"body" dc:"HTTP 响应正文"` } type networkClient interface { Post(request networkRequest) (networkResponse, error) } type defaultNetworkClient struct{} func (defaultNetworkClient) Post(request networkRequest) (networkResponse, error) { ctx := request.Context if ctx == nil { ctx = context.Background() } if err := ctx.Err(); err != nil { return networkResponse{}, err } timeout := request.Timeout if timeout <= 0 { timeout = defaultTimeout } client, err := httpclient.NewHttpClient(&define.Request{ Ctx: ctx, Body: request.Body, Header: map[string]any{"Content-Type": "application/json"}, FullUrl: request.URL, ContentType: "application/json", Method: http.MethodPost, DataField: "BODY_ROOT", SuccessHttpCodeList: nil, SuccessCodeList: nil, ConnectTimeout: timeout.Milliseconds(), ReadTimeout: timeout.Milliseconds(), RetryRule: &define.RequestRetryRule{ RetryCount: 0, RetryTimeInterval: 0, RetryHttpCodeList: []int64{}, RetryBusinessCodeList: []string{}, }, }, nil) if err != nil { return networkResponse{}, err } response := client.Request() if response == nil { return networkResponse{}, errors.New("notice: network package returned nil response") } body := []byte(response.Data) if response.RestyResponse != nil { body = append([]byte(nil), response.RestyResponse.Bytes()...) } if response.RestyResponse == nil && response.FailInfo != nil { return networkResponse{}, errors.New(response.FailInfo.Message) } return networkResponse{StatusCode: response.HttpCode, Body: body}, nil }