Skip to content

Commit

Permalink
fix(server): print marketplace donwload url
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Sep 8, 2022
1 parent 7528727 commit 6e1d502
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 6 additions & 3 deletions server/internal/infrastructure/marketplace/marketplace.go
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearth/server/pkg/log"
"github.com/reearth/reearth/server/pkg/plugin/pluginpack"
"github.com/reearth/reearthx/rerror"
"golang.org/x/oauth2/clientcredentials"
Expand Down Expand Up @@ -35,14 +36,14 @@ func New(endpoint string, conf *clientcredentials.Config) *Marketplace {
}

func (m *Marketplace) FetchPluginPackage(ctx context.Context, pid id.PluginID) (*pluginpack.Package, error) {
purl, err := m.getPluginURL(ctx, pid)
purl, err := m.getPluginURL(pid)
if err != nil {
return nil, err
}
return m.downloadPluginPackage(ctx, purl)
}

func (m *Marketplace) getPluginURL(_ context.Context, pid id.PluginID) (string, error) {
func (m *Marketplace) getPluginURL(pid id.PluginID) (string, error) {
return fmt.Sprintf("%s/api/plugins/%s/%s.zip", m.endpoint, pid.Name(), pid.Version().String()), nil
}

Expand Down Expand Up @@ -102,6 +103,8 @@ type plugin struct {
*/

func (m *Marketplace) downloadPluginPackage(ctx context.Context, url string) (*pluginpack.Package, error) {
log.Infof("marketplace: download plugin from \"%s\"", url)

res, err := m.client.Get(url)
if err != nil {
return nil, rerror.ErrInternalBy(err)
Expand All @@ -113,7 +116,7 @@ func (m *Marketplace) downloadPluginPackage(ctx context.Context, url string) (*p
return nil, rerror.ErrNotFound
}
if res.StatusCode != http.StatusOK {
return nil, rerror.ErrInternalBy(fmt.Errorf("status code is %s", res.Status))
return nil, rerror.ErrInternalBy(fmt.Errorf("status code is %d", res.StatusCode))
}
return pluginpack.PackageFromZip(res.Body, nil, pluginPackageSizeLimit)
}
11 changes: 11 additions & 0 deletions server/internal/infrastructure/marketplace/marketplace_test.go
Expand Up @@ -135,3 +135,14 @@ func TestMarketplace_FetchPluginPackage_NoAuth(t *testing.T) {
// no need to test pluginpack in detail here
assert.Equal(t, id.MustPluginID("testplugin~1.0.1"), got.Manifest.Plugin.ID())
}

func TestMarketplace_GetPluginURL(t *testing.T) {
pid := id.MustPluginID("aaaabbbxxxbb~1.0.0")
u, err := (&Marketplace{
endpoint: "https://xxxxx",
}).getPluginURL(pid)
assert.NoError(t, err)
assert.Equal(t, "https://xxxxx/api/plugins/aaaabbbxxxbb/1.0.0.zip", u)
_, err = url.Parse(u)
assert.NoError(t, err)
}

0 comments on commit 6e1d502

Please sign in to comment.