wx

package
v1.1.64 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeBytesToUint32

func DecodeBytesToUint32(b []byte) uint32

DecodeBytesToUint32 从 4 字节的网络字节序里解析出整数 uint32

func EncodeUint32ToBytes

func EncodeUint32ToBytes(i uint32) []byte

EncodeUint32ToBytes 把整数 uint32 格式化成 4 字节的网络字节序

func FormatMap2XML

func FormatMap2XML(m WXML) ([]byte, error)

FormatMap2XML format map to xml

func HMacSHA256

func HMacSHA256(s, key string) string

HMacSHA256 generates a keyed sha256 hash value.

func HTTPDo

func HTTPDo(ctx context.Context, method, reqURL string, body []byte, options ...HTTPOption) (*http.Response, error)

HTTPDo sends an HTTP request and returns an HTTP response

func HTTPGet

func HTTPGet(ctx context.Context, reqURL string, options ...HTTPOption) (*http.Response, error)

HTTPGet issues a GET to the specified URL.

func HTTPPost

func HTTPPost(ctx context.Context, reqURL string, body []byte, options ...HTTPOption) (*http.Response, error)

HTTPPost issues a POST to the specified URL.

func HTTPPostForm

func HTTPPostForm(ctx context.Context, reqURL string, data url.Values, options ...HTTPOption) (*http.Response, error)

HTTPPostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.

func HTTPUpload

func HTTPUpload(ctx context.Context, reqURL string, form UploadForm, options ...HTTPOption) (*http.Response, error)

HTTPUpload issues a UPLOAD to the specified URL.

func LoadP12Cert

func LoadP12Cert(pfxfile, mchid string) (tls.Certificate, error)

LoadP12Cert 通过p12(pfx)证书文件生成Pem证书

func MD5

func MD5(s string) string

MD5 calculates the md5 hash of a string.

func MarshalNoEscapeHTML

func MarshalNoEscapeHTML(v interface{}) ([]byte, error)

MarshalNoEscapeHTML marshal with no escape HTML

func NewECBDecrypter

func NewECBDecrypter(b cipher.Block) cipher.BlockMode

NewECBDecrypter returns a BlockMode which decrypts in electronic code book mode, using the given Block.

func NewECBEncrypter

func NewECBEncrypter(b cipher.Block) cipher.BlockMode

NewECBEncrypter returns a BlockMode which encrypts in electronic code book mode, using the given Block.

func Nonce

func Nonce(size uint) string

Nonce returns nonce string, param `size` better for even number.

func PKCS5Padding

func PKCS5Padding(cipherText []byte, blockSize int) []byte

func PKCS5Unpadding

func PKCS5Unpadding(plainText []byte, blockSize int) []byte

func RSADecryptOEAP

func RSADecryptOEAP(cipherText, privateKey []byte) ([]byte, error)

RSADecryptOEAP rsa decrypt with PKCS #1 OEAP.

func RSAEncryptOEAP

func RSAEncryptOEAP(plainText, publicKey []byte) ([]byte, error)

RSAEncryptOEAP rsa encrypt with PKCS #1 OEAP.

func SHA256

func SHA256(s string) string

SHA256 calculates the sha256 hash of a string.

func ZeroPadding

func ZeroPadding(cipherText []byte, blockSize int) []byte

func ZeroUnPadding

func ZeroUnPadding(plainText []byte) []byte

Types

type AESCrypto

type AESCrypto interface {
	// Encrypt encrypts the plain text.
	Encrypt(plainText []byte) ([]byte, error)

	// Decrypt decrypts the cipher text.
	Decrypt(cipherText []byte) ([]byte, error)
}

AESCrypto is the interface for aes crypto.

func NewCBCCrypto

func NewCBCCrypto(key, iv []byte, mode PaddingMode) AESCrypto

NewCBCCrypto returns a new aes-cbc crypto.

func NewECBCrypto

func NewECBCrypto(key []byte, mode PaddingMode) AESCrypto

NewECBCrypto returns a new aes-ecb crypto.

type Action

type Action interface {
	// Method returns action method
	Method() string

	// URL returns request url
	URL(accessToken ...string) string

	// WXML returns body for xml request
	WXML(mchid, nonce string) (WXML, error)

	// Body returns body for post request
	Body() ([]byte, error)

	// UploadForm returns form for uploading media
	UploadForm() (UploadForm, error)

	// Decode decodes response
	Decode(resp []byte) error

	// IsUpload specifies the request does upload
	IsUpload() bool

	// TLS specifies the request with certificate
	IsTLS() bool
}

Action is the interface that handle wechat api

func NewAction

func NewAction(method string, reqURL string, options ...ActionOption) Action

NewAction returns a new action

func NewGetAction

func NewGetAction(reqURL string, options ...ActionOption) Action

NewGetAction returns a new action with GET method

func NewPostAction

func NewPostAction(reqURL string, options ...ActionOption) Action

NewPostAction returns a new action with POST method

type ActionOption

type ActionOption func(a *action)

ActionOption configures how we set up the action

func WithBody

func WithBody(f func() ([]byte, error)) ActionOption

WithBody sets post body for action.

func WithDecode

func WithDecode(f func(resp []byte) error) ActionOption

