Skip to content

Commit

Permalink
fix: httpclient JsonEncode/JsonDecode
Browse files Browse the repository at this point in the history
  • Loading branch information
lenye committed Dec 3, 2023
1 parent 9630587 commit 6612082
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* Linux
* macOS
* FreeBSD
* OpenBSD

## 文档

Expand Down
1 change: 1 addition & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Linux
* macOS
* FreeBSD
* OpenBSD

#### 使用二进制发行版

Expand Down
11 changes: 3 additions & 8 deletions internal/im/dingtalk/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package client

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

Expand All @@ -33,15 +31,12 @@ func CheckHttpResponseStatusCode(method, url string, statusCode int) error {

// PostJSON http post json
func PostJSON(url string, reqBody, respBody any) (http.Header, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(reqBody)
body, err := httpclient.JsonEncode(reqBody)
if err != nil {
return nil, err
}

resp, err := httpclient.Post(url, httpclient.HdrValApplicationJson, bytes.NewReader(buf.Bytes()))
resp, err := httpclient.Post(url, httpclient.HdrValApplicationJson, body)
if err != nil {
return nil, fmt.Errorf("%w; %s %s, %v", httpclient.ErrRequest, http.MethodPost, url, err)
}
Expand All @@ -51,5 +46,5 @@ func PostJSON(url string, reqBody, respBody any) (http.Header, error) {
return nil, err
}

return resp.Header, httpclient.DecodeResponse(resp.Body, respBody)
return resp.Header, httpclient.JsonDecode(resp.Body, respBody)
}
11 changes: 3 additions & 8 deletions internal/im/feishu/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package client

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

Expand All @@ -25,22 +23,19 @@ import (

// PostJSON http post json
func PostJSON(url string, reqBody, respBody any) (http.Header, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(reqBody)
body, err := httpclient.JsonEncode(reqBody)
if err != nil {
return nil, err
}

resp, err := httpclient.Post(url, httpclient.HdrValApplicationJson, bytes.NewReader(buf.Bytes()))
resp, err := httpclient.Post(url, httpclient.HdrValApplicationJson, body)
if err != nil {
return nil, fmt.Errorf("%w; %s %s, %v", httpclient.ErrRequest, http.MethodPost, url, err)
}
defer resp.Body.Close()

if resp.StatusCode/100 != 2 {
return resp.Header, httpclient.DecodeResponse(resp.Body, respBody)
return resp.Header, httpclient.JsonDecode(resp.Body, respBody)
}

return resp.Header, nil
Expand Down
15 changes: 5 additions & 10 deletions internal/im/weixin/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package client

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

Expand All @@ -42,20 +40,17 @@ func GetJSON(url string, respBody any) (http.Header, error) {
return nil, err
}

return resp.Header, httpclient.DecodeResponse(resp.Body, respBody)
return resp.Header, httpclient.JsonDecode(resp.Body, respBody)
}

// PostJSON http post json
func PostJSON(url string, reqBody, respBody any) (http.Header, error) {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(reqBody)
body, err := httpclient.JsonEncode(reqBody)
if err != nil {
return nil, err
}

resp, err := httpclient.Post(url, httpclient.HdrValApplicationJson, bytes.NewReader(buf.Bytes()))
resp, err := httpclient.Post(url, httpclient.HdrValApplicationJson, body)
if err != nil {
return nil, fmt.Errorf("%w; %s %s, %v", httpclient.ErrRequest, http.MethodPost, url, err)
}
Expand All @@ -65,7 +60,7 @@ func PostJSON(url string, reqBody, respBody any) (http.Header, error) {
return nil, err
}

return resp.Header, httpclient.DecodeResponse(resp.Body, respBody)
return resp.Header, httpclient.JsonDecode(resp.Body, respBody)
}

func PostFileJSON(url, fieldName, fileName string, respBody any) (http.Header, error) {
Expand All @@ -79,5 +74,5 @@ func PostFileJSON(url, fieldName, fileName string, respBody any) (http.Header, e
return nil, err
}

return resp.Header, httpclient.DecodeResponse(resp.Body, respBody)
return resp.Header, httpclient.JsonDecode(resp.Body, respBody)
}
13 changes: 0 additions & 13 deletions pkg/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package httpclient

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -173,15 +172,3 @@ func PostFile(url, formName, fileName string) (*http.Response, error) {
form := NewMultipartForm().AddFile(formName, fileName)
return PostMultipartForm(url, form)
}

func decodeJson(body io.Reader, v any) error {
return json.NewDecoder(body).Decode(v)
}

// DecodeResponse 解码响应
func DecodeResponse(body io.Reader, v any) error {
if v == nil {
return nil
}
return decodeJson(body, v)
}
42 changes: 42 additions & 0 deletions pkg/httpclient/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022-2023 The pmsg Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package httpclient

import (
"bytes"
"encoding/json"
"io"
)

func JsonDecode(body io.Reader, v any) error {
if v == nil {
return nil
}
return json.NewDecoder(body).Decode(v)
}

func JsonEncode(v any) (io.Reader, error) {
if v == nil {
return nil, nil
}
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(v)
if err != nil {
return nil, err
}
return buf, nil
}

0 comments on commit 6612082

Please sign in to comment.