network

package
v3.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApiPost added in v3.8.1

func ApiPost(ctx context.Context, api string, req, resp any) (err error)

ApiPost performs an HTTP POST request to a specified API endpoint. It serializes the request object, sends it to the API, and unmarshals the response into the resp object. It handles logging, error wrapping, and operation ID validation. Context (ctx) is used for passing metadata and control information. api: the API endpoint to which the request is sent. req: the request object to be sent to the API. resp: a pointer to the response object where the API response will be unmarshalled. Returns an error if the request fails at any stage.

func CallApi added in v3.8.1

func CallApi[T any](ctx context.Context, api string, req any) (*T, error)

CallApi wraps ApiPost to make an API call and unmarshal the response into a new instance of type T.

func FetchAndInsertPagedData added in v3.8.1

func FetchAndInsertPagedData[RESP, L any](ctx context.Context, api string, req page.PageReq, fn func(resp *RESP) []L, batchInsertFn func(ctx context.Context, items []L) error,
	insertFn func(ctx context.Context, item L) error, maxItems int64) error

func GetPageAll added in v3.8.1

func GetPageAll[A interface {
	GetPagination() *sdkws.RequestPagination
}, B, C any](ctx context.Context, api string, req A, fn func(resp *B) []C) ([]C, error)

GetPageAll handles pagination for API requests. It iterates over pages of data until all data is retrieved. A is the request type with pagination support, B is the response type, and C is the type of data to be returned. The function fn processes each page of response data to extract a slice of C.

func GetPageAllWithMaxNum added in v3.8.1

func GetPageAllWithMaxNum[A interface {
	GetPagination() *sdkws.RequestPagination
}, B, C any](ctx context.Context, api string, req A, fn func(resp *B) []C, maxItems int) ([]C, error)

func PageNext added in v3.8.1

func PageNext[Req pagination, Resp any, Elem any](ctx context.Context, req Req, api func(ctx context.Context, req Req) (*Resp, error), fn func(*Resp) []Elem) ([]Elem, error)

func PostWithTimeOut

func PostWithTimeOut(url string, data interface{}, token string, timeout time.Duration) (content []byte, err error)

func Str2bytes

func Str2bytes(s string) []byte

Types

type ApiResponse added in v3.8.1

type ApiResponse struct {
	ErrCode int             `json:"errCode"`
	ErrMsg  string          `json:"errMsg"`
	ErrDlt  string          `json:"errDlt"`
	Data    json.RawMessage `json:"data"`
}

ApiResponse represents the standard structure of an API response.

type HttpCli

type HttpCli struct {
	Error error
	// contains filtered or unexported fields
}

func Get

func Get(url string) *HttpCli

func Post

func Post(url string) *HttpCli

func (*HttpCli) BodyWithBytes

func (c *HttpCli) BodyWithBytes(buf []byte) *HttpCli

func (*HttpCli) BodyWithForm

func (c *HttpCli) BodyWithForm(form map[string]string) *HttpCli

func (*HttpCli) BodyWithJson

func (c *HttpCli) BodyWithJson(obj interface{}) *HttpCli

func (*HttpCli) SetHeader

func (c *HttpCli) SetHeader(key, value string) *HttpCli

func (*HttpCli) SetTimeOut

func (c *HttpCli) SetTimeOut(timeout time.Duration) *HttpCli

func (*HttpCli) ToBytes

func (c *HttpCli) ToBytes() (content []byte, err error)

func (*HttpCli) ToJson

func (c *HttpCli) ToJson(obj interface{}) error

Jump to

Keyboard shortcuts

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