Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.

Commit ddd37fe

Browse files
authored
Merge pull request #3 from xinsnake/roundtripper
Implement http.RoundTripper
2 parents 76485d3 + 91831c5 commit ddd37fe

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ response2, err := dr.Execute()
2222

2323
// check error, get response
2424
```
25+
26+
Or you can use it with `http.Request`
27+
28+
```go
29+
t := dac.NewTransport(username, password)
30+
req, err := http.NewRequest(method, uri, payload)
31+
32+
if err != nil {
33+
log.Fatalln(err)
34+
}
35+
36+
resp, err := t.RoundTrip(req)
37+
if err != nil {
38+
log.Fatalln(err)
39+
}
40+
41+
fmt.Println(resp)
42+
```
43+
44+
2545
# Todos
2646

2747
* Unit testing

digest_auth_client.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,24 @@ type DigestRequest struct {
1717
Wa *wwwAuthenticate
1818
}
1919

20-
func NewRequest(username string, password string, method string, uri string, body string) DigestRequest {
20+
type DigestTransport struct {
21+
Password string
22+
Username string
23+
}
2124

25+
func NewRequest(username string, password string, method string, uri string, body string) DigestRequest {
2226
dr := DigestRequest{}
2327
dr.UpdateRequest(username, password, method, uri, body)
2428
return dr
2529
}
2630

31+
func NewTransport(username string, password string) DigestTransport {
32+
dt := DigestTransport{}
33+
dt.Password = password
34+
dt.Username = username
35+
return dt
36+
}
37+
2738
func (dr *DigestRequest) UpdateRequest(username string,
2839
password string, method string, uri string, body string) *DigestRequest {
2940

@@ -35,6 +46,23 @@ func (dr *DigestRequest) UpdateRequest(username string,
3546
return dr
3647
}
3748

49+
func (dt *DigestTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
50+
username := dt.Username
51+
password := dt.Password
52+
method := req.Method
53+
uri := req.URL.String()
54+
55+
var body string
56+
if req.Body != nil {
57+
buf := new(bytes.Buffer)
58+
buf.ReadFrom(req.Body)
59+
body = buf.String()
60+
}
61+
62+
dr := NewRequest(username, password, method, uri, body)
63+
return dr.Execute()
64+
}
65+
3866
func (dr *DigestRequest) Execute() (resp *http.Response, err error) {
3967

4068
if dr.Auth == nil {

0 commit comments

Comments
 (0)