http_client

package
v0.0.0-...-ec24cc8 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

README

My generics experiment

Is it readable? Well... not satisfied : the same can be accomplished with closures and closures seems more readable to me.

Documentation

Index

Constants

View Source
const (
	DefaultTimeoutMillisecond = 30 * time.Second // default timeout in milliseconds
)

Variables

This section is empty.

Functions

func Add

func Add[T any](source []T, targets ...[]T) []T

func Copy

func Copy[T any](source []T) []T

func GeneralMultipartSerializer

func GeneralMultipartSerializer(form *MultipartForm) (io.Reader, string, error)

GeneralMultipartSerializer Default Multipart Body serializer

func IsNil

func IsNil(source any) bool

func JSONBodyDeserializer

func JSONBodyDeserializer(body []byte, target any) (any, error)

JSONBodyDeserializer Default JSON Body deserializer

func JSONBodySerializer

func JSONBodySerializer(body any) (io.Reader, error)

JSONBodySerializer Default JSON Body serializer

func Kind

func Kind(source any) reflect.Kind

func SliceToMap

func SliceToMap[T comparable, R any](defaultValue R, source ...T) map[T]R

func Sub

func Sub[T comparable](source, target []T) []T

Types

type APIResponse

type APIResponse[R any] struct {
	ReqRespErr
	Response *R
}

APIResponse Response with Error & Type

type BodyDeserializer

type BodyDeserializer func(body []byte, target any) (any, error)

BodyDeserializer Deserialize the body (for response)

type BodySerializer

type BodySerializer func(body any) (io.Reader, error)

BodySerializer Serialize the body (for put/post/patch etc)

type ClientWMiddlewares

type ClientWMiddlewares struct {
	TimeoutMillisecond int64
	// contains filtered or unexported fields
}

func NewClient

func NewClient() *ClientWMiddlewares

func NewClientWithMiddlewares

func NewClientWithMiddlewares(client *http.Client, mwares ...*Interceptor) *ClientWMiddlewares

func (*ClientWMiddlewares) AddMiddlewares

func (s *ClientWMiddlewares) AddMiddlewares(mwares ...*Interceptor)

func (*ClientWMiddlewares) ClearMiddlewares

func (s *ClientWMiddlewares) ClearMiddlewares()

func (*ClientWMiddlewares) ContextTimeout

func (s *ClientWMiddlewares) ContextTimeout() (context.Context, context.CancelFunc)

func (*ClientWMiddlewares) Delete

func (s *ClientWMiddlewares) Delete(targetURL string) *ReqRespErr

Delete HTTP Method Delete

func (*ClientWMiddlewares) DoNewRequest

func (s *ClientWMiddlewares) DoNewRequest(ctx context.Context, header http.Header, method string, targetURL string) *ReqRespErr

DoNewRequest

func (*ClientWMiddlewares) DoNewRequestWithBodyOptions

func (s *ClientWMiddlewares) DoNewRequestWithBodyOptions(ctx context.Context, header http.Header, method string, targetURL string, body io.Reader, contentType string) *ReqRespErr

DoNewRequestWithBodyOptions

func (*ClientWMiddlewares) DoRequest

func (s *ClientWMiddlewares) DoRequest(req *http.Request) *ReqRespErr

DoRequest

func (*ClientWMiddlewares) ForgetMiddlewares

func (s *ClientWMiddlewares) ForgetMiddlewares(mwares ...*Interceptor)

func (*ClientWMiddlewares) Get

func (s *ClientWMiddlewares) Get(targetURL string) *ReqRespErr

Get HTTP Method Get

func (*ClientWMiddlewares) HTTPClient

func (s *ClientWMiddlewares) HTTPClient() *http.Client

func (*ClientWMiddlewares) Head

func (s *ClientWMiddlewares) Head(targetURL string) *ReqRespErr

Head HTTP Method Head

func (*ClientWMiddlewares) Options

func (s *ClientWMiddlewares) Options(targetURL string) *ReqRespErr

Options HTTP Method Options

func (*ClientWMiddlewares) Patch

func (s *ClientWMiddlewares) Patch(targetURL, contentType string, body io.Reader) *ReqRespErr

Patch HTTP Method Patch

func (*ClientWMiddlewares) Post

func (s *ClientWMiddlewares) Post(targetURL, contentType string, body io.Reader) *ReqRespErr

Post HTTP Method Post

func (*ClientWMiddlewares) Put

func (s *ClientWMiddlewares) Put(targetURL, contentType string, body io.Reader) *ReqRespErr

Put HTTP Method Put

func (*ClientWMiddlewares) RoundTrip

func (s *ClientWMiddlewares) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip interface implementation

