1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

replace ioutil with io package and other minor improvements

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-06-17 22:03:18 +03:00
parent 20d2855a66
commit ef2643b046
108 changed files with 1069 additions and 1021 deletions

View File

@@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
@@ -281,7 +280,7 @@ func (c *Client) Do(ctx context.Context, method, urlpath string, payload interfa
// DrainResponseBody drains response body and close it, allowing the transport to reuse TCP connections.
// It's automatically called on Client.ReadXXX methods on the end.
func (c *Client) DrainResponseBody(resp *http.Response) {
_, _ = io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
@@ -398,7 +397,7 @@ func (c *Client) ReadJSON(ctx context.Context, dest interface{}, method, urlpath
}
// DBUG
// b, _ := ioutil.ReadAll(resp.Body)
// b, _ := io.ReadAll(resp.Body)
// println(string(b))
// return json.Unmarshal(b, &dest)
@@ -418,7 +417,7 @@ func (c *Client) ReadPlain(ctx context.Context, dest interface{}, method, urlpat
return ExtractError(resp)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
@@ -495,7 +494,7 @@ func BindResponse(resp *http.Response, dest interface{}) (err error) {
case contentTypeJSON: // the most common scenario on successful responses.
return json.NewDecoder(resp.Body).Decode(&dest)
case contentTypePlainText:
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

View File

@@ -2,7 +2,7 @@ package client
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strings"
)
@@ -39,7 +39,7 @@ func (e APIError) Error() string {
// ExtractError returns the response wrapped inside an APIError.
func ExtractError(resp *http.Response) APIError {
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
return APIError{
Response: resp,

View File

@@ -3,7 +3,7 @@ package client
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
)
@@ -27,7 +27,7 @@ func (t *handlerTransport) RoundTrip(req *http.Request) (*http.Response, error)
reqCopy.TransferEncoding = []string{"chunked"}
}
} else {
reqCopy.Body = ioutil.NopCloser(bytes.NewReader(nil))
reqCopy.Body = io.NopCloser(bytes.NewReader(nil))
}
if reqCopy.RequestURI == "" {
@@ -50,7 +50,7 @@ func (t *handlerTransport) RoundTrip(req *http.Request) (*http.Response, error)
}
if recorder.Body != nil {
resp.Body = ioutil.NopCloser(recorder.Body)
resp.Body = io.NopCloser(recorder.Body)
}
return &resp, nil

View File

@@ -67,25 +67,27 @@ func RateLimit(requestsPerSecond int) Option {
// and right after a response from the server is received.
//
// Example Output for request:
// [DBUG] 2022/03/01 21:54 Iris HTTP Client: POST / HTTP/1.1
// Host: 127.0.0.1:50948
// User-Agent: Go-http-client/1.1
// Content-Length: 22
// Accept: application/json
// Content-Type: application/json
// Accept-Encoding: gzip
//
// {"firstname":"Makis"}
// [DBUG] 2022/03/01 21:54 Iris HTTP Client: POST / HTTP/1.1
// Host: 127.0.0.1:50948
// User-Agent: Go-http-client/1.1
// Content-Length: 22
// Accept: application/json
// Content-Type: application/json
// Accept-Encoding: gzip
//
// {"firstname":"Makis"}
//
// Example Output for response:
// [DBUG] 2022/03/01 21:54 Iris HTTP Client: HTTP/1.1 200 OK
// Content-Length: 27
// Content-Type: application/json; charset=utf-8
// Date: Tue, 01 Mar 2022 19:54:03 GMT
//
// {
// "firstname": "Makis"
// }
// [DBUG] 2022/03/01 21:54 Iris HTTP Client: HTTP/1.1 200 OK
// Content-Length: 27
// Content-Type: application/json; charset=utf-8
// Date: Tue, 01 Mar 2022 19:54:03 GMT
//
// {
// "firstname": "Makis"
// }
func Debug(c *Client) {
handler := &debugRequestHandler{
logger: golog.Child("Iris HTTP Client: ").SetLevel("debug"),