ctyunsdk

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: Apache-2.0 Imports: 21 Imported by: 14

Documentation

Index

Constants

View Source
const (
	EnvironmentDev  = "dev"
	EnvironmentTest = "test"
	EnvironmentProd = "prod"
)
View Source
const (
	AkLen = 32
	SkLen = 32
)
View Source
const (
	OccurOccasionBeforeRequest  = 0 // 请求前
	OccurOccasionBeforeResponse = 1 // 响应前
	OccurOccasionAfterResponse  = 2 // 响应后
)
View Source
const (
	StatusCodeSuccessString = "800"
)

Variables

Functions

func CheckAk

func CheckAk(ak string) error

CheckAk 校验ak

func CheckSk

func CheckSk(sk string) error

CheckSk 校验sk

func ClientProd

func ClientProd() *http.Client

ClientProd 生产环境客户端

func ClientTest

func ClientTest() *http.Client

ClientTest 测试环境客户端

func GetSign

func GetSign(query string, body []byte, tim time.Time, uuid string, credential Credential) string

GetSign 加签

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处理器

func Wrap

func Wrap(target ApiHandler, builder ApiHookBuilder) ApiHandler

Wrap 构造

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 EnvOf

func EnvOf(env Environment) *CtyunClient

EnvOf 通过指定环境构建

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 CtyunClientConfig struct {
	Client    *http.Client
	ApiHooks  []ApiHook
	HttpHooks []HttpHook
}

func ClientConfigProd

func ClientConfigProd() *CtyunClientConfig

ClientConfigProd 构建生产环境默认的客户端

func ClientConfigTest

func ClientConfigTest() *CtyunClientConfig

ClientConfigTest 构建测试环境默认的客户端

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

type CtyunRequestBuilder struct {
	Method  string // 请求方法
	UrlPath string // url路径
}

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 WrapError

func WrapError(err error, resp *CtyunResponse) CtyunRequestError

WrapError 包裹异常

func WrapWithErrorCode

func WrapWithErrorCode(err error, errorCode string, resp *CtyunResponse) CtyunRequestError

WrapWithErrorCode 包裹异常

type CtyunResponse

type CtyunResponse struct {
	Request  *http.Request
	Response *http.Response
}

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 HttpHook

type HttpHook interface {
	BeforeRequest(context.Context, *http.Request)
	AfterResponse(context.Context, *http.Response)
}

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)

Jump to

Keyboard shortcuts

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