Skip to content

Commit

Permalink
refactor(lib): profile follows the dispatch pattern
Browse files Browse the repository at this point in the history
Merge pull request #1736 from qri-io/ramfox/refactor_profile_dispatch
  • Loading branch information
ramfox authored Apr 5, 2021
2 parents 181474a + 345c2b8 commit 3c59dbc
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 666 deletions.
9 changes: 4 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,10 @@ func NewServerRoutes(s Server) *mux.Router {
m.Handle(lib.AEHealth.String(), s.NoLogMiddleware(HealthCheckHandler))
m.Handle(lib.AEIPFS.String(), s.Middleware(s.HandleIPFSPath))

proh := NewProfileHandlers(s.Instance, cfg.API.ReadOnly)
m.Handle(lib.AEMe.String(), s.Middleware(proh.ProfileHandler))
m.Handle(lib.AEProfile.String(), s.Middleware(proh.ProfileHandler))
m.Handle(lib.AEProfilePhoto.String(), s.Middleware(proh.ProfilePhotoHandler))
m.Handle(lib.AEProfilePoster.String(), s.Middleware(proh.PosterHandler))
m.Handle(lib.AEGetProfile.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "profile.getprofile"))).Methods(http.MethodPost)
m.Handle(lib.AESetProfile.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "profile.setprofile"))).Methods(http.MethodPost)
m.Handle(lib.AESetProfilePhoto.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "profile.setprofilePhoto"))).Methods(http.MethodPost)
m.Handle(lib.AESetPosterPhoto.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "profile.setposterphoto"))).Methods(http.MethodPost)

m.Handle(lib.AEPeers.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "peer.list"))).Methods(http.MethodPost)
m.Handle(lib.AEPeer.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "peer.info"))).Methods(http.MethodPost)
Expand Down
8 changes: 0 additions & 8 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,6 @@ func TestServerReadOnlyRoutes(t *testing.T) {
// forbidden endpoints
{"GET", "/ipfs/", 403},
{"GET", "/ipns/", 404},
{"GET", "/profile", 403},
{"POST", "/profile", 403},
{"POST", "/profile/photo", 403},
{"PUT", "/profile/photo", 403},
{"POST", "/profile/poster", 403},
{"PUT", "/profile/poster", 403},
{"POST", "/save", 403},
{"PUT", "/save", 403},
{"POST", "/remove", 403},
Expand All @@ -252,8 +246,6 @@ func TestServerReadOnlyRoutes(t *testing.T) {
// {"GET", "/connect/QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt", 200},
// Cannot test endpoint until we have peers in this test suite
// {"GET", "/peer", 200},
{"GET", "/profile/photo?peername=me", 200},
{"GET", "/profile/poster?peername=me", 200},
{"GET", "/get/peer/movies", 200},
}

Expand Down
108 changes: 0 additions & 108 deletions api/profile.go

This file was deleted.

174 changes: 0 additions & 174 deletions api/profile_test.go

This file was deleted.

1 change: 0 additions & 1 deletion cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func (o *ConfigOptions) CompleteConfig(f Factory) (err error) {
return
}

o.ProfileMethods, err = f.ProfileMethods()
return
}

Expand Down
24 changes: 7 additions & 17 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,13 @@ type ConfigOptions struct {
Concise bool
Output string

inst *lib.Instance
ProfileMethods *lib.ProfileMethods
inst *lib.Instance
}

// Complete adds any missing configuration that can only be added just before calling Run
func (o *ConfigOptions) Complete(f Factory) (err error) {
if o.inst, err = f.Instance(); err != nil {
return
}

o.ProfileMethods, err = f.ProfileMethods()
return
o.inst, err = f.Instance()
return err
}

// Get a configuration option
Expand Down Expand Up @@ -202,7 +197,8 @@ func (o *ConfigOptions) Set(args []string) (err error) {
}

if photoPaths[path] {
if err = setPhotoPath(ctx, o.ProfileMethods, path, args[i+1]); err != nil {
profileMethods := o.inst.Profile()
if err = setPhotoPath(ctx, &profileMethods, path, args[i+1]); err != nil {
if errors.Is(err, lib.ErrUnsupportedRPC) {
return fmt.Errorf("%w - this could mean you're running qri connect in another terminal or application", err)
}
Expand All @@ -228,7 +224,7 @@ func (o *ConfigOptions) Set(args []string) (err error) {
return err
}
if profileChanged {
if _, err = o.ProfileMethods.SaveProfile(ctx, profile); err != nil {
if _, err = o.inst.Profile().SetProfile(ctx, &lib.SetProfileParams{Pro: profile}); err != nil {
if errors.Is(err, lib.ErrUnsupportedRPC) {
return fmt.Errorf("%w - this could mean you're running qri connect in another terminal or application", err)
}
Expand All @@ -241,14 +237,8 @@ func (o *ConfigOptions) Set(args []string) (err error) {
}

func setPhotoPath(ctx context.Context, m *lib.ProfileMethods, proppath, filepath string) error {
f, err := loadFileIfPath(filepath)
if err != nil {
return err
}

p := &lib.FileParams{
Filename: f.Name(),
Data: f,
Filename: filepath,
}

switch proppath {
Expand Down
1 change: 0 additions & 1 deletion cmd/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type Factory interface {

RemoteMethods() (*lib.RemoteMethods, error)
RegistryClientMethods() (*lib.RegistryClientMethods, error)
ProfileMethods() (*lib.ProfileMethods, error)
RenderMethods() (*lib.RenderMethods, error)
}

Expand Down
Loading

0 comments on commit 3c59dbc

Please sign in to comment.