From 403ac00e19e569be14c1053ce7d7776ddcefddbd Mon Sep 17 00:00:00 2001 From: O3H Date: Thu, 19 Dec 2024 13:43:05 +0000 Subject: [PATCH] implement `SubmitPackage` consider simplifying this in future --- experimental/package.go | 5 +++++ experimental/submission.go | 42 ++++++++++++++++++++++++++---------- tests/exp/submission_test.go | 3 +-- util/http.go | 8 +++---- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/experimental/package.go b/experimental/package.go index 66cf0f1..e3d3d44 100644 --- a/experimental/package.go +++ b/experimental/package.go @@ -22,6 +22,11 @@ type Package struct { Latest PackageVersion `json:"latest"` } +type PackageCategory struct { + Name string `json:"name"` + Slug string `json:"slug"` +} + type PackageVersion struct { Namespace string `json:"namespace"` Name string `json:"name"` diff --git a/experimental/submission.go b/experimental/submission.go index a929598..63286e6 100644 --- a/experimental/submission.go +++ b/experimental/submission.go @@ -15,9 +15,19 @@ import ( "github.com/the-egg-corp/thundergo/util" ) +const SUBMIT_ENDPOINT = "api/experimental/submission/submit" + const MAX_MARKDOWN_SIZE = 1000 * 100 const MAX_ICON_SIZE = 1024 * 1024 * 6 +type ManifestMetadata struct { + Name string `json:"name"` + VersionNumber string `json:"version_number"` + WebsiteURL *string `json:"website_url"` + Description string `json:"description"` + Dependencies []string `json:"dependencies"` +} + type PackageSubmissionMetadata struct { UUID string `json:"upload_uuid"` Author string `json:"author_name"` @@ -27,18 +37,28 @@ type PackageSubmissionMetadata struct { HasNsfwContent bool `json:"has_nsfw_content"` } -type ManifestMetadata struct { - Name string `json:"name"` - VersionNumber string `json:"version_number"` - WebsiteURL *string `json:"website_url"` - Description string `json:"description"` - Dependencies []string `json:"dependencies"` +type AvailableCommunity struct { + Community Community `json:"community"` + Categories []PackageCategory `json:"categories"` + URL string `json:"url"` +} + +type PackageSubmissionResult struct { + PackageVersion PackageVersion `json:"package_version"` + AvailableCommunities []AvailableCommunity `json:"available_communities"` } -// TODO: Implement this. Should take an auth key which the user gathers from 'Service Accounts' -// func SubmitPackage(data []byte) (bool, error) { -// return false, nil -// } +// Submits a package to Thunderstore given the zip file as bytes. +// +// An API key can be gathered via Settings -> Service Accounts. It is up to you to store and pass it safely. +func SubmitPackage(authKey string, metadata PackageSubmissionMetadata) (*PackageSubmissionResult, error) { + res, err := util.JsonPostRequest[PackageSubmissionMetadata, PackageSubmissionResult](SUBMIT_ENDPOINT, metadata) + if err != nil { + return nil, errors.New("error sending submission:\n" + err.Error()) + } + + return &res, nil +} func ValidateReadme(data []byte) (bool, error) { if !utf8.Valid(data) { @@ -120,7 +140,7 @@ func ValidateManifest(author string, data []byte) (valid bool, errs []string, er // - Is in the PNG format. // // - Dimensions match 256x256. -func ValidateIcon(fileName string, data []byte) (bool, error) { +func ValidateIcon(data []byte) (bool, error) { // Check bytes dont exceed if len(data) > MAX_ICON_SIZE { return false, errors.New("invalid icon: max file size is 6MB") diff --git a/tests/exp/submission_test.go b/tests/exp/submission_test.go index 3d1f358..3ea9756 100644 --- a/tests/exp/submission_test.go +++ b/tests/exp/submission_test.go @@ -3,7 +3,6 @@ package tests import ( "fmt" "os" - "path/filepath" "testing" TSGO "github.com/the-egg-corp/thundergo/experimental" @@ -21,7 +20,7 @@ func TestValidateIcon(t *testing.T) { t.Fatal(err.Error()) } - valid, err := TSGO.ValidateIcon(filepath.Base(iconPath), icon) + valid, err := TSGO.ValidateIcon(icon) if err != nil { t.Fatal(err.Error()) } diff --git a/util/http.go b/util/http.go index 44df115..e3aa27b 100644 --- a/util/http.go +++ b/util/http.go @@ -63,10 +63,10 @@ func asJSON[T interface{}](res []byte, err error) (T, error) { return data, nil } -func JsonGetRequest[T interface{}](endpoint string) (T, error) { - return asJSON[T](Get(DOMAIN+endpoint, "application/json")) +func JsonGetRequest[Response interface{}](endpoint string) (Response, error) { + return asJSON[Response](Get(DOMAIN+endpoint, "application/json")) } -func JsonPostRequest[T interface{}](endpoint string, body any) (T, error) { - return asJSON[T](Post(DOMAIN+endpoint, "application/json", body)) +func JsonPostRequest[Body interface{}, Response interface{}](endpoint string, body Body) (Response, error) { + return asJSON[Response](Post(DOMAIN+endpoint, "application/json", body)) }