Skip to content

Commit

Permalink
fix(client.go): correctly fetch build number info
Browse files Browse the repository at this point in the history
The endpoint listed for build number data is currently broken and never updates. Using this
workaround should allow the tool to check for build number changes so it can update the cache, but
also when the endpoint is no longer broken, be easily removed.
  • Loading branch information
deadpyxel committed Aug 6, 2024
1 parent ae990f8 commit 03de7be
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
43 changes: 41 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -93,10 +94,48 @@ func (client *APIClient) fetchAndDecode(endpoint string, targetType interface{})
return nil
}

func fetchBuildNumberData(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

return string(body), nil
}

func parseBuildNumber(data string) (int, error) {
if len(data) == 0 {
return 0, errors.New("Cannot parse empty build number data")
}
parts := strings.Split(data, " ")
if len(parts) == 0 {
return 0, errors.New("No build number data available")
}

firstNum, err := strconv.Atoi(parts[0])
if err != nil {
return 0, err
}
return firstNum, nil
}

func (client *APIClient) FetchBuildNumber() (Metadata, error) {
endpoint := "/build"
var metadata Metadata
err := client.fetchAndDecode(endpoint, &metadata)
buildNumberData, err := fetchBuildNumberData("http://assetcdn.101.arenanetworks.com/latest/101")
if err != nil {
return metadata, err
}
newBuild, err := parseBuildNumber(buildNumberData)
if err != nil {
return metadata, err
}
metadata.BuildNumber = newBuild
return metadata, err
}

Expand Down
31 changes: 31 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"errors"
"testing"
)

func TestParseBuildNumber(t *testing.T) {
tests := []struct {
name string
input string
expected int
err error
}{
{"Valid input is parsed with no errors", "10", 10, nil},
{"Empty input data returns zero and an error", "", 0, errors.New("Cannot parse empty build number data")},
{"Invalid input returns zero and an error", "abc", 0, errors.New("strconv.Atoi: parsing \"abc\": invalid syntax")},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseBuildNumber(tt.input)
if result != tt.expected {
t.Errorf("Expected %d, but got %d", tt.expected, result)
}
if err != nil && err.Error() != tt.err.Error() {
t.Errorf("Expected error %v, but got %v", tt.err, err)
}
})
}
}

0 comments on commit 03de7be

Please sign in to comment.