Documentation ¶
Index ¶
- Constants
- Variables
- func Delete(ctx context.Context, url string, opts ...Option) error
- func Get(ctx context.Context, url string, opts ...Option) error
- func Patch(ctx context.Context, url string, opts ...Option) error
- func Path(elem ...interface{}) string
- func Post(ctx context.Context, url string, opts ...Option) error
- func Put(ctx context.Context, url string, opts ...Option) error
- type Client
- func (c *Client) Delete(ctx context.Context, url string, opts ...Option) error
- func (c *Client) Get(ctx context.Context, url string, opts ...Option) error
- func (c *Client) Patch(ctx context.Context, url string, opts ...Option) error
- func (c *Client) Post(ctx context.Context, url string, opts ...Option) error
- func (c *Client) Put(ctx context.Context, url string, opts ...Option) error
- func (c *Client) With(opts ...Option) *Client
- type Config
- type Option
- func Authorization(scheme, token string) Option
- func BaseURL(baseURL *url.URL) Option
- func BasicAuth(username, password string) Option
- func Bearer(token string) Option
- func Body(v interface{}) Option
- func CombineOptions(opts ...Option) Option
- func HTTPClient(cli *http.Client) Option
- func HandleRequest(f func(*http.Request) (*http.Request, error)) Option
- func HandleResponse(f func(*http.Response, error) (*http.Response, error)) Option
- func Header(k, v string) Option
- func JSON(v interface{}) Option
- func Query(k, v string) Option
- func Timeout(t time.Duration) Option
- func Transport(rt http.RoundTripper) Option
- func TransportFrom(f func(http.RoundTripper) http.RoundTripper) Option
- func TransportFunc(f func(*http.Request, http.RoundTripper) (*http.Response, error)) Option
- func URL(urlStr string) Option
- func UserAgent(ua string) Option
- func When(cond ResponseHandlerCond, rh ResponseHandler) Option
- func WhenClientError(h ResponseHandler) Option
- func WhenFailure(h ResponseHandler) Option
- func WhenServerError(h ResponseHandler) Option
- func WhenStatus(h ResponseHandler, codes ...int) Option
- func WhenSuccess(h ResponseHandler) Option
- type OptionFunc
- type RequestHandler
- type ResponseError
- type ResponseHandler
- type ResponseHandlerCond
Examples ¶
Constants ¶
View Source
const Version = "0.3.0"
Variables ¶
View Source
var ( DefaultUserAgent = fmt.Sprintf("hx/%s; %s", Version, runtime.Version()) DefaultOptions = []Option{ UserAgent(DefaultUserAgent), } )
Functions ¶
func Get ¶
Example ¶
package main import ( "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "github.com/izumin5210/hx" ) func main() { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { case r.Method == http.MethodGet && r.URL.Path == "/echo": err := json.NewEncoder(w).Encode(map[string]string{ "message": r.URL.Query().Get("message"), }) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } default: w.WriteHeader(http.StatusNotFound) } })) defer ts.Close() var out struct { Message string `json:"message"` } ctx := context.Background() err := hx.Get( ctx, ts.URL+"/echo", hx.Query("message", "It Works!"), hx.WhenSuccess(hx.AsJSON(&out)), hx.WhenFailure(hx.AsError()), ) if err != nil { // Handle errors... } fmt.Println(out.Message) }
Output: It Works!
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
type Config ¶
type Config struct { URL *url.URL Body io.Reader HTTPClient *http.Client QueryParams url.Values RequestHandlers []RequestHandler ResponseHandlers []ResponseHandler }
type Option ¶
func Authorization ¶
Authorization sets an authorization scheme and a token of an user.
func CombineOptions ¶
func HTTPClient ¶
HTTPClient sets a HTTP client that used to send HTTP request(s).
func HandleRequest ¶ added in v0.2.0
func HandleResponse ¶ added in v0.2.0
func Transport ¶
func Transport(rt http.RoundTripper) Option
Transport sets the round tripper to http.Client.
func TransportFrom ¶
func TransportFrom(f func(http.RoundTripper) http.RoundTripper) Option
TransportFrom sets the round tripper to http.Client.
func TransportFunc ¶ added in v0.2.0
func When ¶
func When(cond ResponseHandlerCond, rh ResponseHandler) Option
func WhenClientError ¶
func WhenClientError(h ResponseHandler) Option
func WhenFailure ¶
func WhenFailure(h ResponseHandler) Option
func WhenServerError ¶
func WhenServerError(h ResponseHandler) Option
func WhenStatus ¶
func WhenStatus(h ResponseHandler, codes ...int) Option
func WhenSuccess ¶
func WhenSuccess(h ResponseHandler) Option
type OptionFunc ¶
func (OptionFunc) ApplyOption ¶ added in v0.1.1
func (f OptionFunc) ApplyOption(c *Config) error
type ResponseError ¶
func (*ResponseError) Error ¶
func (e *ResponseError) Error() string
func (*ResponseError) Unwrap ¶
func (e *ResponseError) Unwrap() error
type ResponseHandler ¶
func AsError ¶
func AsError() ResponseHandler
func AsJSON ¶
func AsJSON(dst interface{}) ResponseHandler
func AsJSONError ¶ added in v0.2.0
func AsJSONError(dst error) ResponseHandler
AsJSONError is ResponseHandler that will populate an error with the JSON returned within the response body. And it will wrap the error with ResponseError and return it.
err := hx.Post(ctx, "https://example.com/posts", hx.JSON(body) hx.WhenSuccess(hx.AsJSON(&post), http.StatusBadRequest), hx.WhenStatus(hx.AsErrorOf(&InvalidArgument{}), http.StatusBadRequest), hx.WhenFailure(hx.AsError()), ) if err != nil { var ( invalidArgErr *InvalidArgument respErr *hx.ResponseError ) if errors.As(err, &invalidArgErr) { // handle known error } else if errors.As(err, &respErr) { // handle unknown response error } else { err := errors.Unwrap(err) // handle unknown error } }
type ResponseHandlerCond ¶
var ( IsSuccess ResponseHandlerCond = checkStatus(func(c int) bool { return c/100 == 2 }) IsFailure ResponseHandlerCond = Not(IsSuccess) IsClientError ResponseHandlerCond = checkStatus(func(c int) bool { return c/100 == 4 }) IsServerError ResponseHandlerCond = checkStatus(func(c int) bool { return c/100 == 5 }) IsTemporaryError ResponseHandlerCond = func(r *http.Response, err error) bool { terr, ok := err.(interface{ Temporary() bool }) return ok && terr.Temporary() } )
func Any ¶
func Any(conds ...ResponseHandlerCond) ResponseHandlerCond
func IsStatus ¶
func IsStatus(codes ...int) ResponseHandlerCond
func Not ¶
func Not(cond ResponseHandlerCond) ResponseHandlerCond
Source Files ¶
Click to show internal directories.
Click to hide internal directories.