Skip to content

Commit f97edeb

Browse files
authored
Merge pull request #5 from jpg0/upload-with-custom-httpclient
Added the ability to specify an http client for uploads.
2 parents 3828fb9 + edfe854 commit f97edeb

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

upload.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ func UploadFile(client *FlickrClient, path string, optionalParams *UploadParams)
138138

139139
// UploadReader does same as UploadFile but the photo file is passed as an io.Reader instead of a file path
140140
func UploadReader(client *FlickrClient, photoReader io.Reader, name string, optionalParams *UploadParams) (*UploadResponse, error) {
141+
return UploadReaderWithClient(client, photoReader, name, optionalParams, nil)
142+
}
143+
144+
// UploadReaderWithClient does same as UploadReader but allows passing a custom httpClient
145+
func UploadReaderWithClient(client *FlickrClient, photoReader io.Reader, name string, optionalParams *UploadParams, httpClient *http.Client) (*UploadResponse, error) {
141146
client.Init()
142147
client.EndpointUrl = UPLOAD_ENDPOINT
143148
client.HTTPVerb = "POST"
@@ -163,18 +168,20 @@ func UploadReader(client *FlickrClient, photoReader io.Reader, name string, opti
163168
req.Header.Set("content-type", "multipart/form-data; boundary="+boundary)
164169
req.ContentLength = -1 // unknown
165170

166-
// Create a Transport to explicitly use the http1.1 client
167-
// TODO: for some reason, when we use the http2 client flickr API responds
168-
// with HTTP: 411 (No Content Length : POST) whereas it should be ok to
169-
// upload using chunks. Explicitly setting `req.Header.Set("transfer-encoding", "chunked")`
170-
// does not help and try to compute the request size isn't the right thing to do IMHO.
171-
// We should investigate why this happens instead of forcing the downgrade to http1.1.
172-
tr := &http.Transport{
173-
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
174-
}
171+
if (httpClient == nil) {
172+
// Create a Transport to explicitly use the http1.1 client
173+
// TODO: for some reason, when we use the http2 client flickr API responds
174+
// with HTTP: 411 (No Content Length : POST) whereas it should be ok to
175+
// upload using chunks. Explicitly setting `req.Header.Set("transfer-encoding", "chunked")`
176+
// does not help and try to compute the request size isn't the right thing to do IMHO.
177+
// We should investigate why this happens instead of forcing the downgrade to http1.1.
178+
tr := &http.Transport{
179+
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
180+
}
175181

176-
// instance an HTTP client
177-
httpClient := &http.Client{Transport: tr}
182+
// instance an HTTP client
183+
httpClient = &http.Client{Transport: tr}
184+
}
178185

179186
// perform upload request streaming the file
180187
resp, err := httpClient.Do(req)

0 commit comments

Comments
 (0)