-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
115 lines (86 loc) · 2.99 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package sdk
import (
"encoding/json"
"errors"
"fmt"
"github.com/paradeum-team/chainstorage-sdk/model"
"net/http"
"sync"
)
type SdkConfig Configuration
var once = sync.Once{}
var mClient *CssClient
type CssClient struct {
Config *Configuration
Logger *PldLogger
httpClient *RestyClient
Bucket *Bucket
Object *Object
Car *Car
}
func newClient(config *ApplicationConfig) (*CssClient, error) {
var err error
once.Do(func() {
initConfig(config)
mClient = &CssClient{}
mClient.Config = &cssConfig
mClient.Logger = GetLogger(&cssLoggerConfig)
mClient.httpClient = &RestyClient{Config: mClient.Config}
mClient.Bucket = &Bucket{Config: mClient.Config, Client: mClient.httpClient, logger: mClient.Logger.logger}
mClient.Object = &Object{Config: mClient.Config, Client: mClient.httpClient, logger: mClient.Logger.logger}
mClient.Car = &Car{Config: mClient.Config, Client: mClient.httpClient, logger: mClient.Logger.logger}
})
//mClient.Logger.logger.Error("client new.")
return mClient, err
}
func New(config *ApplicationConfig) (*CssClient, error) {
return newClient(config)
}
func (c *CssClient) GetIpfsVersion() (model.VersionResponse, error) {
response := model.VersionResponse{}
// 请求Url
apiBaseAddress := c.Config.ChainStorageApiEndpoint
apiPath := "ipfsVersion"
apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath)
// API调用
httpStatus, body, err := c.httpClient.RestyGet(apiUrl)
if err != nil {
c.Logger.logger.Errorf(fmt.Sprintf("API:GetIpfsVersion:HttpGet, apiUrl:%s, httpStatus:%d, err:%+v\n", apiUrl, httpStatus, err))
return response, err
}
if httpStatus != http.StatusOK {
c.Logger.logger.Errorf(fmt.Sprintf("API:GetIpfsVersion:HttpGet, apiUrl:%s, httpStatus:%d, body:%s\n", apiUrl, httpStatus, string(body)))
return response, errors.New(string(body))
}
// 响应数据解析
err = json.Unmarshal(body, &response)
if err != nil {
c.Logger.logger.Errorf(fmt.Sprintf("API:GetIpfsVersion:JsonUnmarshal, body:%s, err:%+v\n", string(body), err))
return response, err
}
return response, nil
}
func (c *CssClient) GetApiVersion() (model.VersionResponse, error) {
response := model.VersionResponse{}
// 请求Url
apiBaseAddress := c.Config.ChainStorageApiEndpoint
apiPath := "version"
apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath)
// API调用
httpStatus, body, err := c.httpClient.RestyGet(apiUrl)
if err != nil {
c.Logger.logger.Errorf(fmt.Sprintf("API:GetApiVersion:HttpGet, apiUrl:%s, httpStatus:%d, err:%+v\n", apiUrl, httpStatus, err))
return response, err
}
if httpStatus != http.StatusOK {
c.Logger.logger.Errorf(fmt.Sprintf("API:GetApiVersion:HttpGet, apiUrl:%s, httpStatus:%d, body:%s\n", apiUrl, httpStatus, string(body)))
return response, errors.New(string(body))
}
// 响应数据解析
err = json.Unmarshal(body, &response)
if err != nil {
c.Logger.logger.Errorf(fmt.Sprintf("API:GetApiVersion:JsonUnmarshal, body:%s, err:%+v\n", string(body), err))
return response, err
}
return response, nil
}