Skip to content

Commit 3e971c9

Browse files
committed
feat: 增加RequestId
1 parent c1ccc1a commit 3e971c9

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/forgoer/ghttp
22

33
go 1.12
4+
5+
require github.com/google/uuid v1.3.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
2+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

request.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ghttp
22

33
import (
44
"crypto/tls"
5+
"github.com/google/uuid"
56
"io/ioutil"
67
"net/http"
78
"net/url"
@@ -17,11 +18,13 @@ type Request struct {
1718
payload interface{}
1819
header http.Header
1920
timeout time.Duration
21+
RequestId string
2022
}
2123

2224
func NewRequest() *Request {
2325
request := &Request{
24-
header: make(http.Header),
26+
header: make(http.Header),
27+
RequestId: uuid.New().String(),
2528
}
2629
return request
2730
}
@@ -90,7 +93,11 @@ func (r *Request) Send() (*Response, error) {
9093
return nil, err
9194
}
9295

93-
return NewResponse(rep, body, time.Now().Sub(start)), nil
96+
response := NewResponse(rep, body, time.Now().Sub(start))
97+
98+
response.RequestId = r.RequestId
99+
100+
return response, nil
94101
}
95102

96103
func (r *Request) Method(method string) *Request {

response.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Response struct {
1212
RawBody []byte
1313
Code int
1414
Duration time.Duration
15+
RequestId string
1516
}
1617

1718
func NewResponse(resp *http.Response, body []byte, duration time.Duration) *Response {

0 commit comments

Comments
 (0)