mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 09:57:01 +00:00
replace ioutil with io package and other minor improvements
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -56,11 +56,12 @@ var errorCodeMap = make(map[ErrorCodeName]ErrorCode)
|
||||
// See "RegisterErrorCode" and "RegisterErrorCodeMap" for alternatives.
|
||||
//
|
||||
// Example:
|
||||
// var (
|
||||
// NotFound = errors.E("NOT_FOUND", http.StatusNotFound)
|
||||
// )
|
||||
// ...
|
||||
// NotFound.Details(ctx, "resource not found", "user with id: %q was not found", userID)
|
||||
//
|
||||
// var (
|
||||
// NotFound = errors.E("NOT_FOUND", http.StatusNotFound)
|
||||
// )
|
||||
// ...
|
||||
// NotFound.Details(ctx, "resource not found", "user with id: %q was not found", userID)
|
||||
//
|
||||
// This method MUST be called on initialization, before HTTP server starts as
|
||||
// the internal map is not protected by mutex.
|
||||
@@ -238,7 +239,8 @@ var (
|
||||
// Error represents the JSON form of "http wire errors".
|
||||
//
|
||||
// Examples can be found at:
|
||||
// https://github.com/kataras/iris/tree/master/_examples/routing/http-wire-errors.
|
||||
//
|
||||
// https://github.com/kataras/iris/tree/master/_examples/routing/http-wire-errors.
|
||||
type Error struct {
|
||||
ErrorCode ErrorCode `json:"http_error_code" yaml:"HTTPErrorCode"`
|
||||
Message string `json:"message,omitempty" yaml:"Message"`
|
||||
|
||||
@@ -13,7 +13,8 @@ import (
|
||||
// A validation error(s) can be given by ErrorCodeName's Validation or Err methods.
|
||||
//
|
||||
// Example can be found at:
|
||||
// https://github.com/kataras/iris/tree/master/_examples/routing/http-wire-errors/custom-validation-errors
|
||||
//
|
||||
// https://github.com/kataras/iris/tree/master/_examples/routing/http-wire-errors/custom-validation-errors
|
||||
type ValidationError interface {
|
||||
error
|
||||
|
||||
|
||||
@@ -114,43 +114,43 @@ type List[T any] struct {
|
||||
//
|
||||
// Example Code:
|
||||
//
|
||||
// import "github.com/kataras/iris/v12/x/pagination"
|
||||
// ...more code
|
||||
// import "github.com/kataras/iris/v12/x/pagination"
|
||||
// ...more code
|
||||
//
|
||||
// type User struct {
|
||||
// Firstname string `json:"firstname"`
|
||||
// Lastname string `json:"lastname"`
|
||||
// }
|
||||
// type User struct {
|
||||
// Firstname string `json:"firstname"`
|
||||
// Lastname string `json:"lastname"`
|
||||
// }
|
||||
//
|
||||
// type ExtraUser struct {
|
||||
// User
|
||||
// ExtraData string
|
||||
// }
|
||||
// type ExtraUser struct {
|
||||
// User
|
||||
// ExtraData string
|
||||
// }
|
||||
//
|
||||
// func main() {
|
||||
// users := []User{
|
||||
// {"Gerasimos", "Maropoulos"},
|
||||
// {"Efi", "Kwfidou"},
|
||||
// }
|
||||
// func main() {
|
||||
// users := []User{
|
||||
// {"Gerasimos", "Maropoulos"},
|
||||
// {"Efi", "Kwfidou"},
|
||||
// }
|
||||
//
|
||||
// t := pagination.NewList(users, 100, nil, pagination.ListOptions{
|
||||
// Page: 1,
|
||||
// Size: 50,
|
||||
// })
|
||||
// t := pagination.NewList(users, 100, nil, pagination.ListOptions{
|
||||
// Page: 1,
|
||||
// Size: 50,
|
||||
// })
|
||||
//
|
||||
// // Optionally, transform a T list of objects to a V list of objects.
|
||||
// v, err := pagination.TransformList(t, func(u User) (ExtraUser, error) {
|
||||
// return ExtraUser{
|
||||
// User: u,
|
||||
// ExtraData: "test extra data",
|
||||
// }, nil
|
||||
// })
|
||||
// if err != nil { panic(err) }
|
||||
// // Optionally, transform a T list of objects to a V list of objects.
|
||||
// v, err := pagination.TransformList(t, func(u User) (ExtraUser, error) {
|
||||
// return ExtraUser{
|
||||
// User: u,
|
||||
// ExtraData: "test extra data",
|
||||
// }, nil
|
||||
// })
|
||||
// if err != nil { panic(err) }
|
||||
//
|
||||
// paginationJSON, err := json.MarshalIndent(v, "", " ")
|
||||
// if err!=nil { panic(err) }
|
||||
// fmt.Println(paginationJSON)
|
||||
// }
|
||||
// paginationJSON, err := json.MarshalIndent(v, "", " ")
|
||||
// if err!=nil { panic(err) }
|
||||
// fmt.Println(paginationJSON)
|
||||
// }
|
||||
func NewList[T any](items []T, totalCount int64, filter any, opts ListOptions) *List[T] {
|
||||
pageSize := opts.GetLimit()
|
||||
|
||||
@@ -199,13 +199,13 @@ func NewList[T any](items []T, totalCount int64, filter any, opts ListOptions) *
|
||||
//
|
||||
// Example Code:
|
||||
//
|
||||
// listOfUsers := pagination.NewList(...)
|
||||
// newListOfExtraUsers, err := pagination.TransformList(listOfUsers, func(u User) (ExtraUser, error) {
|
||||
// return ExtraUser{
|
||||
// User: u,
|
||||
// ExtraData: "test extra data",
|
||||
// }, nil
|
||||
// })
|
||||
// listOfUsers := pagination.NewList(...)
|
||||
// newListOfExtraUsers, err := pagination.TransformList(listOfUsers, func(u User) (ExtraUser, error) {
|
||||
// return ExtraUser{
|
||||
// User: u,
|
||||
// ExtraData: "test extra data",
|
||||
// }, nil
|
||||
// })
|
||||
func TransformList[T any, V any](list *List[T], transform func(T) (V, error)) (*List[V], error) {
|
||||
if list == nil {
|
||||
return &List[V]{
|
||||
|
||||
Reference in New Issue
Block a user