diff --git a/experimental/api.go b/experimental/api.go index 1a8821f..8202bc5 100644 --- a/experimental/api.go +++ b/experimental/api.go @@ -3,6 +3,8 @@ package experimental import ( "encoding/base64" "fmt" + "strings" + "github.com/the-egg-corp/thundergo/util" "github.com/samber/lo" @@ -18,6 +20,7 @@ func GetCommunities() (CommunityList, error) { return util.JsonGetRequest[CommunityList]("api/experimental/community") } +// Get a specific [Community] by it's identifier or short name. func GetCommunity(nameOrId string) (*Community, bool) { communities, err := GetCommunities() @@ -26,7 +29,7 @@ func GetCommunity(nameOrId string) (*Community, bool) { } return lo.Find(communities.Results, func(comm *Community) bool { - return comm.Name == nameOrId || comm.Identifier == nameOrId + return strings.EqualFold(comm.Name, nameOrId) || strings.EqualFold(comm.Identifier, nameOrId) }) } diff --git a/experimental/community.go b/experimental/community.go index 8c3fae7..3986f7c 100644 --- a/experimental/community.go +++ b/experimental/community.go @@ -6,6 +6,8 @@ import ( "github.com/the-egg-corp/thundergo/util" ) +//var commCache CommunityList + type Category = common.PackageCategory type CommunityList struct { diff --git a/experimental/validation.go b/experimental/validation.go index fc096e9..e6b403d 100644 --- a/experimental/validation.go +++ b/experimental/validation.go @@ -23,15 +23,7 @@ func ValidateManifest() (ValidatorResponse, error) { return ValidatorResponse{}, nil } -// func ValidateIcon(data Base64String) (ValidatorResponse, error) { -// body := IconValidatorParams{ -// IconData: data.String(), -// } - -// endpoint := "api/experimental/submission/validate/icon" -// return util.JsonPostRequest[ValidatorResponse](endpoint, body) -// } - +// Decodes image data and validates that the image is a PNG and the dimensions are 256x256. func ValidateIcon(data []byte) (bool, error) { // Decode the image img, _, err := image.Decode(bytes.NewReader(data)) diff --git a/tests/community_test.go b/tests/community_test.go index 396b1ef..66b428d 100644 --- a/tests/community_test.go +++ b/tests/community_test.go @@ -3,9 +3,11 @@ package tests import ( TSGO "github.com/the-egg-corp/thundergo/experimental" "github.com/the-egg-corp/thundergo/util" + //TSGOV1 "github.com/the-egg-corp/thundergo/v1" "testing" ) +// region Experimental Tests func TestCommunityExp(t *testing.T) { comm, found := TSGO.GetCommunity("lethal-company") @@ -26,3 +28,5 @@ func TestCommunitiesExp(t *testing.T) { util.PrettyPrint(comms.Results) } + +//endregion diff --git a/tests/package_test.go b/tests/package_test.go index 585ab13..109a5f5 100644 --- a/tests/package_test.go +++ b/tests/package_test.go @@ -109,4 +109,18 @@ func TestPackageGetByUUID(t *testing.T) { util.PrettyPrint(pkg) } +func TestMetrics(t *testing.T) { + pkgs, err := TSGOV1.GetAllPackages() + if err != nil { + t.Fatal(err.Error()) + } + + metrics, err := pkgs.Get("Owen3H", "CSync").Metrics() + if err != nil { + t.Fatal(err) + } + + util.PrettyPrint(metrics) +} + //endregion diff --git a/util/funcs.go b/util/funcs.go index d9ce8f4..b9dbae7 100644 --- a/util/funcs.go +++ b/util/funcs.go @@ -7,6 +7,7 @@ import ( "github.com/sanity-io/litter" ) +// An alias for [time.Time] that is correctly unmarshalled from JSON. type DateTime struct { time.Time } @@ -23,6 +24,7 @@ func (t *DateTime) UnmarshalJSON(b []byte) error { return nil } +// Prints the interface to STDOUT in a readable way. func PrettyPrint(i interface{}) { litter.Config.StripPackageNames = true litter.Dump(i) diff --git a/v1/api.go b/v1/api.go index df7a84d..6ea7ee6 100644 --- a/v1/api.go +++ b/v1/api.go @@ -4,6 +4,7 @@ import ( "github.com/the-egg-corp/thundergo/util" ) +// The list of every package on Thunderstore across every community. func GetAllPackages() (PackageList, error) { return util.JsonGetRequest[PackageList]("api/v1/package") } diff --git a/v1/community.go b/v1/community.go index 007d7b5..398ea29 100644 --- a/v1/community.go +++ b/v1/community.go @@ -9,7 +9,7 @@ type Community struct { Identifier string } -// Returns a list of all packages (mods) within this Community. +// Returns a list of all packages (mods) within this community. func (comm Community) AllPackages(predicate ...func(item Package, index int) bool) (PackageList, error) { endpoint := fmt.Sprint("c/", comm.Identifier, "/api/v1/package") pkgs, err := util.JsonGetRequest[PackageList](endpoint) @@ -22,10 +22,17 @@ func (comm Community) AllPackages(predicate ...func(item Package, index int) boo return pkgs, nil } +// Gets a single package from this community given the owner and package name. func (comm Community) GetPackage(author string, name string) *Package { if pkgCache != nil { return pkgCache.Get(author, name) } - return nil + pkgs, err := comm.AllPackages() + + if err != nil { + return nil + } + + return pkgs.Get(author, name) } diff --git a/v1/package.go b/v1/package.go index 1c1fcb1..c52d4cd 100644 --- a/v1/package.go +++ b/v1/package.go @@ -1,9 +1,11 @@ package v1 import ( - "github.com/the-egg-corp/thundergo/util" + "fmt" "strings" + "github.com/the-egg-corp/thundergo/util" + "github.com/samber/lo" ) @@ -30,21 +32,21 @@ func (list PackageList) Filter(predicate func(pkg Package) bool) PackageList { return arr } -func (list PackageList) tryFind(pred func(pkg Package) bool) *Package { - pkg, found := lo.Find(list, pred) +func tryFind[T any](arr []T, pred func(pkg T) bool) *T { + pkg, found := lo.Find(arr, pred) return lo.Ternary(found, &pkg, nil) } // Grab a single package from the list given the package owner's name and the package's short name. func (list PackageList) Get(author string, name string) *Package { - return list.tryFind(func(p Package) bool { + return tryFind(list, func(p Package) bool { return strings.EqualFold(p.Name, name) && strings.EqualFold(p.Owner, author) }) } // Grab a single package from the list given the package owner's name and the package's short name. func (list PackageList) GetByUUID(uuid string) *Package { - return list.tryFind(func(p Package) bool { + return tryFind(list, func(p Package) bool { return strings.EqualFold(p.UUID, uuid) }) } @@ -55,11 +57,14 @@ func (list PackageList) GetByUUID(uuid string) *Package { // // "Owen3H-CSync" func (list PackageList) GetExact(fullName string) *Package { - return list.tryFind(func(p Package) bool { + return tryFind(list, func(p Package) bool { return strings.EqualFold(p.FullName, fullName) }) } +// Represents a package/mod on Thunderstore that is global and not specific to any community. +// +// To easily find a version from Versions, use [Package.GetVersion]. type Package struct { Name string `json:"name"` FullName string `json:"full_name"` @@ -89,32 +94,20 @@ type Package struct { // // "v3.1", "v2", "1.0" func (pkg Package) GetVersion(verNumber string) *PackageVersion { - ver, found := lo.Find(pkg.Versions, func(v PackageVersion) bool { + return tryFind(pkg.Versions, func(v PackageVersion) bool { return strings.EqualFold(v.VersionNumber, strings.Replace(verNumber, "v", "", 1)) }) - - if !found { - return nil - } - - return &ver } -// type CommunityPackage struct { -// Community string `json:"community"` -// Package -// } - -// func (pkg CommunityPackage) Metrics() (PackageMetrics, error) { -// endpoint := fmt.Sprint("c/", pkg.Community, "/api/v1/package-metrics/", pkg.Owner, "/", pkg.Name) -// return util.JsonGetRequest[PackageMetrics](endpoint) -// } - -// func (pkg CommunityPackage) VersionMetrics(version string) (PackageVersionMetrics, error) { -// endpoint := fmt.Sprint("c/", pkg.Community, "/api/v1/package-metrics/", pkg.Owner, "/", pkg.Name, "/", pkg.Versions[0]) -// return util.JsonGetRequest[PackageVersionMetrics](endpoint) -// } +// Gets this package's statistics such as downloads and likes. +func (pkg Package) Metrics(version ...string) (PackageMetrics, error) { + endpoint := fmt.Sprint("api/v1/package-metrics/", pkg.Owner, "/", pkg.Name) + return util.JsonGetRequest[PackageMetrics](endpoint) +} +// A specific version of a package. +// +// Note: This is NOT equivalent to [Package] as its fields differ. type PackageVersion struct { Name string `json:"name"` FullName string `json:"full_name"` diff --git a/v1/user.go b/v1/user.go index f68f316..50f04b2 100644 --- a/v1/user.go +++ b/v1/user.go @@ -11,12 +11,14 @@ type UserMedia struct { Status []string `json:"status"` } +// Represents a user within a Thunderstore team. type UserTeam struct { Name string `json:"name"` Role string `json:"role"` MemberCount uint8 `json:"member_count"` } +// Represents the profile of a Thunderstore user. type UserProfile struct { Username string `json:"username"` Capabilities []*string `json:"capabilities"`