exchange

package
v0.0.0-...-755224a Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: BSD-3-Clause Imports: 10 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	SchemeName = "scheme"
	HostName   = "host"
	PathName   = "path"
	QueryName  = "query"
	MethodName = "method"
)

Variables

View Source
var PkgUrl = reflect.TypeOf(any(pkg{})).PkgPath()

Functions

func AddHeaders

func AddHeaders(req *http.Request, header http.Header)

func CreateHeaders

func CreateHeaders(h http.Header, resp *http.Response, keys ...string)

func Deserialize

func Deserialize[E runtime.ErrorHandler, T any](ctx 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, 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](nil, 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](nil, 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](nil, resp.Body)
fmt.Printf("test: Deserialize[DebugError,[]byte](resp) -> [%v] [status:%v]\n", string(result2), status2)
Output:

[[] github.com/go-sre/core/exchange/deserialize [body is nil]]
test: Deserialize[DebugError,[]byte](nil) -> [] [status:Invalid Content]
[[] github.com/go-sre/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 LookupRequest

func LookupRequest(name string, req *http.Request) (string, error)
Example
uri := "http://localhost:8080/base-path/resource?first=false"
req, _ := http.NewRequest("", uri, nil)

name := ""

s, err := LookupRequest("", nil)
fmt.Printf("test: LookupRequest(%v) -> [err:%v] [%v]\n", name, err, s)

s, err = LookupRequest("test", req)
fmt.Printf("test: LookupRequest(%v) -> [err:%v] [%v]\n", name, err, s)

s, err = LookupRequest("method", req)
fmt.Printf("test: LookupRequest(%v) -> [err:%v] [%v]\n", name, err, s)

s, err = LookupRequest("scheme", req)
fmt.Printf("test: LookupRequest(%v) -> [err:%v] [%v]\n", name, err, s)

s, err = LookupRequest("host", req)
fmt.Printf("test: LookupRequest(%v) -> [err:%v] [%v]\n", name, err, s)

s, err = LookupRequest("path", req)
fmt.Printf("test: LookupRequest(%v) -> [err:%v] [%v]\n", name, err, s)

s, err = LookupRequest("query", req)
fmt.Printf("test: LookupRequest(%v) -> [err:%v] [%v]\n", name, err, s)

os.Setenv("RUNTIME", "DEV")
s, err = LookupRequest("$RUNTIME", req)
fmt.Printf("test: LookupRequest(%v) -> [err:%v] [%v]\n", name, err, s)
Output:

test: LookupRequest() -> [err:invalid argument: Request is nil] []
test: LookupRequest() -> [err:invalid argument : LookupEnv() template variable is invalid: test] []
test: LookupRequest() -> [err:<nil>] [GET]
test: LookupRequest() -> [err:<nil>] [http]
test: LookupRequest() -> [err:<nil>] [localhost:8080]
test: LookupRequest() -> [err:<nil>] [/base-path/resource]
test: LookupRequest() -> [err:<nil>] [first=false]
test: LookupRequest() -> [err:<nil>] [DEV]

func ReadAll

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

ReadAll - read all the body, with a deferred close

func ReadCookies

func ReadCookies(req *http.Request) map[string]*http.Cookie

func WriteResponse

func WriteResponse[E runtime.ErrorHandler](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

func WriteResponseCopy

func WriteResponseCopy[E runtime.ErrorHandler](w http.ResponseWriter, resp *http.Response, headers ...string)

WriteResponseCopy - write a http.Response, utilizing the data, status, and response for controlling the content

Example
w := httptest.NewRecorder()

resp := http.Response{Header: http.Header{}}
resp.StatusCode = 504
resp.Header.Add("key", "value")
resp.Header.Add("key1", "value1")
resp.Header.Add("key2", "value2")

resp.Body = ioutil.NopCloser(bytes.NewReader([]byte("error content")))
WriteResponseCopy[runtime.DebugError](w, &resp, "key")
fmt.Printf("test: WriteResponse(w,resp,status) -> [status:%v] [body:%v] [header:%v]\n", w.Code, w.Body.String(), w.Result().Header)

resp.Body = ioutil.NopCloser(bytes.NewReader([]byte("foo")))
w = httptest.NewRecorder()
resp.StatusCode = 200
WriteResponseCopy[runtime.DebugError](w, &resp, "*")
fmt.Printf("test: WriteResponse(w,resp,status) -> [status:%v] [body:%v] [header:%v]\n", w.Code, w.Body.String(), w.Result().Header)
Output:

test: WriteResponse(w,resp,status) -> [status:504] [body:error content] [header:map[Key:[value]]]
test: WriteResponse(w,resp,status) -> [status:200] [body:foo] [header:map[Key:[value] Key1:[value1] Key2:[value2]]]

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