Skip to content

Commit

Permalink
implement SubmitPackage
Browse files Browse the repository at this point in the history
consider simplifying this in future
  • Loading branch information
Owen3H committed Dec 19, 2024
1 parent 9ae60cd commit 403ac00
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
5 changes: 5 additions & 0 deletions experimental/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
42 changes: 31 additions & 11 deletions experimental/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions tests/exp/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tests
import (
"fmt"
"os"
"path/filepath"
"testing"

TSGO "github.com/the-egg-corp/thundergo/experimental"
Expand All @@ -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())
}
Expand Down
8 changes: 4 additions & 4 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 403ac00

Please sign in to comment.