Skip to content

Commit

Permalink
feat: 支持context
Browse files Browse the repository at this point in the history
doRequest操作设置ctx
  • Loading branch information
mingzaily committed May 16, 2024
1 parent 82df83f commit 044c90e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
19 changes: 16 additions & 3 deletions alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"github.com/smartwalle/ngx"
"github.com/smartwalle/nsign"
"io"
"net/http"
"net/url"
Expand All @@ -21,6 +19,8 @@ import (
"time"

"github.com/smartwalle/ncrypto"
"github.com/smartwalle/ngx"
"github.com/smartwalle/nsign"
)

var (
Expand All @@ -46,6 +46,7 @@ type Client struct {
Client *http.Client
location *time.Location
onReceivedData func(method string, data []byte)
ctx context.Context

// 内容加密
needEncrypt bool
Expand Down Expand Up @@ -387,7 +388,11 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err
req.SetFileForm(param.FileParams())
}

rsp, err := req.Do(context.Background())
ctx := context.Background()
if c.ctx != nil {
ctx = c.ctx
}
rsp, err := req.Do(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -598,6 +603,14 @@ func (c *Client) OnReceivedData(fn func(method string, data []byte)) {
c.onReceivedData = fn
}

// WithContext 设置请求的上下文
func (c *Client) WithContext(ctx context.Context) *Client {
if ctx != nil {
c.ctx = ctx
}
return c
}

func base64decode(data []byte) ([]byte, error) {
var dBuf = make([]byte, base64.StdEncoding.DecodedLen(len(data)))
n, err := base64.StdEncoding.Decode(dBuf, data)
Expand Down
2 changes: 0 additions & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ go 1.18

require (
github.com/smartwalle/alipay/v3 v3.2.16
github.com/smartwalle/ngx v1.0.9
github.com/smartwalle/nsign v1.0.9
github.com/smartwalle/xid v1.0.7
)

Expand Down
12 changes: 7 additions & 5 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"context"
"fmt"
"github.com/smartwalle/alipay/v3"
"github.com/smartwalle/xid"
"log"
"net/http"

"github.com/smartwalle/alipay/v3"
"github.com/smartwalle/xid"
)

var client *alipay.Client
Expand Down Expand Up @@ -63,7 +65,7 @@ func pay(writer http.ResponseWriter, request *http.Request) {
p.TotalAmount = "10.00"
p.ProductCode = "FAST_INSTANT_TRADE_PAY"

url, _ := client.TradePagePay(p)
url, _ := client.WithContext(context.Background()).TradePagePay(p)
http.Redirect(writer, request, url.String(), http.StatusTemporaryRedirect)
}

Expand All @@ -84,7 +86,7 @@ func callback(writer http.ResponseWriter, request *http.Request) {
var p = alipay.TradeQuery{}
p.OutTradeNo = outTradeNo

rsp, err := client.TradeQuery(p)
rsp, err := client.WithContext(context.Background()).TradeQuery(p)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
writer.Write([]byte(fmt.Sprintf("验证订单 %s 信息发生错误: %s", outTradeNo, err.Error())))
Expand Down Expand Up @@ -116,7 +118,7 @@ func notify(writer http.ResponseWriter, request *http.Request) {
p.AddBizField("out_trade_no", notification.OutTradeNo)

var rsp *alipay.TradeQueryRsp
if err = client.Request(p, &rsp); err != nil {
if err = client.WithContext(context.Background()).Request(p, &rsp); err != nil {
log.Printf("异步通知验证订单 %s 信息发生错误: %s \n", notification.OutTradeNo, err.Error())
return
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/smartwalle/alipay/v3

go 1.18

require (
github.com/smartwalle/ncrypto v1.0.4
github.com/smartwalle/ngx v1.0.9
github.com/smartwalle/nsign v1.0.9
)

go 1.18
43 changes: 25 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
github.com/smartwalle/ncrypto v1.0.0 h1:nQFxIS3fRgr8V0xRkhnfNQOrcJGPNF6d5XzFwVm79KU=
github.com/smartwalle/ncrypto v1.0.0/go.mod h1:NmCbG0nLnSDnMImEDrjptFKs0PiLThnFkjQSMtGYgs4=
github.com/smartwalle/ncrypto v1.0.1 h1:CVe/h/srt6knLiF/9V5OnDDhguskWR801meHizq7KmU=
github.com/smartwalle/ncrypto v1.0.1/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ncrypto v1.0.2 h1:pTAhCqtPCMhpOwFXX+EcMdR6PNzruBNoGQrN2S1GbGI=
github.com/smartwalle/ncrypto v1.0.2/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ncrypto v1.0.3 h1:fnzjoriZt2LZeD8ljEtRe2eU33Au7i8vIF4Gafz5RuI=
github.com/smartwalle/ncrypto v1.0.3/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/smartwalle/ncrypto v1.0.4 h1:P2rqQxDepJwgeO5ShoC+wGcK2wNJDmcdBOWAksuIgx8=
github.com/smartwalle/ncrypto v1.0.4/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ngx v1.0.6 h1:JPNqNOIj+2nxxFtrSkJO+vKJfeNUSEQueck/Wworjps=
github.com/smartwalle/ngx v1.0.6/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0=
github.com/smartwalle/ngx v1.0.7 h1:BIQo6wmAnERehogNKUnthoxwBavTWxbR9oLFcGjWXKQ=
github.com/smartwalle/ngx v1.0.7/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0=
github.com/smartwalle/ngx v1.0.8 h1:FIQ0OPtQ7BNEhPutB8JWPU/nD1RJZEyPD++uDb5SZws=
github.com/smartwalle/ngx v1.0.8/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0=
github.com/smartwalle/ngx v1.0.9 h1:pUXDvWRZJIHVrCKA1uZ15YwNti+5P4GuJGbpJ4WvpMw=
github.com/smartwalle/ngx v1.0.9/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0=
github.com/smartwalle/nsign v0.0.1 h1:fpEwoThIjr1JRPqz5hEwbH5ADX/so6G/zcwvj0ywYyM=
github.com/smartwalle/nsign v0.0.1/go.mod h1:eY6I4CJlyNdVMP+t6z1H6Jpd4m5/V+8xi44ufSTxXgc=
github.com/smartwalle/nsign v1.0.8 h1:78KWtwKPrdt4Xsn+tNEBVxaTLIJBX9YRX0ZSrMUeuHo=
github.com/smartwalle/nsign v1.0.8/go.mod h1:eY6I4CJlyNdVMP+t6z1H6Jpd4m5/V+8xi44ufSTxXgc=
github.com/smartwalle/nsign v1.0.9 h1:8poAgG7zBd8HkZy9RQDwasC6XZvJpDGQWSjzL2FZL6E=
github.com/smartwalle/nsign v1.0.9/go.mod h1:eY6I4CJlyNdVMP+t6z1H6Jpd4m5/V+8xi44ufSTxXgc=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 h1:Xs2Ncz0gNihqu9iosIZ5SkBbWo5T8JhhLJFMQL1qmLI=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0/go.mod h1:vy+2G/6NvVMpwGX/NyLqcC41fxepnuKHk16E6IZUcJc=
go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs=
go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4=
go.opentelemetry.io/otel/metric v1.26.0 h1:7S39CLuY5Jgg9CrnA9HHiEjGMF/X2VHvoXGgSllRz30=
go.opentelemetry.io/otel/metric v1.26.0/go.mod h1:SY+rHOI4cEawI9a7N1A4nIg/nTQXe1ccCNWYOJUrpX4=
go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA=
go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 044c90e

Please sign in to comment.