packer

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2020 License: MIT Imports: 10 Imported by: 0

README

vk-execute-packer (WIP)

Documentation Go Report Card license

Пакер запросов для либы vksdk

go get github.com/zweihander/vk-execute-packer
Пример
package main

import (
	"fmt"
	"os"

	"github.com/SevereCloud/vksdk/v2/api"
	"github.com/SevereCloud/vksdk/v2/api/params"
	packer "github.com/zweihander/vk-execute-packer/v2"
)

func main() {
	token := os.Getenv("TOKEN")
	vk := api.NewVK(token)
	packer.Default(vk, packer.Debug())

	resp, err := vk.UtilsResolveScreenName(
		params.NewUtilsResolveScreenNameBuilder().
			ScreenName("durov").Params,
	)
	if err != nil {
		panic(err)
	}

	fmt.Println("durov id:", resp.ObjectID)
}
Параметры

Параметры передаются в виде аргументов в методы packer.Default() и packer.New()

  • packer.Debug() включает вывод дебаг инфы
  • packer.Tokens(tokens...) форсит пакер использовать предоставленные токены для выполнения execute-ов
    (без этой опции пакер будет использовать токены применяющиеся в запросах)
  • packer.MaxPackedRequests(num) устанавливает максимальное кол-во запросов в пачке (максимум 25)
  • packer.Rules(mode, methods...) устанавливает правила фильтрации методов
    Пример:
// батчить только messages.send и messages.edit
packer.Default(vk, packer.Rules(packer.Allow, "messages.send", "messages.edit"))

// батчить все методы кроме groups.getMembers и board.getTopics
packer.Default(vk, packer.Rules(packer.Ignore, "groups.getMembers", "board.getTopics"))

// P.S. метод execute всегда выполняется отдельно

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default(vk *api.VK, opts ...Option)

Default creates new Packer, wraps vk.Handler and creates timeout-based trigger for sending batches every 2 seconds.

Types

type FilterMode

type FilterMode bool

FilterMode - batch filter mode

const (
	// Allow mode
	Allow FilterMode = true
	// Ignore mode
	Ignore FilterMode = false
)

type Option

type Option func(*Packer)

Option - Packer option

func Debug

func Debug() Option

Debug enables printing debug info into stdout.

func MaxPackedRequests

func MaxPackedRequests(max int) Option

MaxPackedRequests sets the maximum API calls inside one batch.

func Rules

func Rules(mode FilterMode, methods ...string) Option

Rules sets the batching rules (ignore some methods or allow it).

func Tokens

func Tokens(tokens ...string) Option

Tokens provides tokens which will be used for sending batch requests. If tokens are not provided, packer will use tokens from incoming requests.

type Packer

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

Packer struct

func New

func New(handler VKHandler, opts ...Option) *Packer

New creates a new Packer.

NOTE: this method will not create any trigger for sending batches which means that the batch will be sent only when the number of requests in it equals to 'maxPackedRequests' (default 25, can be overwritten with MaxPackedRequests() option). You will need to create your custom logic which sometimes will call packer.Send() method to solve this.

func (*Packer) Handler

func (p *Packer) Handler(method string, params ...api.Params) (api.Response, error)

Handler implements vk.Handler function, which proceeds requests to VK API.

func (*Packer) Send

func (p *Packer) Send()

Send sends current batch if it contains at least one request.

type VKHandler

type VKHandler = func(string, ...api.Params) (api.Response, error)

VKHandler - alias to function which proceeds requests to VK API.

Jump to

Keyboard shortcuts

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