| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package managesdk
- import (
- "fmt"
- "time"
- "github.com/go-resty/resty/v2"
- )
- type Client struct {
- config *Config
- http *resty.Client
- manage *ManageService
- opLog *OperateLogService
- }
- func NewClient(config Config) *Client {
- timeout := 10
- if config.Timeout > 0 {
- timeout = config.Timeout
- }
- baseURL := config.BaseURL
- if baseURL == "" {
- switch config.Type {
- case ServiceTypePerson:
- baseURL = DefaultPersonBaseURL
- case ServiceTypeResource:
- baseURL = DefaultResourceBaseURL
- default:
- baseURL = DefaultPersonBaseURL
- }
- }
- httpClient := resty.New().
- SetBaseURL(baseURL).
- SetTimeout(time.Duration(timeout)*time.Second).
- SetHeader("Content-Type", "application/json")
- c := &Client{
- config: &config,
- http: httpClient,
- }
- c.manage = &ManageService{client: c}
- c.opLog = &OperateLogService{client: c}
- return c
- }
- func (c *Client) Type() ServiceType {
- return c.config.Type
- }
- func (c *Client) Manage() *ManageService {
- return c.manage
- }
- func (c *Client) OperateLog() *OperateLogService {
- return c.opLog
- }
- func (c *Client) Version() (map[string]any, error) {
- var resp VersionResponse
- _, err := c.http.R().SetResult(&resp).Get("/version")
- if err != nil {
- return nil, fmt.Errorf("请求版本接口失败: %w", err)
- }
- if !resp.Success {
- return nil, fmt.Errorf("获取版本失败: %s", resp.Msg)
- }
- return resp.Data, nil
- }
- func (c *Client) apiV1Path(path string) string {
- return fmt.Sprintf("/v1%s", path)
- }
|