func (*ClientWMiddlewares) SetHTTPClient

func (s *ClientWMiddlewares) SetHTTPClient(client *http.Client)

type GenericCallbackWrapper

type GenericCallbackWrapper[R any] struct {
	Callback func() R
}

func NewCallbackWrapper

func NewCallbackWrapper[R any](callback func() R) *GenericCallbackWrapper[R]

type GenericHTTPClient

type GenericHTTPClient struct {
	DefaultHeader     http.Header
	Serializer        MultipartSerializer
	SerializerForJSON BodySerializer
	Deserializer      BodyDeserializer
	BaseURL           string
	// contains filtered or unexported fields
}

func NewGenericHTTPClient

func NewGenericHTTPClient(baseURL string) *GenericHTTPClient

NewGenericHTTPClient New a NewGenericHTTPClient instance

func NewGenericHTTPClientWithMiddlewares

func NewGenericHTTPClientWithMiddlewares(baseURL string, simpleHTTP *ClientWMiddlewares) *GenericHTTPClient

NewGenericHTTPClientWithMiddlewares a GenericHTTPClient instance with a SimpleHTTP

func (*GenericHTTPClient) GetSimpleHTTP

func (w *GenericHTTPClient) GetSimpleHTTP() *ClientWMiddlewares

type GenericSlice

type GenericSlice[T comparable] []T

func GenericSliceFromSlice

func GenericSliceFromSlice[T comparable](source []T) *GenericSlice[T]

func (*GenericSlice[E]) Append

func (s *GenericSlice[E]) Append(item ...E) *GenericSlice[E]

func (*GenericSlice[E]) Concat

func (s *GenericSlice[E]) Concat(slices ...[]E) *GenericSlice[E]

func (*GenericSlice[E]) Len

func (s *GenericSlice[E]) Len() int

func (*GenericSlice[E]) RemoveItem

func (s *GenericSlice[E]) RemoveItem(input ...E) *GenericSlice[E]

func (*GenericSlice[T]) ToArray

func (s *GenericSlice[T]) ToArray() []T

type Interceptor

type Interceptor func(req *http.Request) error

type MultipartForm

type MultipartForm struct {
	Value map[string][]string
	// File The absolute paths of files
	File map[string][]string
}

MultipartForm Path params for API usages

type MultipartSerializer

type MultipartSerializer func(body *MultipartForm) (io.Reader, string, error)

MultipartSerializer Serialize the multipart body (for put/post/patch etc)

type PathParam

type PathParam map[string]any

PathParam Path params for API usages

type ReqRespErr

type ReqRespErr struct {
	Req  *http.Request
	Resp *http.Response
	Err  error
}

type WithBody

type WithBody[T any, R any] func(pathParam PathParam, body T, target *R) *GenericCallbackWrapper[*APIResponse[R]]

WithBody API with request body options

func PATCH

func PATCH[T any, R any](client *GenericHTTPClient, url string) WithBody[T, R]

func POST

func POST[T any, R any](client *GenericHTTPClient, url string) WithBody[T, R]

func PUT

func PUT[T any, R any](client *GenericHTTPClient, url string) WithBody[T, R]

func WithJSONSerializer

func WithJSONSerializer[T any, R any](client *GenericHTTPClient, method string, relativeURL string, contentType string, bodySerializer BodySerializer) WithBody[T, R]

type WithMultipart

type WithMultipart[R any] func(pathParam PathParam, body *MultipartForm, target *R) *GenericCallbackWrapper[*APIResponse[R]]

WithMultipart API with request body options

func PATCHMultipart

func PATCHMultipart[R any](client *GenericHTTPClient, url string) WithMultipart[R]

func POSTMultipart

func POSTMultipart[R any](client *GenericHTTPClient, url string) WithMultipart[R]

func PUTMultipart

func PUTMultipart[R any](client *GenericHTTPClient, url string) WithMultipart[R]

func WithMultipartSerializer

func WithMultipartSerializer[R any](client *GenericHTTPClient, method string, relativeURL string, multipartSerializer MultipartSerializer) WithMultipart[R]

type WithoutBody

type WithoutBody[R any] func(pathParam PathParam, target *R) *GenericCallbackWrapper[*APIResponse[R]]

WithoutBody API without request body options

func DELETE

func DELETE[R any](client *GenericHTTPClient, url string) WithoutBody[R]

func DoNewRequest

func DoNewRequest[R any](client *GenericHTTPClient, method string, relativeURL string) WithoutBody[R]

func GET

func GET[R any](client *GenericHTTPClient, url string) WithoutBody[R]

Jump to

Keyboard shortcuts

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