47 lines
915 B
Go
47 lines
915 B
Go
package api
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net/http"
|
|
|
|
"gitlink.org.cn/cloudream/common/sdks"
|
|
"gitlink.org.cn/cloudream/jcs-pub/client/internal/http/auth"
|
|
"gitlink.org.cn/cloudream/jcs-pub/client/sdk/api"
|
|
"golang.org/x/net/http2"
|
|
)
|
|
|
|
type response[T any] struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
Data T `json:"data"`
|
|
}
|
|
|
|
func (r *response[T]) ToError() *sdks.CodeMessageError {
|
|
return &sdks.CodeMessageError{
|
|
Code: r.Code,
|
|
Message: r.Message,
|
|
}
|
|
}
|
|
|
|
type Client struct {
|
|
cfg api.Config
|
|
httpCli *http.Client
|
|
}
|
|
|
|
func NewClient(cfg api.Config) *Client {
|
|
httpCli := &http.Client{
|
|
Transport: &http2.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
RootCAs: cfg.RootCA,
|
|
Certificates: []tls.Certificate{cfg.Cert},
|
|
ServerName: auth.ClientInternalSNI,
|
|
NextProtos: []string{"h2"},
|
|
},
|
|
},
|
|
}
|
|
return &Client{
|
|
cfg: cfg,
|
|
httpCli: httpCli,
|
|
}
|
|
}
|