Skip to content

Commit 403ac00

Browse files
committed
implement SubmitPackage
consider simplifying this in future
1 parent 9ae60cd commit 403ac00

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

experimental/package.go

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ type Package struct {
2222
Latest PackageVersion `json:"latest"`
2323
}
2424

25+
type PackageCategory struct {
26+
Name string `json:"name"`
27+
Slug string `json:"slug"`
28+
}
29+
2530
type PackageVersion struct {
2631
Namespace string `json:"namespace"`
2732
Name string `json:"name"`

experimental/submission.go

+31-11
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ import (
1515
"github.com/the-egg-corp/thundergo/util"
1616
)
1717

18+
const SUBMIT_ENDPOINT = "api/experimental/submission/submit"
19+
1820
const MAX_MARKDOWN_SIZE = 1000 * 100
1921
const MAX_ICON_SIZE = 1024 * 1024 * 6
2022

23+
type ManifestMetadata struct {
24+
Name string `json:"name"`
25+
VersionNumber string `json:"version_number"`
26+
WebsiteURL *string `json:"website_url"`
27+
Description string `json:"description"`
28+
Dependencies []string `json:"dependencies"`
29+
}
30+
2131
type PackageSubmissionMetadata struct {
2232
UUID string `json:"upload_uuid"`
2333
Author string `json:"author_name"`
@@ -27,18 +37,28 @@ type PackageSubmissionMetadata struct {
2737
HasNsfwContent bool `json:"has_nsfw_content"`
2838
}
2939

30-
type ManifestMetadata struct {
31-
Name string `json:"name"`
32-
VersionNumber string `json:"version_number"`
33-
WebsiteURL *string `json:"website_url"`
34-
Description string `json:"description"`
35-
Dependencies []string `json:"dependencies"`
40+
type AvailableCommunity struct {
41+
Community Community `json:"community"`
42+
Categories []PackageCategory `json:"categories"`
43+
URL string `json:"url"`
44+
}
45+
46+
type PackageSubmissionResult struct {
47+
PackageVersion PackageVersion `json:"package_version"`
48+
AvailableCommunities []AvailableCommunity `json:"available_communities"`
3649
}
3750

38-
// TODO: Implement this. Should take an auth key which the user gathers from 'Service Accounts'
39-
// func SubmitPackage(data []byte) (bool, error) {
40-
// return false, nil
41-
// }
51+
// Submits a package to Thunderstore given the zip file as bytes.
52+
//
53+
// An API key can be gathered via Settings -> Service Accounts. It is up to you to store and pass it safely.
54+
func SubmitPackage(authKey string, metadata PackageSubmissionMetadata) (*PackageSubmissionResult, error) {
55+
res, err := util.JsonPostRequest[PackageSubmissionMetadata, PackageSubmissionResult](SUBMIT_ENDPOINT, metadata)
56+
if err != nil {
57+
return nil, errors.New("error sending submission:\n" + err.Error())
58+
}
59+
60+
return &res, nil
61+
}
4262

4363
func ValidateReadme(data []byte) (bool, error) {
4464
if !utf8.Valid(data) {
@@ -120,7 +140,7 @@ func ValidateManifest(author string, data []byte) (valid bool, errs []string, er
120140
// - Is in the PNG format.
121141
//
122142
// - Dimensions match 256x256.
123-
func ValidateIcon(fileName string, data []byte) (bool, error) {
143+
func ValidateIcon(data []byte) (bool, error) {
124144
// Check bytes dont exceed
125145
if len(data) > MAX_ICON_SIZE {
126146
return false, errors.New("invalid icon: max file size is 6MB")

tests/exp/submission_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tests
33
import (
44
"fmt"
55
"os"
6-
"path/filepath"
76
"testing"
87

98
TSGO "github.com/the-egg-corp/thundergo/experimental"
@@ -21,7 +20,7 @@ func TestValidateIcon(t *testing.T) {
2120
t.Fatal(err.Error())
2221
}
2322

24-
valid, err := TSGO.ValidateIcon(filepath.Base(iconPath), icon)
23+
valid, err := TSGO.ValidateIcon(icon)
2524
if err != nil {
2625
t.Fatal(err.Error())
2726
}

util/http.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ func asJSON[T interface{}](res []byte, err error) (T, error) {
6363
return data, nil
6464
}
6565

66-
func JsonGetRequest[T interface{}](endpoint string) (T, error) {
67-
return asJSON[T](Get(DOMAIN+endpoint, "application/json"))
66+
func JsonGetRequest[Response interface{}](endpoint string) (Response, error) {
67+
return asJSON[Response](Get(DOMAIN+endpoint, "application/json"))
6868
}
6969

70-
func JsonPostRequest[T interface{}](endpoint string, body any) (T, error) {
71-
return asJSON[T](Post(DOMAIN+endpoint, "application/json", body))
70+
func JsonPostRequest[Body interface{}, Response interface{}](endpoint string, body Body) (Response, error) {
71+
return asJSON[Response](Post(DOMAIN+endpoint, "application/json", body))
7272
}

0 commit comments

Comments
 (0)