feature(1.2.8): 优化代码

- 删除 third_party_request
This commit is contained in:
新亮
2021-11-20 17:19:08 +08:00
parent b05ed0b59d
commit 498033753e
5 changed files with 0 additions and 220 deletions

View File

@@ -1,33 +0,0 @@
package third_party_request
import (
"github.com/xinliangnote/go-gin-api/configs"
"github.com/xinliangnote/go-gin-api/pkg/errors"
"github.com/xinliangnote/go-gin-api/pkg/httpclient"
"github.com/xinliangnote/go-gin-api/pkg/mail"
)
// 实现 AlarmObject 告警
var _ httpclient.AlarmObject = (*AlarmEmail)(nil)
type AlarmEmail struct{}
// Send 邮件告警方式
func (a *AlarmEmail) Send(subject, body string) error {
cfg := configs.Get().Mail
if cfg.Host == "" || cfg.Port == 0 || cfg.User == "" || cfg.Pass == "" || cfg.To == "" {
return errors.New("mail config error")
}
options := &mail.Options{
MailHost: cfg.Host,
MailPort: cfg.Port,
MailUser: cfg.User,
MailPass: cfg.Pass,
MailTo: cfg.To,
Subject: subject,
Body: body,
}
return mail.Send(options)
}

View File

@@ -1,63 +0,0 @@
package go_gin_api
import (
"encoding/json"
"github.com/xinliangnote/go-gin-api/pkg/httpclient"
"github.com/pkg/errors"
)
// 接口地址
var demoGetApi = "http://127.0.0.1:9999/demo/get/"
// 接口返回结构
type demoGetResponse struct {
Name string `json:"name"`
Job string `json:"job"`
}
// DemoGet 发起请求
func DemoGet(name string, opts ...httpclient.Option) (res *demoGetResponse, err error) {
api := demoGetApi + name
body, err := httpclient.Get(api, nil, opts...)
if err != nil {
return nil, err
}
res = new(demoGetResponse)
err = json.Unmarshal(body, res)
if err != nil {
return nil, errors.Wrap(err, "DemoGet json unmarshal error")
}
return res, nil
}
// DemoGetRetryVerify 设置重试规则
func DemoGetRetryVerify(body []byte) (shouldRetry bool) {
if len(body) == 0 {
return true
}
return false
}
// DemoGetAlarmVerify 设置告警规则
func DemoGetAlarmVerify(body []byte) (shouldAlarm bool) {
if len(body) == 0 {
return true
}
return false
}
// DemoGetMock 设置 Mock 数据
func DemoGetMock() (body []byte) {
res := new(demoGetResponse)
res.Name = "AA"
res.Job = "AA_JOB"
body, _ = json.Marshal(res)
return body
}

View File

@@ -1,29 +0,0 @@
package go_gin_api
import (
"testing"
"time"
"github.com/xinliangnote/go-gin-api/internal/api/third_party_request"
"github.com/xinliangnote/go-gin-api/pkg/httpclient"
)
var demoGetAuthorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOjEsIlVzZXJOYW1lIjoieGlubGlhbmdub3RlIiwiZXhwIjoxNjEzODI3MTEzLCJpYXQiOjE2MTM3NDA3MTMsIm5iZiI6MTYxMzc0MDcxM30.SnooP1ikO33ryGPdohsmOKqISa-bWzMkMvUNb5f2zc0"
func TestDemoGet(t *testing.T) {
res, err := DemoGet("Tom",
httpclient.WithTTL(time.Second*5),
//httpclient.WithTrace(ctx.Trace()),
//httpclient.WithLogger(ctx.Logger()),
httpclient.WithHeader("Authorization", demoGetAuthorization),
httpclient.WithOnFailedRetry(3, time.Second*1, DemoGetRetryVerify),
httpclient.WithOnFailedAlarm("接口告警", new(third_party_request.AlarmEmail), DemoGetAlarmVerify),
httpclient.WithMock(DemoGetMock),
)
if err != nil {
t.Log("get [demo/get] err", err)
}
t.Log(res)
}

View File

@@ -1,66 +0,0 @@
package go_gin_api
import (
"encoding/json"
"net/url"
"github.com/xinliangnote/go-gin-api/pkg/httpclient"
"github.com/pkg/errors"
)
// 接口地址
var demoPostApi = "http://127.0.0.1:9999/demo/post/"
// 接口返回结构
type demoPostResponse struct {
Name string `json:"name"`
Job string `json:"job"`
}
// DemoPost 发起请求
func DemoPost(name string, opts ...httpclient.Option) (res *demoPostResponse, err error) {
api := demoPostApi
params := url.Values{}
params.Set("name", name)
body, err := httpclient.PostForm(api, params, opts...)
if err != nil {
return nil, err
}
res = new(demoPostResponse)
err = json.Unmarshal(body, res)
if err != nil {
return nil, errors.Wrap(err, "DemoPost json unmarshal error")
}
return res, nil
}
// DemoPostRetryVerify 设置重试规则
func DemoPostRetryVerify(body []byte) (shouldRetry bool) {
if len(body) == 0 {
return true
}
return false
}
// DemoPostAlarmVerify 设置告警规则
func DemoPostAlarmVerify(body []byte) (shouldAlarm bool) {
if len(body) == 0 {
return true
}
return false
}
// DemoPostMock 设置 Mock 数据
func DemoPostMock() (body []byte) {
res := new(demoPostResponse)
res.Name = "BB"
res.Job = "BB_JOB"
body, _ = json.Marshal(res)
return body
}

View File

@@ -1,29 +0,0 @@
package go_gin_api
import (
"testing"
"time"
"github.com/xinliangnote/go-gin-api/internal/api/third_party_request"
"github.com/xinliangnote/go-gin-api/pkg/httpclient"
)
var demoPostAuthorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOjEsIlVzZXJOYW1lIjoieGlubGlhbmdub3RlIiwiZXhwIjoxNjEzODI3MTEzLCJpYXQiOjE2MTM3NDA3MTMsIm5iZiI6MTYxMzc0MDcxM30.SnooP1ikO33ryGPdohsmOKqISa-bWzMkMvUNb5f2zc0"
func TestDemoPost(t *testing.T) {
res, err := DemoPost("Jack",
httpclient.WithTTL(time.Second*5),
//httpclient.WithTrace(ctx.Trace()),
//httpclient.WithLogger(ctx.Logger()),
httpclient.WithHeader("Authorization", demoPostAuthorization),
httpclient.WithOnFailedRetry(3, time.Second*1, DemoPostRetryVerify),
httpclient.WithOnFailedAlarm("接口告警", new(third_party_request.AlarmEmail), DemoPostAlarmVerify),
httpclient.WithMock(DemoPostMock),
)
if err != nil {
t.Log("post [demo/post] err", err)
}
t.Log(res)
}