Allow fetching with arbitrary *http.Client.

This commit is contained in:
Anschel Schaffer-Cohen 2013-03-19 09:49:00 -04:00
parent 79b9108a49
commit ab85624fd0
1 changed files with 10 additions and 3 deletions

View File

@ -182,10 +182,11 @@ func (this *Document) LoadFile(filename string, charset CharsetFunc) (err error)
return this.LoadStream(fd, charset) return this.LoadStream(fd, charset)
} }
// Load the contents of this document from the supplied uri. // Load the contents of this document from the supplied uri using the specifed
func (this *Document) LoadUri(uri string, charset CharsetFunc) (err error) { // client.
func (this *Document) LoadUriClient(uri string, client *http.Client, charset CharsetFunc) (err error) {
var r *http.Response var r *http.Response
if r, err = http.Get(uri); err != nil { if r, err = client.Get(uri); err != nil {
return return
} }
@ -193,6 +194,12 @@ func (this *Document) LoadUri(uri string, charset CharsetFunc) (err error) {
return this.LoadStream(r.Body, charset) return this.LoadStream(r.Body, charset)
} }
// Load the contents of this document from the supplied uri.
// (calls LoadUriClient with http.DefaultClient)
func (this *Document) LoadUri(uri string, charset CharsetFunc) (err error) {
return this.LoadUriClient(uri, http.DefaultClient, charset)
}
// Save the contents of this document to the supplied file. // Save the contents of this document to the supplied file.
func (this *Document) SaveFile(path string) error { func (this *Document) SaveFile(path string) error {
return ioutil.WriteFile(path, this.SaveBytes(), 0600) return ioutil.WriteFile(path, this.SaveBytes(), 0600)