forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simplify httpc (zeromicro#1748)
* feat: simplify httpc * chore: fix lint errors * chore: fix log url issue * chore: fix log url issue * refactor: handle resp & err in ResponseHandler * chore: remove unnecessary var names in return clause
- Loading branch information
Showing
8 changed files
with
91 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,44 @@ | ||
package httpc | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"github.com/zeromicro/go-zero/rest/httpc/internal" | ||
) | ||
|
||
// Do sends an HTTP request to the service assocated with the given key. | ||
func Do(key string, r *http.Request) (*http.Response, error) { | ||
return NewService(key).Do(r) | ||
var interceptors = []internal.Interceptor{ | ||
internal.LogInterceptor, | ||
} | ||
|
||
// DoRequest sends an HTTP request and returns an HTTP response. | ||
func DoRequest(r *http.Request) (*http.Response, error) { | ||
return request(r, defaultClient{}) | ||
} | ||
|
||
// Get sends an HTTP GET request to the service assocated with the given key. | ||
func Get(key, url string) (*http.Response, error) { | ||
return NewService(key).Get(url) | ||
type ( | ||
client interface { | ||
do(r *http.Request) (*http.Response, error) | ||
} | ||
|
||
defaultClient struct{} | ||
) | ||
|
||
func (c defaultClient) do(r *http.Request) (*http.Response, error) { | ||
return http.DefaultClient.Do(r) | ||
} | ||
|
||
// Post sends an HTTP POST request to the service assocated with the given key. | ||
func Post(key, url, contentType string, body io.Reader) (*http.Response, error) { | ||
return NewService(key).Post(url, contentType, body) | ||
func request(r *http.Request, cli client) (*http.Response, error) { | ||
var respHandlers []internal.ResponseHandler | ||
for _, interceptor := range interceptors { | ||
var h internal.ResponseHandler | ||
r, h = interceptor(r) | ||
respHandlers = append(respHandlers, h) | ||
} | ||
|
||
resp, err := cli.do(r) | ||
for i := len(respHandlers) - 1; i >= 0; i-- { | ||
respHandlers[i](resp, err) | ||
} | ||
|
||
return resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters