Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions internal/client/eightsleep.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"compress/gzip"
"context"
"crypto/tls"
"encoding/json"
Expand Down Expand Up @@ -120,8 +121,8 @@ func (c *Client) authTokenEndpoint(ctx context.Context) error {
"grant_type": "password",
"username": c.Email,
"password": c.Password,
"client_id": "sleep-client",
"client_secret": "",
"client_id": c.ClientID,
"client_secret": c.ClientSecret,
}
body, _ := json.Marshal(payload)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, authURL, bytes.NewReader(body))
Expand Down Expand Up @@ -288,6 +289,15 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values,
return err
}
defer resp.Body.Close()
var bodyReader io.Reader = resp.Body
if resp.Header.Get("Content-Encoding") == "gzip" {
gr, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
defer gr.Close()
bodyReader = gr
}
if resp.StatusCode == http.StatusTooManyRequests {
time.Sleep(2 * time.Second)
return c.do(ctx, method, path, query, body, out)
Expand All @@ -301,11 +311,11 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values,
return c.do(ctx, method, path, query, body, out)
}
if resp.StatusCode >= 300 {
b, _ := io.ReadAll(resp.Body)
b, _ := io.ReadAll(bodyReader)
return fmt.Errorf("api %s %s: %s", method, path, string(b))
}
if out != nil {
return json.NewDecoder(resp.Body).Decode(out)
return json.NewDecoder(bodyReader).Decode(out)
}
return nil
}
Expand Down
30 changes: 30 additions & 0 deletions internal/client/eightsleep_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package client

import (
"compress/gzip"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -101,3 +103,31 @@ func Test429Retry(t *testing.T) {
t.Fatalf("expected backoff, got %v", elapsed)
}
}

func TestDoHandlesGzipResponse(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/gzipped", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
json.NewEncoder(gz).Encode(map[string]string{"hello": "world"})
gz.Close()
})
srv := httptest.NewServer(mux)
defer srv.Close()

c := New("email", "pass", "uid", "", "")
c.BaseURL = srv.URL
c.token = "t"
c.tokenExp = time.Now().Add(time.Hour)
c.HTTP = srv.Client()

var out map[string]string
if err := c.do(context.Background(), http.MethodGet, "/gzipped", nil, nil, &out); err != nil {
t.Fatalf("do gzip: %v", err)
}
if out["hello"] != "world" {
t.Fatalf("expected {hello: world}, got %v", out)
}
}