/
mysql_api.go
55 lines (45 loc) · 1.29 KB
/
mysql_api.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
package service
import (
gt "gopkg.in/h2non/gentleman.v2"
oHttp "github.com/fwtpe/owl-backend/common/http"
"github.com/fwtpe/owl-backend/common/http/client"
apiModel "github.com/fwtpe/owl-backend/common/model/mysqlapi"
)
type MysqlApiServiceConfig struct {
*oHttp.RestfulClientConfig
}
type MysqlApiService interface {
GetHealth() *apiModel.MysqlApi
}
func NewMysqlApiService(config *MysqlApiServiceConfig) MysqlApiService {
newClient := oHttp.NewApiService(config.RestfulClientConfig).NewClient()
return &mysqlApiServiceImpl{
Config: config,
getHealth: newClient.Get().AddPath("/health"),
}
}
type mysqlApiServiceImpl struct {
Config *MysqlApiServiceConfig
getHealth *gt.Request
}
// Return some information about MySQL-API: address, error message,
// response of health request
func (s *mysqlApiServiceImpl) GetHealth() *apiModel.MysqlApi {
req := s.getHealth.Clone()
view := &apiModel.MysqlApi{
Address: s.Config.HttpClientConfig.Url,
}
resp, err := client.ToGentlemanReq(req).SendAndStatusMatch(200)
if err != nil {
view.Message = err.Error()
return view
}
// Handle the response body
health := &apiModel.HealthView{}
if err := client.ToGentlemanResp(resp).BindJson(health); err != nil {
view.Message = err.Error()
return view
}
view.Response = health
return view
}