-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: idiomatic checks battery, remove httpmock (#38)
* FEAT: idiomatic checks battery, remove httpmock * add test for html size contentlength shortcut
- Loading branch information
Showing
14 changed files
with
330 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type CarbonData struct { | ||
Statistics struct { | ||
AdjustedBytes float64 `json:"adjustedBytes"` | ||
Energy float64 `json:"energy"` | ||
Co2 struct { | ||
Grid struct { | ||
Grams float64 `json:"grams"` | ||
Litres float64 `json:"litres"` | ||
} `json:"grid"` | ||
Renewable struct { | ||
Grams float64 `json:"grams"` | ||
Litres float64 `json:"litres"` | ||
} `json:"renewable"` | ||
} `json:"co2"` | ||
} `json:"statistics"` | ||
CleanerThan int `json:"cleanerThan"` | ||
ScanUrl string `json:"scanUrl"` | ||
} | ||
|
||
type Carbon struct { | ||
client *http.Client | ||
} | ||
|
||
func NewCarbon(client *http.Client) *Carbon { | ||
return &Carbon{client: client} | ||
} | ||
|
||
// HtmlSize gets the HTML size of the website | ||
func (c *Carbon) HtmlSize(ctx context.Context, url string) (int, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to get HTML size: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
// short cut to avoid reading body into RAM | ||
if resp.ContentLength != -1 { | ||
return int(resp.ContentLength), nil | ||
} | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
return len(body), nil | ||
} | ||
|
||
// CarbonData gets the carbon data based on the HTML size | ||
func (c *Carbon) CarbonData(ctx context.Context, sizeInBytes int) (*CarbonData, error) { | ||
const carbonDataUrl = "https://api.websitecarbon.com/data" | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, carbonDataUrl, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
q := req.URL.Query() | ||
q.Add("bytes", strconv.Itoa(sizeInBytes)) | ||
q.Add("green", "0") | ||
req.URL.RawQuery = q.Encode() | ||
|
||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get carbon data: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
|
||
var carbonData CarbonData | ||
if err := json.Unmarshal(body, &carbonData); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal carbon data: %w", err) | ||
} | ||
|
||
return &carbonData, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xray-web/web-check-api/testutils" | ||
) | ||
|
||
func TestCarbonHtmlSize(t *testing.T) { | ||
t.Parallel() | ||
const htmlBody = `<html><body>Test</body></html>` | ||
var size = len(htmlBody) | ||
client := testutils.MockClient(testutils.Response(http.StatusOK, []byte(htmlBody))) | ||
c := NewCarbon(client) | ||
size, err := c.HtmlSize(context.TODO(), "/carbon") | ||
assert.NoError(t, err) | ||
assert.Equal(t, len(htmlBody), size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package checks | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type Checks struct { | ||
Carbon *Carbon | ||
Rank *Rank | ||
} | ||
|
||
func NewChecks() *Checks { | ||
client := &http.Client{ | ||
Timeout: 5 * time.Second, | ||
} | ||
return &Checks{ | ||
Carbon: NewCarbon(client), | ||
Rank: NewRank(client), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type TrancoRanks struct { | ||
Ranks []TrancoRank `json:"ranks"` | ||
} | ||
|
||
type TrancoRank struct { | ||
Date string `json:"date"` | ||
Rank int `json:"rank"` | ||
} | ||
|
||
type Rank struct { | ||
client *http.Client | ||
} | ||
|
||
func NewRank(client *http.Client) *Rank { | ||
return &Rank{client: client} | ||
} | ||
|
||
func (r *Rank) GetRank(ctx context.Context, url string) (*TrancoRanks, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://tranco-list.eu/api/ranks/domain/%s", url), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := r.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var result TrancoRanks | ||
return &result, json.NewDecoder(resp.Body).Decode(&result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.