Merge pull request #3 from xinsnake/roundtripper
Implement http.RoundTripper
diff --git a/README.md b/README.md
index bfc938d..b21bd40 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,26 @@
// check error, get response
```
+
+Or you can use it with `http.Request`
+
+```go
+t := dac.NewTransport(username, password)
+req, err := http.NewRequest(method, uri, payload)
+
+if err != nil {
+ log.Fatalln(err)
+}
+
+resp, err := t.RoundTrip(req)
+if err != nil {
+ log.Fatalln(err)
+}
+
+fmt.Println(resp)
+```
+
+
# Todos
* Unit testing
diff --git a/digest_auth_client.go b/digest_auth_client.go
index a67a6b2..4111228 100644
--- a/digest_auth_client.go
+++ b/digest_auth_client.go
@@ -17,13 +17,24 @@
Wa *wwwAuthenticate
}
-func NewRequest(username string, password string, method string, uri string, body string) DigestRequest {
+type DigestTransport struct {
+ Password string
+ Username string
+}
+func NewRequest(username string, password string, method string, uri string, body string) DigestRequest {
dr := DigestRequest{}
dr.UpdateRequest(username, password, method, uri, body)
return dr
}
+func NewTransport(username string, password string) DigestTransport {
+ dt := DigestTransport{}
+ dt.Password = password
+ dt.Username = username
+ return dt
+}
+
func (dr *DigestRequest) UpdateRequest(username string,
password string, method string, uri string, body string) *DigestRequest {
@@ -35,6 +46,23 @@
return dr
}
+func (dt *DigestTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
+ username := dt.Username
+ password := dt.Password
+ method := req.Method
+ uri := req.URL.String()
+
+ var body string
+ if req.Body != nil {
+ buf := new(bytes.Buffer)
+ buf.ReadFrom(req.Body)
+ body = buf.String()
+ }
+
+ dr := NewRequest(username, password, method, uri, body)
+ return dr.Execute()
+}
+
func (dr *DigestRequest) Execute() (resp *http.Response, err error) {
if dr.Auth == nil {