Documentation
¶
Index ¶
- Constants
- func WithResponseDefaultLanguage(lang language.Tag) func(*response)
- func WithResponseErrorMsgHandler(supported []language.Tag, handle func(code int, lang language.Tag) string) func(*response)
- func WithResponseLanguageHeaderKey(languageHeaderKey string) func(*response)
- func WithResponseLogger(logger xlog.XLog) func(*response)
- func WithResponseShowXCode(xcodes ...xcode.XCode) func(*response)
- type IResponse
- type TableResponse
- type TableResponseItem
- type TableResponseRowExtendItem
Constants ¶
View Source
const ( PageLinkHeaderKey = "Link" PageInfoHeaderKey = "X-Pagination-Info" TotalCountHeaderKey = "X-PaginateTotal-Count" HasMoreHeaderKey = "X-More-Resource" ErrorCodeHeaderKey = "X-Error-Code" ErrorDataHeaderKey = "X-Error-Data" LangHeaderKey = "X-Language" )
Variables ¶
This section is empty.
Functions ¶
func WithResponseDefaultLanguage ¶
WithResponseDefaultLanguage 设置默认语言
func WithResponseErrorMsgHandler ¶
func WithResponseErrorMsgHandler(supported []language.Tag, handle func(code int, lang language.Tag) string) func(*response)
WithResponseErrorMsgHandler 指定错误消息处理器。 主要应用于多国语言展示错误信息。 其中,`code` 为错误码;`language` 为标准的 i18n 标识
func WithResponseLanguageHeaderKey ¶
func WithResponseLanguageHeaderKey(languageHeaderKey string) func(*response)
WithResponseLanguageHeaderKey 设置多国语言 header 参数 key, 默认为 `X-Language`。 主要应用于多国语言展示错误信息。
func WithResponseLogger ¶
WithResponseLogger 指定 Logger
func WithResponseShowXCode ¶
WithResponseShowXCode 设置需要对客户端展示的错误码。 默认情况下,该值为空,则表示所有错误码均向用户展示(设置为空,亦如此);
Types ¶
type IResponse ¶
type IResponse interface {
// SetHeader 设置请求头
SetHeader(key, value string) IResponse
// Retrieve 查询单个资源的响应
Retrieve(entity interface{})
// TableWithPagination 表格分页响应
TableWithPagination(resp *TableResponse)
// ListWithPagination 分页列表的响应
ListWithPagination(totalRow uint, entities interface{})
// ListWithMoreFlag 查询列表的响应
ListWithMoreFlag(hasMore bool, entities interface{})
// Post 新增请求的响应
Post(entity interface{})
// Put 全量更新资源的响应
Put(entity interface{})
// Patch 部分更新资源的响应
// 部分 cdn 服务商不支持 http patch 方法,如 阿里云
Patch(entity interface{})
// Delete 删除的响应
Delete(err error)
// WithMessage 通过 json 响应文本消息: {"message": "something..."}
WithMessage(msg string)
// WithBody 响应文本消息
WithBody(body string)
// WithError 响应错误消息(HttpStatus!=200)
WithError(err error)
// WithErrorData 响应错误消息(HttpStatus!=200),并在 header 中返回错误数据
WithErrorData(err error, data interface{})
}
func NewResponse ¶
NewResponse 创建 Restful 标准响应生成器
usage: 基础用法 rru := restful.NewResponse(ctx)
启用语言包
rru := restful.NewResponse( ctx, restful.WithResponseErrorMsgHandler(lang.Handle()), )
type TableResponse ¶
type TableResponse struct {
TotalRow uint // 分页的记录条数
Columns []string // 表格列
RowKeys []string // 表格行
Items []*TableResponseItem // 表格行数据
Extends []*TableResponseRowExtendItem // 表格行扩展数据
}
type TableResponseItem ¶
type TableResponseRowExtendItem ¶
type TableResponseRowExtendItem struct {
RowKey string // 行关键字
Data interface{} // 数据
}
Click to show internal directories.
Click to hide internal directories.