init
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package huya
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ActivityClient 抽象虎牙活动接口,HTTP 与 WSS 传输都实现它。
|
||||
type ActivityClient interface {
|
||||
GetActPrizeList(ctx context.Context, uid int64, cookie string, sid int32) (*GetActPrizeListResp, error)
|
||||
QueryUserScore(ctx context.Context, uid int64, cookie string, sid int32) (*GetUserScoreResp, error)
|
||||
GetUserPrizeRecords(ctx context.Context, uid int64, cookie string, sid int32) (*GetUserPrizeRecordsResp, error)
|
||||
ScoreExchangePrize(ctx context.Context, uid int64, cookie string, sid, pid int32) (*ScoreExchangePrizeResp, error)
|
||||
}
|
||||
|
||||
// NewActivityClient 按传输层创建活动客户端,供 CLI 和 GUI 共用。
|
||||
func NewActivityClient(transport, wssHost string, timeout time.Duration, logger func(string)) (ActivityClient, func(), error) {
|
||||
switch strings.ToLower(strings.TrimSpace(transport)) {
|
||||
case "", "http":
|
||||
client := NewHTTPClient(logger)
|
||||
if timeout > 0 {
|
||||
client.Client.Timeout = timeout
|
||||
}
|
||||
return client, func() {}, nil
|
||||
case "wss", "ws", "websocket":
|
||||
client := NewWSSClient(logger)
|
||||
if strings.TrimSpace(wssHost) != "" {
|
||||
client.Host = strings.TrimSpace(wssHost)
|
||||
}
|
||||
if timeout > 0 {
|
||||
client.CallTimeout = timeout
|
||||
}
|
||||
return client, func() { _ = client.Close() }, nil
|
||||
default:
|
||||
return nil, func() {}, fmt.Errorf("未知传输层: %s", transport)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user