aboutsummaryrefslogtreecommitdiff
path: root/go/util/http_util.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/util/http_util.go')
-rw-r--r--go/util/http_util.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/go/util/http_util.go b/go/util/http_util.go
index 10e781140..7854302ab 100644
--- a/go/util/http_util.go
+++ b/go/util/http_util.go
@@ -3,6 +3,7 @@ package util
import (
"bytes"
"fmt"
+ "io"
"io/ioutil"
"net/http"
"net/url"
@@ -84,6 +85,43 @@ func Delete(url string, jwt security.EncodedJwt) error {
return nil
}
+func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachBuffer func([]byte)) error {
+ r, err := client.PostForm(url, values)
+ if err != nil {
+ return err
+ }
+ defer r.Body.Close()
+ if r.StatusCode != 200 {
+ return fmt.Errorf("%s: %s", url, r.Status)
+ }
+ bufferSize := len(allocatedBytes)
+ for {
+ n, err := r.Body.Read(allocatedBytes)
+ if n == bufferSize {
+ eachBuffer(allocatedBytes)
+ }
+ if err != nil {
+ if err == io.EOF {
+ return nil
+ }
+ return err
+ }
+ }
+ return nil
+}
+
+func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) error {
+ r, err := client.PostForm(url, values)
+ if err != nil {
+ return err
+ }
+ defer r.Body.Close()
+ if r.StatusCode != 200 {
+ return fmt.Errorf("%s: %s", url, r.Status)
+ }
+ return readFn(r.Body)
+}
+
func DownloadUrl(fileUrl string) (filename string, content []byte, e error) {
response, err := client.Get(fileUrl)
if err != nil {