Skip to content

Commit

Permalink
fix(server): marketplace http client
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Sep 8, 2022
1 parent 6e1d502 commit ce982d9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
23 changes: 10 additions & 13 deletions server/internal/infrastructure/marketplace/marketplace.go
Expand Up @@ -7,7 +7,6 @@ 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 All @@ -17,21 +16,13 @@ var pluginPackageSizeLimit int64 = 10 * 1024 * 1024 // 10MB

type Marketplace struct {
endpoint string
client *http.Client
conf *clientcredentials.Config
}

func New(endpoint string, conf *clientcredentials.Config) *Marketplace {
var client *http.Client
if conf != nil {
client = conf.Client(context.Background())
}
if client == nil {
client = http.DefaultClient
}

return &Marketplace{
endpoint: strings.TrimSuffix(endpoint, "/"),
client: client,
conf: conf,
}
}

Expand Down Expand Up @@ -103,9 +94,15 @@ type plugin struct {
*/

func (m *Marketplace) downloadPluginPackage(ctx context.Context, url string) (*pluginpack.Package, error) {
log.Infof("marketplace: download plugin from \"%s\"", url)
var client *http.Client
if m.conf != nil {
client = m.conf.Client(ctx)
}
if client == nil {
client = http.DefaultClient
}

res, err := m.client.Get(url)
res, err := client.Get(url)
if err != nil {
return nil, rerror.ErrInternalBy(err)
}
Expand Down
4 changes: 3 additions & 1 deletion server/internal/usecase/interactor/plugin_upload.go
Expand Up @@ -173,7 +173,9 @@ func (i *Plugin) installScenePlugin(ctx context.Context, p *pluginpack.Package,
}
}

s.Plugins().Add(scene.NewPlugin(p.Manifest.Plugin.ID(), ppid))
if !s.Plugins().Add(scene.NewPlugin(p.Manifest.Plugin.ID(), ppid)) {
return interfaces.ErrPluginAlreadyInstalled
}

if pp != nil {
if err := i.propertyRepo.Save(ctx, pp); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion server/internal/usecase/interactor/scene_plugin.go
Expand Up @@ -60,7 +60,9 @@ func (i *Scene) InstallPlugin(ctx context.Context, sid id.SceneID, pid id.Plugin
}
}

s.Plugins().Add(scene.NewPlugin(pid, p.IDRef()))
if !s.Plugins().Add(scene.NewPlugin(pid, p.IDRef())) {
return nil, nil, interfaces.ErrPluginAlreadyInstalled
}

if p != nil {
if err := i.propertyRepo.Save(ctx, p); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions server/pkg/scene/plugins.go
Expand Up @@ -67,11 +67,12 @@ func (p *Plugins) HasPluginByName(name string) bool {
return false
}

func (p *Plugins) Add(sp *Plugin) {
func (p *Plugins) Add(sp *Plugin) bool {
if sp == nil || p.HasPluginByName(sp.plugin.Name()) || sp.plugin.Equal(OfficialPluginID) {
return
return false
}
p.plugins = append(p.plugins, sp)
return true
}

func (p *Plugins) Remove(pid PluginID) {
Expand Down
7 changes: 6 additions & 1 deletion server/pkg/scene/plugins_test.go
Expand Up @@ -317,38 +317,43 @@ func TestPlugins_Add(t *testing.T) {
Name string
Input *Plugin
PS, Expected *Plugins
Want bool
}{
{
Name: "add nil plugin",
Input: nil,
PS: NewPlugins([]*Plugin{NewPlugin(pid, pr)}),
Expected: NewPlugins([]*Plugin{NewPlugin(pid, pr)}),
Want: false,
},
{
Name: "add existing plugin",
Input: NewPlugin(pid, pr),
PS: NewPlugins([]*Plugin{NewPlugin(pid, pr)}),
Expected: NewPlugins([]*Plugin{NewPlugin(pid, pr)}),
Want: false,
},
{
Name: "add official plugin",
Input: NewPlugin(OfficialPluginID, pr),
PS: NewPlugins([]*Plugin{NewPlugin(pid, pr)}),
Expected: NewPlugins([]*Plugin{NewPlugin(pid, pr)}),
Want: false,
},
{
Name: "add new plugin",
Input: NewPlugin(pid, pr),
PS: NewPlugins([]*Plugin{}),
Expected: NewPlugins([]*Plugin{NewPlugin(pid, pr)}),
Want: true,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
tc.PS.Add(tc.Input)
assert.Equal(t, tc.Want, tc.PS.Add(tc.Input))
assert.Equal(t, tc.Expected, tc.PS)
})
}
Expand Down

0 comments on commit ce982d9

Please sign in to comment.