httphelper

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 8 Imported by: 5

README

httphelper

Go Reference

Helpers for Go's http package.

Documentation

Overview

Package httphelper contains http package helpers.

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyResponseBody = errors.New("empty response body")

Functions

func AuthBasic added in v0.6.0

func AuthBasic(userid, password string) string

AuthBasic returns Basic authorization value.

func ErrorOptionWithMessage added in v0.12.0

func ErrorOptionWithMessage() func(o *ErrorOptions)

ErrorOptionWithMessage turns on Error's Message string deserialization from HTTP response body.

func ErrorOptionWithObject added in v0.5.0

func ErrorOptionWithObject() func(o *ErrorOptions)

ErrorOptionWithObject turns on Error's Object JSON deserialization from HTTP response body.

func IsNotStatusOK added in v0.12.0

func IsNotStatusOK(rs *http.Response) bool

IsNotStatusOK determines whether r.StatusCode is not http.StatusOK.

func JsonBody added in v0.10.0

func JsonBody(in any) (io.ReadCloser, error)

JsonBody creates http Body containing JSON-encoded 'in'. If 'in' is nil, http.NoBody is returned.

func JsonReader added in v0.15.0

func JsonReader(in any) (*bytes.Reader, error)

JsonReader creates bytes.Reader containing JSON-encoded 'in' to use as body with http.NewRequest or http.NewRequestWithContext. If 'in' is nil, nil is returned.

func ReqBody added in v0.14.0

func ReqBody[E any](client *http.Client, rq *http.Request, isError func(*http.Response) bool) ([]byte, error)

ReqBody sends an http.Request by http.Client and returns the contents of the response body.

If 'isError' is not nil and returns 'true', httphelper.Error is returned. httphelper.Error.Object of type 'E' is JSON-decoded from the response body. If response body does not contain JSON-encoded 'E', but contains a string, this string is returned in httphelper.Error.Message field.

Pass generichelper.NoType as 'E' to omit processing of httphelper.Error.Object.

func ReqJson added in v0.14.0

func ReqJson[O, E any](client *http.Client, rq *http.Request, isError func(*http.Response) bool) (*O, error)

ReqJson sends an http.Request by http.Client and returns output object of type 'O' passed JSON-encoded as the response body.

If 'isError' is not nil and returns 'true', httphelper.Error is returned. httphelper.Error.Object of type 'E' is JSON-decoded from the response body. If response body does not contain JSON-encoded 'E', but contains a string, this string is returned in httphelper.Error.Message field.

Pass generichelper.NoType as corresponding type argument to omit processing of either object.

Types

type Error added in v0.5.0

type Error[T any] struct {
	StatusCode int    `json:"status_code"`
	Status     string `json:"status"`
	Object     T      `json:"object,omitempty"`
	Message    string `json:"message,omitempty"`
}

Error represents a HTTP error. Object (if turned on in options) is deserialized from JSON read from HTTP response body (if any).

func NewError added in v0.5.0

func NewError[T any](rs *http.Response, opts ...func(o *ErrorOptions)) (*Error[T], error)

NewError creates Error from http.Response.

func (*Error[T]) Error added in v0.5.0

func (e *Error[T]) Error() string

Error implements the error interface.

type ErrorOptions added in v0.5.0

type ErrorOptions struct {
	// contains filtered or unexported fields
}

ErrorOptions contains Error options.

Jump to

Keyboard shortcuts

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