Documentation ¶
Index ¶
- Constants
- Variables
- func AddHeader(h http.Header, name, value string) http.Header
- func Content[T any](body io.Reader) (t T, status *core.Status)
- func CreateAcceptEncodingHeader() http.Header
- func DeadlineExceededError(t any) bool
- func Do(req *http.Request) (resp *http.Response, status *core.Status)
- func Exchange(req *http.Request) (*http.Response, *core.Status)
- func Forward(dest http.Header, src http.Header, names ...string) http.Header
- func GetContentType(headers any) string
- func MultiExchange(reqs []RequestItem, handler OnResponse)
- func NewHealthResponseOK() *http.Response
- func NewNotFoundResponse() *http.Response
- func NewResponse(statusCode int, h http.Header, content any) (resp *http.Response, status *core.Status)
- func NewResponseFromUri(uri any) (*http.Response, *core.Status)
- func RegisterExchange(domain string, handler core.HttpExchange) error
- func SetHeader(h http.Header, name, value string) http.Header
- func SetHeaders(w http.ResponseWriter, headers any)
- func WriteResponse(w http.ResponseWriter, headers any, statusCode int, content any, ...) (contentLength int64)
- type Attr
- type OnResponse
- type RequestItem
- type ResponseWriter
- func (w *ResponseWriter) Body() []byte
- func (w *ResponseWriter) Header() http.Header
- func (w *ResponseWriter) Response() *http.Response
- func (w *ResponseWriter) SetStatusCode(code int)
- func (w *ResponseWriter) StatusCode() int
- func (w *ResponseWriter) Write(p []byte) (int, error)
- func (w *ResponseWriter) WriteHeader(statusCode int)
- func (w *ResponseWriter) Written() int64
Examples ¶
Constants ¶
const ( ContentTypeJson = "application/json" ContentType = "Content-Type" ContentEncoding = "Content-Encoding" AcceptEncoding = "Accept-Encoding" AcceptEncodingValue = "gzip, deflate, br" ContentLength = "Content-Length" ContentEncodingGzip = "gzip" ContentTypeTextHtml = "text/html" ContentTypeText = "text/plain charset=utf-8" ContentLocation = "Content-Location" ExchangeOverride = "X-Exchange-Override" ContentResolver = "X-Content-Resolver" ResolverSeparator = "->" )
const (
PkgPath = "github/behavioral-ai/core/httpx"
)
Variables ¶
var (
Client = http.DefaultClient
)
Functions ¶
func AddHeader ¶
Example ¶
h := AddHeader(nil, ContentLocation, "https://www.google.com/search") fmt.Printf("test: AddHeader() -> [h:%v]\n", h) h = AddHeader(h, ContentLocation, "https://www.google.com/search") fmt.Printf("test: AddHeader() -> [h:%v]\n", h)
Output: test: AddHeader() -> [h:map[Content-Location:[https://www.google.com/search]]] test: AddHeader() -> [h:map[Content-Location:[https://www.google.com/search https://www.google.com/search]]]
func DeadlineExceededError ¶
func GetContentType ¶
GetContentType - return the content type header value
func MultiExchange ¶
func MultiExchange(reqs []RequestItem, handler OnResponse)
Example ¶
package main import ( "fmt" "github.com/behavioral-ai/core/core" "net/http" ) type exchangeResult struct { Failure bool Resp *http.Response Status *core.Status } var results []exchangeResult func onResponse(id string, resp *http.Response, status *core.Status) { //fmt.Printf("[req:%v]\n [resp:%v]\n [status:%v]\n", resp.Request, resp, status) fmt.Printf("[id:%v] [status:%v]\n", id, status) results = append(results, exchangeResult{ Failure: false, Resp: resp, Status: status, }) //return true } func main() { var reqs []RequestItem r, _ := http.NewRequest("", "https://www.google.com/search?q=golang", nil) reqs = append(reqs, RequestItem{Id: "1", Request: r}) r, _ = http.NewRequest("", "https://www.search.yahoo.com/search?q=golang", nil) reqs = append(reqs, RequestItem{Id: "2", Request: r}) r, _ = http.NewRequest("", "https://www.bing.com/search?q=golang", nil) reqs = append(reqs, RequestItem{Id: "3", Request: r}) r, _ = http.NewRequest("", "https://www.duckduckgo.com/search?q=golang", nil) reqs = append(reqs, RequestItem{Id: "4", Request: r}) MultiExchange(reqs, onResponse) fmt.Printf("test: MultiExchange() -> [count:%v]\n", len(results)) } /* func ExampleMultiExchangeFailure() { var reqs []*http.Request r, _ := http.NewRequest("", "http://localhost:8080/search?q=golang", nil) reqs = append(reqs, r) r, _ = http.NewRequest("", "https://www.search.yahoo.com/search?q=golang", nil) reqs = append(reqs, r) r, _ = http.NewRequest("", "https://www.bing.com/search?q=golang", nil) reqs = append(reqs, r) r, _ = http.NewRequest("", "https://www.duckduckgo.com/search?q=golang", nil) reqs = append(reqs, r) results, status := MultiExchange(reqs, onResponse) fmt.Printf("test: ExampleMultiExchange() -> [count:%v] [%v]\n", len(results), status) //Output: //[status:Internal Error [Get "http://localhost:8080/search?q=golang": dial tcp [::1]:8080: connectex: No connection could be made because the target machine actively refused it.]] //[status:OK] //[status:OK] //[status:OK] //test: ExampleMultiExchange() -> [count:4] [Execution Error [error: request failures]] } */
Output: [id:1] [status:OK] [id:3] [status:OK] [id:4] [status:OK] [id:2] [status:OK] test: MultiExchange() -> [count:4]
func NewHealthResponseOK ¶
Example ¶
status := "\"status\": \"up\"" resp := NewHealthResponseOK() buf, _ := iox.ReadAll(resp.Body, nil) body := string(buf) fmt.Printf("test: NewHealthResponseOK() -> [status-code:%v] [content:%v]\n", resp.StatusCode, strings.Contains(body, status))
Output: test: NewHealthResponseOK() -> [status-code:200] [content:true]
func NewNotFoundResponse ¶
func NewResponse ¶
func NewResponse(statusCode int, h http.Header, content any) (resp *http.Response, status *core.Status)
Example ¶
resp, _ := NewResponse(http.StatusOK, nil, nil) fmt.Printf("test: NewResponse() -> [status-code:%v]\n", resp.StatusCode) resp, _ = NewResponse(core.StatusOK().HttpCode(), nil, "version 1.2.35") buf, _ := iox.ReadAll(resp.Body, nil) fmt.Printf("test: NewResponse() -> [status-code:%v] [content:%v]\n", resp.StatusCode, string(buf))
Output: test: NewResponse() -> [status-code:200] test: NewResponse() -> [status-code:200] [content:version 1.2.35]
func NewResponseFromUri ¶
NewResponseFromUri - read a Http response given a URL
func RegisterExchange ¶
func RegisterExchange(domain string, handler core.HttpExchange) error
RegisterExchange - add a domain and Http Exchange handler to the proxy
func SetHeaders ¶
func SetHeaders(w http.ResponseWriter, headers any)
SetHeaders - set the headers into an HTTP response writer
func WriteResponse ¶
func WriteResponse(w http.ResponseWriter, headers any, statusCode int, content any, reqHeader http.Header) (contentLength int64)
WriteResponse - write a http.Response, utilizing the content, status code, and headers Content types supported: []byte, string, error, io.Reader, io.ReadCloser. Other types will be treated as JSON and serialized, if the headers content type is JSON. If not JSON, then an error will be raised.
Types ¶
type RequestItem ¶
type ResponseWriter ¶
type ResponseWriter struct {
// contains filtered or unexported fields
}
ResponseWriter - write a response
func NewResponseWriter ¶
func NewResponseWriter() *ResponseWriter
NewResponseWriter - create a new response writer
func (*ResponseWriter) Body ¶
func (w *ResponseWriter) Body() []byte
Body - return the response body
func (*ResponseWriter) Header ¶
func (w *ResponseWriter) Header() http.Header
Header - return the response http.Header
func (*ResponseWriter) Response ¶
func (w *ResponseWriter) Response() *http.Response
Response - return the response
func (*ResponseWriter) SetStatusCode ¶
func (w *ResponseWriter) SetStatusCode(code int)
SetStatusCode - return the response status code
func (*ResponseWriter) StatusCode ¶
func (w *ResponseWriter) StatusCode() int
StatusCode - return the response status code
func (*ResponseWriter) Write ¶
func (w *ResponseWriter) Write(p []byte) (int, error)
Write - write the response body
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(statusCode int)
WriteHeader - write the response status code
func (*ResponseWriter) Written ¶
func (w *ResponseWriter) Written() int64
Written - return bytes written