Skip to content

Commit

Permalink
v0.7.1
Browse files Browse the repository at this point in the history
- Fixed `util.Zero` being accidentally inverted, causing it to always be wrong.
- Implemented `Package.LatestVersion` and `PackageVersion.Download` methods.
  • Loading branch information
Owen3H committed Aug 25, 2024
1 parent 54f0a64 commit 3d279d5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion experimental/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions tests/package_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion util/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 8 additions & 9 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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))
}
8 changes: 8 additions & 0 deletions v1/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit 3d279d5

Please sign in to comment.