Documentation ¶
Index ¶
- Constants
- Variables
- func Deserialize[E runtime.ErrorHandler, T any](body io.ReadCloser) (T, *runtime.Status)
- func Do(req *http.Request) (*http.Response, error)
- func DoClient(req *http.Request, client *http.Client) (*http.Response, error)
- func DoT[E runtime.ErrorHandler, T any, H Exchange](req *http.Request) (resp *http.Response, t T, status *runtime.Status)
- func GetContentLocation(req *http.Request) string
- func ReadAll(body io.ReadCloser) ([]byte, error)
- func WriteResponse(w http.ResponseWriter, data []byte, status *runtime.Status, headers ...string)
- type Default
- type Exchange
Examples ¶
Constants ¶
View Source
const ( ContentTypeText = "text/plain" // charset=utf-8 ContentTypeJson = "application/json" )
Variables ¶
View Source
var Client = http.DefaultClient
Functions ¶
func Deserialize ¶
func Deserialize[E runtime.ErrorHandler, T any](body io.ReadCloser) (T, *runtime.Status)
Deserialize - templated function, providing deserialization of a request/response body
Example ¶
result, status := Deserialize[runtime.DebugError, []byte](nil) fmt.Printf("test: Deserialize[DebugError,[]byte](nil) -> [%v] [status:%v]\n", string(result), status) resp := new(http.Response) result, status = Deserialize[runtime.DebugError, []byte](resp.Body) fmt.Printf("test: Deserialize[DebugError,[]byte](resp) -> [%v] [status:%v]\n", string(result), status) resp.Body = &httptest.ReaderCloser{Reader: strings.NewReader("Hello World String"), Err: nil} result, status = Deserialize[runtime.DebugError, []byte](resp.Body) fmt.Printf("test: Deserialize[DebugError,[]byte](resp) -> [%v] [status:%v]\n", string(result), status) resp.Body = &httptest.ReaderCloser{Reader: bytes.NewReader([]byte("Hello World []byte")), Err: nil} result2, status2 := Deserialize[runtime.DebugError, []byte](resp.Body) fmt.Printf("test: Deserialize[DebugError,[]byte](resp) -> [%v] [status:%v]\n", string(result2), status2)
Output: [[] github.com/idiomatic-go/motif/exchange/deserialize [body is nil]] test: Deserialize[DebugError,[]byte](nil) -> [] [status:Invalid Content] [[] github.com/idiomatic-go/motif/exchange/deserialize [body is nil]] test: Deserialize[DebugError,[]byte](resp) -> [] [status:Invalid Content] test: Deserialize[DebugError,[]byte](resp) -> [Hello World String] [status:OK] test: Deserialize[DebugError,[]byte](resp) -> [Hello World []byte] [status:OK]
func Do ¶
Do - process a "client.Do" request with the http.DefaultClient
Example ¶
req, _ := http.NewRequest("GET", "https://www.google.com/search?q=test", nil) resp, buf, status := DoT[runtime.DebugError, []byte, Default](req) fmt.Printf("test: Do[DebugError,[]byte,DefaultExchange](req) -> [status:%v] [buf:%v] [resp:%v]\n", status, len(buf) > 0, resp != nil)
Output: test: Do[DebugError,[]byte,DefaultExchange](req) -> [status:OK] [buf:true] [resp:true]
func DoClient ¶
DoClient - process a "client.Do" request with the given client. Also, check the req.Context to determine if there is an Exchange interface that should be called instead.
func GetContentLocation ¶
func ReadAll ¶
func ReadAll(body io.ReadCloser) ([]byte, error)
ReadAll - read all the body, with a deferred close
func WriteResponse ¶
WriteResponse - write a http.Response, utilizing the data, status, and headers for controlling the content
Types ¶
Source Files ¶
Click to show internal directories.
Click to hide internal directories.