utils

package
v0.0.0-...-deab030 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package utils 包含一些公共工具

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultURLBase 是默认的 api 地址前缀
	DefaultURLBase = "https://open.feishu.cn/open-apis"
)

Functions

func GetJSON

func GetJSON(ctx context.Context, urlPath string, params url.Values, result interface{}) error

GetJSON 使用 GET 方法调用位于 URLBase+urlPath 的接口,params 是 url 上的参数,result 是响应的 body, 用 json 编码; 调用者可使用 APIOptions 附着到 ctx 来调整调用配置

func GetJSONWithAppAccessToken

func GetJSONWithAppAccessToken(ctx context.Context, urlPath string, provider conf.AppAccessTokenProvider, params url.Values, result interface{}) error

GetJSONWithAppAccessToken 类似于 GetJSON,不过 Authorization 头部会添加 app access token

func GetJSONWithTenantAccessToken

func GetJSONWithTenantAccessToken(ctx context.Context, urlPath string, provider conf.TenantAccessTokenProvider, params url.Values, result interface{}) error

GetJSONWithTenantAccessToken 类似于 GetJSON,不过 Authorization 头部会添加 tenant access token

func PostJSON

func PostJSON(ctx context.Context, urlPath string, body interface{}, result interface{}) error

PostJSON 使用 POST 方法调用位于 URLBase+urlPath 的接口,body 是请求的 body,result 是响应的 body, 两者均用 json 编码/解码; 调用者可使用 APIOptions 附着到 ctx 来调整调用配置

func PostJSONWithAppAccessToken

func PostJSONWithAppAccessToken(ctx context.Context, urlPath string, provider conf.AppAccessTokenProvider, body interface{}, result interface{}) error

PostJSONWithAppAccessToken 类似于 PostJSON,不过 Authorization 头部会添加 app access token

func PostJSONWithTenantAccessToken

func PostJSONWithTenantAccessToken(ctx context.Context, urlPath string, provider conf.TenantAccessTokenProvider, body interface{}, result interface{}) error

PostJSONWithTenantAccessToken 类似于 PostJSON,不过 Authorization 头部会添加 tenant access token

Types

type APIError

type APIError APIResultBase

APIError 是 API 错误

func (*APIError) Error

func (e *APIError) Error() string

type APIOptions

type APIOptions struct {
	// URLBase 是 api 地址前缀,若空使用 DefaultURLBase.
	// NOTE: URLBase 结尾的 '/' 会被截取掉
	URLBase string

	// Client 是使用的 http client, 若空使用 http.DefaultClient
	Client HTTPClient
}

APIOptions 是调用 api 时的配置

func CtxAPIOptions

func CtxAPIOptions(ctx context.Context) APIOptions

CtxAPIOptions 从 context.Context 中获得 APIOptions, 若 context.Context 没有,则返回全局默认

func DefaultAPIOptions

func DefaultAPIOptions() APIOptions

DefaultAPIOptions 获得全局默认的 APIOptions

func (APIOptions) AsDefault

func (opts APIOptions) AsDefault()

AsDefault 将 APIOptions 设置成全局默认

func (APIOptions) WithCtx

func (opts APIOptions) WithCtx(ctx context.Context) context.Context

WithCtx 将 APIOptions 附着到 context.Context 中并返回一个新的 context.Context

type APIResultBase

type APIResultBase struct {
	// Code 是错码,非 0 表示错误
	Code int `json:"code"`

	// Msg 是错误描述
	Msg string `json:"msg"`
}

APIResultBase 是 api 返回结果基础字段

func (*APIResultBase) ResultError

func (result *APIResultBase) ResultError() error

ResultError 在 Code 非 0 的时候返回非 nil error

type HTTPClient

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPClient 是泛化的 http.Client

Jump to

Keyboard shortcuts

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