exchange

package
v0.0.0-...-9a998da Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2023 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	ContentTypeText = "text/plain" // charset=utf-8
	ContentTypeJson = "application/json"
)

Variables

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

func Do(req *http.Request) (*http.Response, error)

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

func DoClient(req *http.Request, client *http.Client) (*http.Response, error)

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 DoT

func DoT[E runtime.ErrorHandler, T any, H Exchange](req *http.Request) (resp *http.Response, t T, status *runtime.Status)

func GetContentLocation

func GetContentLocation(req *http.Request) string

func ReadAll

func ReadAll(body io.ReadCloser) ([]byte, error)

ReadAll - read all the body, with a deferred close

func WriteResponse

func WriteResponse(w http.ResponseWriter, data []byte, status *runtime.Status, headers ...string)

WriteResponse - write a http.Response, utilizing the data, status, and headers for controlling the content

Types

type Default

type Default struct{}

func (Default) Do

func (Default) Do(req *http.Request) (*http.Response, error)

type Exchange

type Exchange interface {
	Do(req *http.Request) (*http.Response, error)
}

Exchange - interface for Http request/response interaction

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL