Skip to content

Commit

Permalink
metrics, docs & some qol
Browse files Browse the repository at this point in the history
- more v1 documentation
- added Package.Metrics() with a test (passing)
- fixed GetPackage not working
- Package.GetVersion now uses tryFind
- GetCommunity name/id check replaced with EqualFold
  • Loading branch information
Owen3H committed Apr 8, 2024
1 parent 21f1c5d commit f82f622
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 39 deletions.
5 changes: 4 additions & 1 deletion experimental/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package experimental
import (
"encoding/base64"
"fmt"
"strings"

"github.com/the-egg-corp/thundergo/util"

"github.com/samber/lo"
Expand All @@ -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()

Expand All @@ -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)
})
}

Expand Down
2 changes: 2 additions & 0 deletions experimental/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/the-egg-corp/thundergo/util"
)

//var commCache CommunityList

type Category = common.PackageCategory

type CommunityList struct {
Expand Down
10 changes: 1 addition & 9 deletions experimental/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions tests/community_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -26,3 +28,5 @@ func TestCommunitiesExp(t *testing.T) {

util.PrettyPrint(comms.Results)
}

//endregion
14 changes: 14 additions & 0 deletions tests/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions util/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
11 changes: 9 additions & 2 deletions v1/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
47 changes: 20 additions & 27 deletions v1/package.go
Original file line number Diff line number Diff line change
@@ -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"
)

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

0 comments on commit f82f622

Please sign in to comment.