From 3d279d5302c3d306e6bb13f29e29c65ab026bbde Mon Sep 17 00:00:00 2001 From: O3H Date: Sun, 25 Aug 2024 16:39:40 +0100 Subject: [PATCH] v0.7.1 - Fixed `util.Zero` being accidentally inverted, causing it to always be wrong. - Implemented `Package.LatestVersion` and `PackageVersion.Download` methods. --- experimental/api.go | 2 +- tests/package_v1_test.go | 10 ++++++++++ util/funcs.go | 2 +- util/http.go | 17 ++++++++--------- v1/package.go | 8 ++++++++ 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/experimental/api.go b/experimental/api.go index b36cf46..c1b11ff 100644 --- a/experimental/api.go +++ b/experimental/api.go @@ -52,7 +52,7 @@ func GetCommunity(nameOrId string) (*Community, bool, error) { // // If an error occurred or it was not found, the result will be nil. func GetPackage(author string, name string) (*Package, error) { - endpoint := fmt.Sprint("api/experimental/package/", author, "/", name) + endpoint := fmt.Sprintf("api/experimental/package/%s/%s", author, name) pkg, err := util.JsonGetRequest[Package](endpoint) // Zero value, couldn't find package. diff --git a/tests/package_v1_test.go b/tests/package_v1_test.go index 2535f54..62eb712 100644 --- a/tests/package_v1_test.go +++ b/tests/package_v1_test.go @@ -110,3 +110,13 @@ func TestPackageFilter(t *testing.T) { filtered := pkgs.ExcludeCategories("modpack", "modpacks") fmt.Println(filtered.Size()) } + +func TestDownloadVersion(t *testing.T) { + pkg := comm.GetPackage("Owen3H", "CSync") + if pkg == nil { + t.Fatal(errors.New("error downloading version: package not found")) + } + + data, _ := pkg.LatestVersion().Download() + println(data) +} diff --git a/util/funcs.go b/util/funcs.go index ed9df86..006e4c4 100644 --- a/util/funcs.go +++ b/util/funcs.go @@ -36,7 +36,7 @@ func (dt DateTime) Humanize() string { } func Zero(v interface{}) bool { - return lo.Ternary(v != nil, true, reflect.ValueOf(v).IsZero()) + return lo.Ternary(v != nil, false, reflect.ValueOf(v).IsZero()) } // Prints the interface to STDOUT in a readable way. diff --git a/util/http.go b/util/http.go index 9d87a53..44df115 100644 --- a/util/http.go +++ b/util/http.go @@ -17,7 +17,7 @@ const DOMAIN = "https://thunderstore.io/" var client = resty.NewWithClient(&http.Client{Timeout: REQ_TIMEOUT}) -func post(url string, contentType string, body any) ([]byte, error) { +func Post(url string, contentType string, body any) ([]byte, error) { data, err := json.Marshal(&body) if err != nil { return nil, err @@ -40,16 +40,15 @@ func post(url string, contentType string, body any) ([]byte, error) { return response.Body(), nil } -func get(url string) ([]byte, error) { - response, err := client.R().Get(url) +func Get(url string, contentType string) ([]byte, error) { + response, err := client.R(). + SetHeader("Content-Type", contentType). + Get(url) + if err != nil { return nil, err } - // if !response.IsSuccess() { - // fmt.Println(fmt.Sprint(response.StatusCode(), " ", url)) - // } - return response.Body(), nil } @@ -65,9 +64,9 @@ func asJSON[T interface{}](res []byte, err error) (T, error) { } func JsonGetRequest[T interface{}](endpoint string) (T, error) { - return asJSON[T](get(DOMAIN + endpoint)) + return asJSON[T](Get(DOMAIN+endpoint, "application/json")) } func JsonPostRequest[T interface{}](endpoint string, body any) (T, error) { - return asJSON[T](post(DOMAIN+endpoint, "application/json", body)) + return asJSON[T](Post(DOMAIN+endpoint, "application/json", body)) } diff --git a/v1/package.go b/v1/package.go index 62042e7..733ba53 100644 --- a/v1/package.go +++ b/v1/package.go @@ -101,6 +101,10 @@ type Package struct { Versions []PackageVersion `json:"versions"` } +func (pkg Package) LatestVersion() PackageVersion { + return pkg.Versions[0] +} + // Gets a specific [PackageVersion] from this package's list of versions. // // verNumber should be specified in the format: major.minor.patch @@ -143,6 +147,10 @@ type PackageVersion struct { WebsiteURL string `json:"website_url"` } +func (ver PackageVersion) Download() ([]byte, error) { + return util.Get(ver.DownloadURL, "application/zip") +} + // type PackageVersion struct { // DateCreated Time `json:"date_created"` // Downloads int32 `json:"download_count"`