| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- 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
- case ServiceTypeActivity:
- baseURL = DefaultActivityBaseURL
- case ServiceTypePlan:
- baseURL = DefaultPlanBaseURL
- 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)
- }
|