exchange

package
v0.0.0-...-58692e4 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: BSD-3-Clause Imports: 9 Imported by: 7

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client() *http.Client
Example
cp := Client()
if cp != nil {
	cp = nil
}
fmt.Printf("test: Client()  [!nil:%v]\n", Client() != nil)
Output:

test: Client()  [!nil:true]

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/gotemplates/core/exchange/deserialize [body is nil]]
test: Deserialize[DebugError,[]byte](nil) -> [] [status:Invalid Content]
[[] github.com/gotemplates/core/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[E runtime.ErrorHandler, H HttpExchange, T any](req *http.Request) (resp *http.Response, t T, status *runtime.Status)
Example
req, _ := http.NewRequest("GET", "https://www.google.com/search?q=test", nil)
resp, buf, status := Do[runtime.DebugError, Default, []byte](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 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 HttpExchange

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

HttpExchange - 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