1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package sdk
- import (
- "git.sxidc.com/service-supports/ds-sdk/client"
- "git.sxidc.com/service-supports/ds-sdk/grpc_client"
- "github.com/pkg/errors"
- )
- var sdkInstance *SDK
- func GetInstance() *SDK {
- return sdkInstance
- }
- func InitInstance(token string, address string, httpPort string, grpcPort string, namespace string, dataSource string, opts ...Option) error {
- if sdkInstance != nil {
- return nil
- }
- options := &Options{
- token: token,
- baseUrl: "http://" + address + ":" + httpPort,
- grpcAddress: address + ":" + grpcPort,
- namespace: namespace,
- dataSource: dataSource,
- }
- for _, opt := range opts {
- opt(options)
- }
- c := client.New(options.timeout)
- namespaceInfos, err := c.GetNamespaces(options.token, options.baseUrl, options.namespace, 1, 1)
- if err != nil {
- return err
- }
- if namespaceInfos == nil || len(namespaceInfos) == 0 {
- return errors.New("命名空间不存在")
- }
- dataSourceInfos, err := c.GetDataSources(
- options.token, options.baseUrl, options.namespace, options.dataSource, "", 1, 1)
- if err != nil {
- return err
- }
- if dataSourceInfos == nil || len(dataSourceInfos) == 0 {
- return errors.New("数据源不存在")
- }
- grpcClient, err := grpc_client.NewClient(options.grpcAddress)
- if err != nil {
- return err
- }
- sdkInstance = &SDK{
- options: options,
- client: c,
- grpcClient: grpcClient,
- }
- return nil
- }
- func DestroyInstance() error {
- if sdkInstance == nil {
- return nil
- }
- err := grpc_client.Destroy(sdkInstance.grpcClient)
- if err != nil {
- return err
- }
- sdkInstance = nil
- return nil
- }
|