httpx

package
v0.0.0-...-d0ebcda Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2024 License: BSD-3-Clause Imports: 16 Imported by: 6

Documentation

Index

Examples

Constants

View Source
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   = "->"
)
View Source
const (
	PkgPath = "github/behavioral-ai/core/httpx"
)

Variables

View Source
var (
	Client = http.DefaultClient
)

Functions

func AddHeader

func AddHeader(h http.Header, name, value string) http.Header
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 Content

func Content[T any](body io.Reader) (t T, status *core.Status)

func CreateAcceptEncodingHeader

func CreateAcceptEncodingHeader() http.Header

func DeadlineExceededError

func DeadlineExceededError(t any) bool

func Do

func Do(req *http.Request) (resp *http.Response, status *core.Status)

Do - process an HTTP request, checking for file:// scheme

func Exchange

func Exchange(req *http.Request) (*http.Response, *core.Status)

Exchange - process an HTTP call utilizing an Exchange

func Forward

func Forward(dest http.Header, src http.Header, names ...string) http.Header

Forward - forward headers

func GetContentType

func GetContentType(headers any) string

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

func NewHealthResponseOK() *http.Response
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 NewNotFoundResponse() *http.Response

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

func NewResponseFromUri(uri any) (*http.Response, *core.Status)

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 SetHeader

func SetHeader(h http.Header, name, value string) http.Header

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 Attr

type Attr struct {
	Key   string
	Value string
}

type OnResponse

type OnResponse func(id string, resp *http.Response, status *core.Status)

type RequestItem

type RequestItem struct {
	Id      string
	Request *http.Request
}

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

Jump to

Keyboard shortcuts

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