goreq

package module
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: BSD-2-Clause Imports: 14 Imported by: 1

README

goreq

Is a http client wrapper

Features:

  • logging request in CURL format
  • logging request time
  • repeat request via repeat strategy
  • has repeat timeout
  • validate request before call
  • custom response error strategy
  • parallel paginator
If you find this project useful or want to support the author, you can send tokens to any of these wallets
  • Bitcoin: bc1qgx5c3n7q26qv0tngculjz0g78u6mzavy2vg3tf
  • Ethereum: 0x62812cb089E0df31347ca32A1610019537bbFe0D
  • Dogecoin: DET7fbNzZftp4sGRrBehfVRoi97RiPKajV

Documentation

Index

Constants

View Source
const DefaultTimeout = 30

DefaultTimeout Default request timeout

Variables

This section is empty.

Functions

func BuildCURL

func BuildCURL(request HttpRequest) string

BuildCURL Build curl for logging

func Ensure

func Ensure(request HttpRequest) (*http.Response, []byte, error)

Ensure request

func ParallelPaginatorJsonEnsure added in v1.3.0

func ParallelPaginatorJsonEnsure[F any, R any](form F, hr HttpRequest) (items []R, meta gorest.Meta, e porterr.IError)

ParallelPaginatorJsonEnsure Execute api call that can have async count of parallel request

func SecureClient added in v1.3.5

func SecureClient(certPath string) (*http.Client, porterr.IError)

SecureClient Init secure client

Types

type HttpRequest

type HttpRequest struct {
	//Host service label. For messages
	Label string
	//Default Http client
	Client *http.Client
	//Service host
	Host string
	//Http method. GET, POST, PUT etc
	Method string
	//Remote endpoint
	Url string
	//Http headers
	Headers http.Header
	//Http body
	Body []byte
	//Count of retry attempts
	RetryCount uint
	//Retry timeout. Default 30s
	RetryTimeout time.Duration
	//Retry strategy callback
	RetryStrategy func(response *http.Response) bool
	//Response error
	ResponseErrorStrategy func(response *http.Response) error
	//Logger. Implements RequestLogger
	Logger Logger
	//How many body bytes must be logged
	//0 - all body will be logged
	LogBodySize int
}

HttpRequest Each request performs via struct bellow

func (HttpRequest) EnsureJSON

func (r HttpRequest) EnsureJSON(method string, url string, header http.Header, body interface{}, dto interface{}) (*http.Response, error)

EnsureJSON ensure JSON request

func (*HttpRequest) InitDefaultLogger added in v1.2.0

func (r *HttpRequest) InitDefaultLogger()

InitDefaultLogger Init default logger

type IPaginator added in v1.3.0

type IPaginator interface {
	// GetPage get current page
	GetPage() int
	// SetPage set current page
	SetPage(page int)
	// GetLimit get limit
	GetLimit() int
	// SetLimit set limit
	SetLimit(limit int)
	// GetParallelCount get parallel request count
	GetParallelCount() int
	// SetParallelCount set parallel request count
	SetParallelCount(count int)
}

IPaginator interface

type Logger

type Logger interface {
	Print(v ...interface{})
	Println(v ...interface{})
	Printf(format string, v ...interface{})
}

Logger Request logger interface Implement default logger methods

type Paginator added in v1.3.0

type Paginator struct {
	// Page of pagination
	Page int `json:"page"`
	// Limit for pagination
	Limit int `json:"limit"`
	// Parallel request
	ParallelCount int `json:"parallelCount"`
}

Paginator Base paginator struct Inject the struct into your request forms

func (*Paginator) GetLimit added in v1.3.0

func (p *Paginator) GetLimit() int

GetLimit get limit

func (*Paginator) GetPage added in v1.3.0

func (p *Paginator) GetPage() int

GetPage get current page

func (*Paginator) GetParallelCount added in v1.3.0

func (p *Paginator) GetParallelCount() int

GetParallelCount get count of max parallel requests

func (*Paginator) SetLimit added in v1.3.0

func (p *Paginator) SetLimit(limit int)

SetLimit set limit

func (*Paginator) SetPage added in v1.3.0

func (p *Paginator) SetPage(page int)

SetPage set current page

func (*Paginator) SetParallelCount added in v1.3.0

func (p *Paginator) SetParallelCount(count int)

SetParallelCount set max number of parallel request

type PaginatorResponse added in v1.3.0

type PaginatorResponse[T any] struct {
	// List of elements
	Items []T
	// Meta information
	Meta gorest.Meta
	// Error
	Error porterr.IError
}

PaginatorResponse Response from API

Jump to

Keyboard shortcuts

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