Documentation ¶
Index ¶
- Constants
- Variables
- func CheckAk(ak string) error
- func CheckSk(sk string) error
- func ClientProd() *http.Client
- func ClientTest() *http.Client
- func GetSign(query string, body []byte, tim time.Time, uuid string, credential Credential) string
- type AddUserAgentHttpHook
- type ApiDecoratorChain
- type ApiHandler
- type ApiHook
- type ApiHookBuilder
- type Credential
- type CtyunClient
- type CtyunClientConfig
- type CtyunRequest
- type CtyunRequestBuilder
- type CtyunRequestError
- func ConsoleLogApiHook(ctx context.Context, credential *Credential, param interface{}, ...) (interface{}, CtyunRequestError)
- func ErrorAfterResponse(err error, resp *CtyunResponse) CtyunRequestError
- func ErrorBeforeRequest(err error) CtyunRequestError
- func ErrorBeforeResponse(err error) CtyunRequestError
- func WrapError(err error, resp *CtyunResponse) CtyunRequestError
- func WrapWithErrorCode(err error, errorCode string, resp *CtyunResponse) CtyunRequestError
- type CtyunResponse
- type CtyunResponseModel
- type Endpoint
- type EndpointName
- type EndpointRegistry
- type Environment
- type HttpHook
- type OccurOccasion
- type PrintLogHttpHook
- type StubApiHook
- type StubFunc
Constants ¶
const ( EnvironmentDev = "dev" EnvironmentTest = "test" EnvironmentProd = "prod" )
const ( AkLen = 32 SkLen = 32 )
const ( OccurOccasionBeforeRequest = 0 // 请求前 OccurOccasionBeforeResponse = 1 // 响应前 OccurOccasionAfterResponse = 2 // 响应后 )
const (
StatusCodeSuccessString = "800"
)
Variables ¶
var Environments = []Environment{ EnvironmentDev, EnvironmentTest, EnvironmentProd, }
Functions ¶
Types ¶
type AddUserAgentHttpHook ¶
type AddUserAgentHttpHook struct { }
func (AddUserAgentHttpHook) AfterResponse ¶
func (h AddUserAgentHttpHook) AfterResponse(_ context.Context, _ *http.Response)
func (AddUserAgentHttpHook) BeforeRequest ¶
func (h AddUserAgentHttpHook) BeforeRequest(_ context.Context, request *http.Request)
type ApiDecoratorChain ¶
type ApiDecoratorChain struct { Hooks []ApiHook Target interface{} }
ApiDecoratorChain 处理链
func (ApiDecoratorChain) Next ¶
func (this ApiDecoratorChain) Next(ctx context.Context, credential *Credential, param interface{}) (interface{}, CtyunRequestError)
Next 驱动处理链执行下个动作
type ApiHandler ¶
type ApiHandler interface { // Do 执行api动作 Do(context.Context, *Credential, interface{}) (interface{}, CtyunRequestError) }
ApiHandler api处理器
type ApiHook ¶
type ApiHook func(context.Context, *Credential, interface{}, ApiDecoratorChain) (interface{}, CtyunRequestError)
ApiHook api钩子
type ApiHookBuilder ¶
type ApiHookBuilder struct {
// contains filtered or unexported fields
}
func NewApiHookBuilder ¶
func NewApiHookBuilder() *ApiHookBuilder
func (*ApiHookBuilder) AddHooks ¶
func (this *ApiHookBuilder) AddHooks(hook ApiHook) *ApiHookBuilder
AddHooks 添加钩子
type Credential ¶
type Credential struct {
// contains filtered or unexported fields
}
func NewCredential ¶
func NewCredential(ak string, sk string) (*Credential, error)
NewCredential 构造新的凭证
func NewCredentialFromEnv ¶
func NewCredentialFromEnv() (*Credential, error)
NewCredentialFromEnv 构造新的凭证
type CtyunClient ¶
type CtyunClient struct { Env Environment Config *CtyunClientConfig // contains filtered or unexported fields }
func NewCtyunClient ¶
func NewCtyunClient(env Environment, cfg *CtyunClientConfig) (*CtyunClient, error)
NewCtyunClient 新建环境
func (*CtyunClient) RegisterEndpoint ¶
func (c *CtyunClient) RegisterEndpoint(env Environment, endpoint Endpoint)
RegisterEndpoint 注册端点
func (CtyunClient) RequestToEndpoint ¶
func (c CtyunClient) RequestToEndpoint(ctx context.Context, endpointName EndpointName, request *CtyunRequest) (*CtyunResponse, CtyunRequestError)
RequestToEndpoint 向端点发送请求
type CtyunClientConfig ¶
type CtyunRequest ¶
type CtyunRequest struct {
// contains filtered or unexported fields
}
func (*CtyunRequest) AddHeader ¶
func (c *CtyunRequest) AddHeader(key, value string) *CtyunRequest
AddHeader 增加请求头
func (*CtyunRequest) AddParam ¶
func (c *CtyunRequest) AddParam(key, value string) *CtyunRequest
AddParam 增加参数
func (*CtyunRequest) WriteJson ¶
func (c *CtyunRequest) WriteJson(data interface{}) (*CtyunRequest, CtyunRequestError)
WriteJson 以application/json方式写入
func (*CtyunRequest) WriteXWwwFormUrlEncoded ¶
func (c *CtyunRequest) WriteXWwwFormUrlEncoded(data url.Values) *CtyunRequest
WriteXWwwFormUrlEncoded 以x-www-form-urlencoded方式写入
type CtyunRequestBuilder ¶
func (CtyunRequestBuilder) ReplaceUrl ¶ added in v1.0.4
func (c CtyunRequestBuilder) ReplaceUrl(src string, target interface{}) *CtyunRequestBuilder
ReplaceUrl 替换路径中的目标值,例如把/orders/{masterOrderId}替换为/orders/1
func (CtyunRequestBuilder) WithCredential ¶
func (c CtyunRequestBuilder) WithCredential(credential *Credential) *CtyunRequest
WithCredential 增加请求credential
type CtyunRequestError ¶
type CtyunRequestError interface { error // 错误信息 OccurOccasion() OccurOccasion // 发生场合 ErrorCode() string // 返回码 CtyunResponse() *CtyunResponse // 标准返回 }
func ConsoleLogApiHook ¶
func ConsoleLogApiHook(ctx context.Context, credential *Credential, param interface{}, chain ApiDecoratorChain) (interface{}, CtyunRequestError)
ConsoleLogApiHook 打印控制台日志的钩子函数
func ErrorAfterResponse ¶
func ErrorAfterResponse(err error, resp *CtyunResponse) CtyunRequestError
ErrorAfterResponse 在响应后发生的异常
func ErrorBeforeRequest ¶
func ErrorBeforeRequest(err error) CtyunRequestError
ErrorBeforeRequest 在请求前发生的异常
func ErrorBeforeResponse ¶
func ErrorBeforeResponse(err error) CtyunRequestError
ErrorBeforeResponse 在响应前发生的异常
func WrapWithErrorCode ¶
func WrapWithErrorCode(err error, errorCode string, resp *CtyunResponse) CtyunRequestError
WrapWithErrorCode 包裹异常
type CtyunResponse ¶
func (CtyunResponse) Parse ¶
func (c CtyunResponse) Parse(obj interface{}) CtyunRequestError
Parse 解析为目标对象
func (CtyunResponse) ParseByStandardModel ¶
func (c CtyunResponse) ParseByStandardModel(obj interface{}) (*CtyunResponseModel, CtyunRequestError)
ParseByStandardModel 解析为标准模型对象
func (CtyunResponse) ParseByStandardModelWithCheck ¶
func (c CtyunResponse) ParseByStandardModelWithCheck(obj interface{}) CtyunRequestError
ParseByStandardModelWithCheck 解析并且判断
type CtyunResponseModel ¶
type CtyunResponseModel struct { StatusCode interface{} `json:"statusCode"` ErrorCode interface{} `json:"errorCode"` Message string `json:"message"` Description string `json:"description"` ReturnObj interface{} `json:"returnObj,omitempty"` }
func (CtyunResponseModel) IsSuccess ¶
func (c CtyunResponseModel) IsSuccess() bool
IsSuccess 判断响应是否成功
func (CtyunResponseModel) ParseErrorCode ¶
func (c CtyunResponseModel) ParseErrorCode() string
ParseErrorCode 解析ErrorCode到string
func (CtyunResponseModel) ParseStatusCode ¶
func (c CtyunResponseModel) ParseStatusCode() string
ParseStatusCode 解析StatusCode到string
type Endpoint ¶
type Endpoint struct { EndpointName EndpointName // 端点的名称 Url string // 对应的地址 }
type EndpointName ¶
type EndpointName string
type EndpointRegistry ¶
type EndpointRegistry map[EndpointName]Endpoint
func (EndpointRegistry) GetEndpoint ¶
func (e EndpointRegistry) GetEndpoint(endpointName EndpointName) (Endpoint, bool)
GetEndpoint 获取端点信息
func (*EndpointRegistry) Register ¶
func (e *EndpointRegistry) Register(endpoint Endpoint) error
Register 注册端点
type Environment ¶
type Environment string
type OccurOccasion ¶
type OccurOccasion int
type PrintLogHttpHook ¶
type PrintLogHttpHook struct { }
func (PrintLogHttpHook) AfterResponse ¶
func (d PrintLogHttpHook) AfterResponse(_ context.Context, response *http.Response)
func (PrintLogHttpHook) BeforeRequest ¶
func (d PrintLogHttpHook) BeforeRequest(_ context.Context, request *http.Request)
type StubApiHook ¶
type StubApiHook struct {
// contains filtered or unexported fields
}
func NewStubApiHook ¶
func NewStubApiHook() *StubApiHook
func (*StubApiHook) RegisterStubWithApiHandler ¶
func (this *StubApiHook) RegisterStubWithApiHandler(handler interface{}, f StubFunc)
RegisterStubWithApiHandler 注册桩
func (*StubApiHook) RegisterStubWithName ¶
func (this *StubApiHook) RegisterStubWithName(stubName string, f StubFunc)
RegisterStubWithName 注册桩
func (StubApiHook) Stub ¶
func (this StubApiHook) Stub(ctx context.Context, credential *Credential, param interface{}, chain ApiDecoratorChain) (interface{}, CtyunRequestError)
Stub 打桩
type StubFunc ¶
type StubFunc func(ctx context.Context, credential *Credential, param interface{}, target interface{}) (interface{}, CtyunRequestError)