Skip to content

Commit 4f28b01

Browse files
author
Adam Hasselbalch Hansen
committed
example... works?"
1 parent 00cf4f7 commit 4f28b01

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ auth for GET and POST (and other) HTTP methods
1414

1515

1616
## <a name="pkg-index">Index</a>
17-
* [func RandomKey() string](#RandomKey)
1817
* [type AuthClient](#AuthClient)
19-
* [func NewAuthClient(c \*http.Client, user, pass string) \*AuthClient](#NewAuthClient)
20-
* [func (c *AuthClient) Do(r \*http.Request) (\*http.Response, error)](#AuthClient.Do)
18+
* [func NewAuthClient(c *http.Client, user, pass string) *AuthClient](#NewAuthClient)
19+
* [func (c *AuthClient) Do(r *http.Request) (*http.Response, error)](#AuthClient.Do)
2120

2221

2322
#### <a name="pkg-files">Package files</a>
@@ -27,12 +26,6 @@ auth for GET and POST (and other) HTTP methods
2726

2827

2928

30-
## <a name="RandomKey">func</a> [RandomKey](/src/target/digest.go?s=1990:2013#L78)
31-
``` go
32-
func RandomKey() string
33-
```
34-
35-
3629

3730
## <a name="AuthClient">type</a> [AuthClient](/src/target/digest.go?s=303:377#L6)
3831
``` go

digest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *AuthClient) Do(r *http.Request) (*http.Response, error) {
3737
// Copy headers
3838
(*initreq).Header = (*r).Header
3939

40-
resp, err := c.Do(initreq)
40+
resp, err := c.Client.Do(initreq)
4141
if err != nil {
4242
return nil, err
4343
}
@@ -46,7 +46,7 @@ func (c *AuthClient) Do(r *http.Request) (*http.Response, error) {
4646
// No auth necessary
4747
// redo the request with the body if needed
4848
if r.Method != "GET" {
49-
resp, err = c.Do(r)
49+
resp, err = c.Client.Do(r)
5050
}
5151
} else {
5252

@@ -57,7 +57,7 @@ func (c *AuthClient) Do(r *http.Request) (*http.Response, error) {
5757
digestParts["password"] = c.Password
5858
r.Header.Set("Authorization", getDigestAuthrization(digestParts))
5959

60-
resp, err = c.Do(r)
60+
resp, err = c.Client.Do(r)
6161
}
6262

6363
return resp, err

example_digest_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package digest_test
2+
3+
import (
4+
"digest"
5+
"fmt"
6+
"net/http"
7+
"net/http/httputil"
8+
"strings"
9+
)
10+
11+
// ExampleDo demonstrates how AuthClient is a drop-in replacement for http.Client
12+
func Example_Do() {
13+
14+
url := "http://posttestserver.com/post.php"
15+
method = "POST"
16+
body := strings.NewReader(`{"key":"value"}`)
17+
18+
client := &http.Client{}
19+
20+
request, err := http.NewRequest(method, url, body)
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
request.Header.Set("Content-Type", "application/json")
26+
27+
username := "JohnnyBravo"
28+
password := "OhMomma!"
29+
30+
authclient := digest.NewAuthClient(client, username, password)
31+
32+
resp, err := ac.Do(request)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
b, err = httputil.DumpResponse(resp, true)
38+
if err != nil {
39+
panic(err)
40+
}
41+
fmt.Printf("%s", string(b))
42+
43+
}

0 commit comments

Comments
 (0)