-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7b3dba
commit 5e5db89
Showing
10 changed files
with
176 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
internal/api/repository/third_party_request/go_gin_api_repo/go_gin_api_mock.go
This file was deleted.
Oops, something went wrong.
80 changes: 0 additions & 80 deletions
80
internal/api/repository/third_party_request/go_gin_api_repo/go_gin_api_test.go
This file was deleted.
Oops, something went wrong.
20 changes: 15 additions & 5 deletions
20
...i/repository/third_party_request/alarm.go → internal/api/third_party_request/alarm.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,34 @@ | ||
package third_party_request | ||
|
||
import ( | ||
"github.com/xinliangnote/go-gin-api/configs" | ||
"github.com/xinliangnote/go-gin-api/pkg/httpclient" | ||
"github.com/xinliangnote/go-gin-api/pkg/mail" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// 实现 AlarmObject 告警 | ||
var _ httpclient.AlarmObject = (*AlarmEmail)(nil) | ||
|
||
type AlarmEmail struct{} | ||
|
||
// 邮件告警方式 | ||
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: "smtp.163.com", | ||
MailPort: 465, | ||
MailUser: "xx@163.com", | ||
MailPass: "", | ||
MailTo: "", | ||
MailHost: cfg.Host, | ||
MailPort: cfg.Port, | ||
MailUser: cfg.User, | ||
MailPass: cfg.Pass, | ||
MailTo: cfg.To, | ||
Subject: subject, | ||
Body: body, | ||
} | ||
|
||
return mail.Send(options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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"` | ||
} | ||
|
||
// 发起请求 | ||
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 | ||
} | ||
|
||
// 设置重试规则 | ||
func DemoGetRetryVerify(body []byte) (shouldRetry bool) { | ||
if len(body) == 0 { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
// 设置告警规则 | ||
func DemoGetAlarmVerify(body []byte) (shouldAlarm bool) { | ||
if len(body) == 0 { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
// 设置 Mock 数据 | ||
func DemoGetMock() (body []byte) { | ||
res := new(demoGetResponse) | ||
res.Name = "AA" | ||
res.Job = "AA_JOB" | ||
|
||
body, _ = json.Marshal(res) | ||
return body | ||
} |
29 changes: 29 additions & 0 deletions
29
internal/api/third_party_request/go_gin_api/demoget_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.