forked from parnurzeal/gorequest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request parnurzeal#162 from fromkeith/develop-clone
"Clone" the SuperAgent for reuse
- Loading branch information
Showing
11 changed files
with
1,109 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// +build go1.2 | ||
// +build !go1.3 | ||
|
||
package gorequest | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
|
||
// we don't want to mess up other clones when we modify the client.. | ||
// so unfortantely we need to create a new client | ||
func (s *SuperAgent) safeModifyHttpClient() { | ||
if !s.isClone { | ||
return | ||
} | ||
oldClient := s.Client | ||
s.Client = &http.Client{} | ||
s.Client.Jar = oldClient.Jar | ||
s.Client.Transport = oldClient.Transport | ||
s.Client.CheckRedirect = oldClient.CheckRedirect | ||
} | ||
|
||
// I'm not sure how this func will work with Clone. | ||
func (s *SuperAgent) Timeout(timeout time.Duration) *SuperAgent { | ||
s.Transport.Dial = func(network, addr string) (net.Conn, error) { | ||
conn, err := net.DialTimeout(network, addr, timeout) | ||
if err != nil { | ||
s.Errors = append(s.Errors, err) | ||
return nil, err | ||
} | ||
conn.SetDeadline(time.Now().Add(timeout)) | ||
return conn, nil | ||
} | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// +build go1.3 | ||
|
||
package gorequest | ||
|
||
import ( | ||
"time" | ||
"net/http" | ||
) | ||
|
||
|
||
// we don't want to mess up other clones when we modify the client.. | ||
// so unfortantely we need to create a new client | ||
func (s *SuperAgent) safeModifyHttpClient() { | ||
if !s.isClone { | ||
return | ||
} | ||
oldClient := s.Client | ||
s.Client = &http.Client{} | ||
s.Client.Jar = oldClient.Jar | ||
s.Client.Transport = oldClient.Transport | ||
s.Client.Timeout = oldClient.Timeout | ||
s.Client.CheckRedirect = oldClient.CheckRedirect | ||
} | ||
|
||
|
||
func (s *SuperAgent) Timeout(timeout time.Duration) *SuperAgent { | ||
s.safeModifyHttpClient() | ||
s.Client.Timeout = timeout | ||
return s | ||
} |
Oops, something went wrong.