summaryrefslogtreecommitdiff
path: root/client/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'client/client.go')
-rw-r--r--client/client.go21
1 files changed, 10 insertions, 11 deletions
diff --git a/client/client.go b/client/client.go
index b68b2be..60d4461 100644
--- a/client/client.go
+++ b/client/client.go
@@ -3,7 +3,6 @@
package client
import (
- "bytes"
"fmt"
"io"
"net/http"
@@ -20,28 +19,28 @@ type CaesarClient struct {
once sync.Once
}
-// EncodeMessage asks the Caesar server to encode the supplied message and
-// returns the result along with an error in case the request fails.
-func (c *CaesarClient) EncodeMessage(r io.Reader) (io.Reader, error) {
+// EncodeMessage asks the Caesar server to encode the supplied message, writes
+// the encoded message to the supplied writer, and returns an error in case the
+// request fails.
+func (c *CaesarClient) EncodeMessage(w io.Writer, r io.Reader) error {
c.once.Do(func() {
c.c = &http.Client{Timeout: 10 * time.Second}
})
req, err := http.NewRequest("POST", c.Endpoint, r)
if err != nil {
- return nil, err
+ return err
}
req.Header.Set("user-agent", "caesar-client/1.0")
resp, err := c.c.Do(req)
if err != nil {
- return nil, err
+ return err
}
if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("http status %s", resp.Status)
+ return fmt.Errorf("http status %s", resp.Status)
}
- var body bytes.Buffer
defer resp.Body.Close()
- if _, err := io.Copy(&body, resp.Body); err != nil {
- return nil, err
+ if _, err := io.Copy(w, resp.Body); err != nil {
+ return err
}
- return &body, nil
+ return nil
}