WithDecode sets response decode for action.

func WithQuery

func WithQuery(key, value string) ActionOption

WithQuery sets query params for action.

func WithTLS

func WithTLS() ActionOption

WithTLS sets request with tls for action.

func WithUpload

func WithUpload(f func() (UploadForm, error)) ActionOption

WithUpload sets uploadform for action.

func WithWXML

func WithWXML(f func(mchid, nonce string) (WXML, error)) ActionOption

WithWXML sets post with xml for action.

type CDATA

type CDATA string

CDATA XML CDATA section which is defined as blocks of text that are not parsed by the parser, but are otherwise recognized as markup.

func (CDATA) MarshalXML

func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML encodes the receiver as zero or more XML elements.

type Client

type Client interface {
	// Post sends an HTTP post request
	Do(ctx context.Context, method, reqURL string, body []byte, options ...HTTPOption) ([]byte, error)

	// Upload sends an HTTP post request for uploading media
	Upload(ctx context.Context, reqURL string, form UploadForm, options ...HTTPOption) ([]byte, error)

	// Set sets options for client
	Set(options ...ClientOption)
}

Client is the interface that do http request

func DefaultClient

func DefaultClient(certs ...tls.Certificate) Client

DefaultClient returns a new default wechat client

type ClientOption

type ClientOption func(c *wxclient)

ClientOption configures how we set up the wechat client.

func WithDebug

func WithDebug() ClientOption

WithDebug sets debug mode for wechat client.

func WithHTTPClient

func WithHTTPClient(client HTTPClient) ClientOption

WithHTTPClient sets http client for wechat client.

func WithLogger

func WithLogger(logger Logger) ClientOption

WithLogger sets logger for wechat client.

type FormFileFunc

type FormFileFunc func(w io.Writer) error

FormFileFunc writes file content to multipart writer.

type HTTPClient

type HTTPClient interface {
	// Do sends an HTTP request and returns an HTTP response.
	// Should use context to specify the timeout for request.
	Do(ctx context.Context, method, reqURL string, body []byte, options ...HTTPOption) (*http.Response, error)

	// Upload issues a UPLOAD to the specified URL.
	// Should use context to specify the timeout for request.
	Upload(ctx context.Context, reqURL string, form UploadForm, options ...HTTPOption) (*http.Response, error)
}

HTTPClient is the interface for a http client.

func NewHTTPClient

func NewHTTPClient(client *http.Client) HTTPClient

NewHTTPClient returns a new http client

type HTTPOption

type HTTPOption func(s *httpSetting)

HTTPOption configures how we set up the http request.

func WithHTTPClose

func WithHTTPClose() HTTPOption

WithHTTPClose specifies close the connection after replying to this request (for servers) or after sending this request and reading its response (for clients).

func WithHTTPCookies

func WithHTTPCookies(cookies ...*http.Cookie) HTTPOption

WithHTTPCookies specifies the cookies to http request.

func WithHTTPHeader

func WithHTTPHeader(key, value string) HTTPOption

WithHTTPHeader specifies the header to http request.

type LogData

type LogData struct {
	URL        string        `json:"url"`
	Method     string        `json:"method"`
	Body       []byte        `json:"body"`
	StatusCode int           `json:"status_code"`
	Response   []byte        `json:"response"`
	Duration   time.Duration `json:"duration"`
	Error      error         `json:"error"`
}

type Logger

type Logger interface {
	Log(ctx context.Context, data *LogData)
}

func DefaultLogger

func DefaultLogger() Logger

DefaultLogger returns default logger

type M

type M map[string]interface{}

M is a convenient alias for a map[string]interface{}.

type PaddingMode

type PaddingMode string

PaddingMode aes padding mode

const (
	// ZERO zero padding mode
	ZERO PaddingMode = "ZERO"
	// PKCS5 PKCS#5 padding mode
	PKCS5 PaddingMode = "PKCS#5"
	// PKCS7 PKCS#7 padding mode
	PKCS7 PaddingMode = "PKCS#7"
)

type PemBlockType

type PemBlockType string

PemBlockType pem block type which taken from the preamble.

const (
	// RSAPKCS1 private key in PKCS#1
	RSAPKCS1 PemBlockType = "RSA PRIVATE KEY"
	// RSAPKCS8 private key in PKCS#8
	RSAPKCS8 PemBlockType = "PRIVATE KEY"
)

type UploadField

type UploadField func(f *uploadform)

UploadField configures how we set up the upload from.

func WithFormField

func WithFormField(fieldname, fieldvalue string) UploadField

WithFormField specifies the form field to upload from.

func WithFormFile

func WithFormFile(fieldname, filename string, fn FormFileFunc) UploadField

WithFormFile specifies the file field to upload from.

type UploadForm

type UploadForm interface {
	// Write writes fields to multipart writer
	Write(w *multipart.Writer) error
}

UploadForm is the interface for http upload.

func NewUploadForm

func NewUploadForm(fields ...UploadField) UploadForm

NewUploadForm returns an upload form

type WXML

type WXML map[string]string

WXML deal with xml for wechat

func ParseXML2Map

func ParseXML2Map(b []byte) (WXML, error)

ParseXML2Map parse xml to map

Jump to

Keyboard shortcuts

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