Skip to content

Commit

Permalink
feat: support custom http client (#58)
Browse files Browse the repository at this point in the history
* feat: support custom http client

* Update base.go

---------

Co-authored-by: hsluoyz <[email protected]>
  • Loading branch information
notdu and hsluoyz authored Mar 2, 2023
1 parent c7b0bc9 commit 8204ffd
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions casdoorsdk/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ import (
"strings"
)

var (
// client is a shared http Client.
client HttpClient = &http.Client{}
)

// SetHttpClient sets custom http Client.
func SetHttpClient(httpClient HttpClient) {
client = httpClient
}

// HttpClient interface has the method required to use a type as custom http client.
// The net/*http.Client type satisfies this interface.
type HttpClient interface {
Do(*http.Request) (*http.Response, error)
}

type Response struct {
Status string `json:"status"`
Msg string `json:"msg"`
Expand Down Expand Up @@ -65,8 +81,6 @@ func DoGetBytes(url string) ([]byte, error) {

// DoGetBytesRaw is a general function to get response from param url through HTTP Get method.
func DoGetBytesRaw(url string) ([]byte, error) {
client := &http.Client{}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -146,7 +160,6 @@ func DoPostBytesRaw(url string, contentType string, body io.Reader) ([]byte, err
contentType = "text/plain;charset=UTF-8"
}

client := &http.Client{}
var resp *http.Response

req, err := http.NewRequest("POST", url, body)
Expand Down

0 comments on commit 8204ffd

Please sign in to comment.