Skip to content

Commit

Permalink
refactor(lib, api, cmd): adjust log methods to use dispatch pattern
Browse files Browse the repository at this point in the history
Merge pull request #1730 from qri-io/ramfox/refactor_log_dispatch
  • Loading branch information
ramfox committed Mar 31, 2021
2 parents e17b796 + d83a1cb commit 75b42e1
Show file tree
Hide file tree
Showing 19 changed files with 189 additions and 370 deletions.
11 changes: 4 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,10 @@ func NewServerRoutes(s Server) *mux.Router {
routeParams = newrefRouteParams(lib.AERender, false, false, http.MethodGet, http.MethodPost)
handleRefRoute(m, routeParams, s.Middleware(renderh.RenderHandler))

lh := NewLogHandlers(s.Instance)
routeParams = newrefRouteParams(lib.AEHistory, false, false, http.MethodGet, http.MethodPost)
handleRefRoute(m, routeParams, s.Middleware(lh.LogHandler))
routeParams = newrefRouteParams(lib.AELogbook, false, false, http.MethodGet, http.MethodPost)
handleRefRoute(m, routeParams, s.Middleware(lh.LogbookHandler))
m.Handle(lib.AELogs.String(), s.Middleware(lh.PlainLogsHandler))
m.Handle(lib.AELogbookSummary.String(), s.Middleware(lh.LogbookSummaryHandler))
m.Handle(lib.AEHistory.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "log.history"))).Methods(http.MethodPost)
m.Handle(lib.AEEntries.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "log.entries"))).Methods(http.MethodPost)
m.Handle(lib.AERawLogbook.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "log.rawlogbook"))).Methods(http.MethodPost)
m.Handle(lib.AELogbookSummary.String(), s.Middleware(lib.NewHTTPRequestHandler(s.Instance, "log.logbooksummary"))).Methods(http.MethodPost)

rch := NewRegistryClientHandlers(s.Instance, cfg.API.ReadOnly)
m.Handle(lib.AERegistryNew.String(), s.Middleware(rch.CreateProfileHandler))
Expand Down
1 change: 0 additions & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ func TestServerReadOnlyRoutes(t *testing.T) {
{"GET", "/profile/photo?peername=me", 200},
{"GET", "/profile/poster?peername=me", 200},
{"GET", "/get/peer/movies", 200},
{"GET", "/history/peer/movies", 200},
}

for i, c := range cases {
Expand Down
8 changes: 5 additions & 3 deletions api/fsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestNoHistory(t *testing.T) {
t.Errorf("api response (-want +got):\n%s", diff)
}

logHandler := NewLogHandlers(run.Inst)
historyHandler := lib.NewHTTPRequestHandler(run.Inst, "log.history")

expectNoHistoryBody := `{
"meta": {
Expand All @@ -198,16 +198,18 @@ func TestNoHistory(t *testing.T) {
}`

// History with no history
gotStatusCode, gotBodyString = APICall("/history/peer/test_ds", logHandler.LogHandler, map[string]string{"peername": "peer", "name": "test_ds"})
p := lib.HistoryParams{Ref: "peer/test_ds"}
gotStatusCode, gotBodyString = JSONAPICallWithBody("POST", lib.AEHistory.String(), p, historyHandler, nil)
if gotStatusCode != 422 {
t.Errorf("expected status code 422, got %d", gotStatusCode)
}
if diff := cmp.Diff(expectNoHistoryBody, gotBodyString); diff != "" {
t.Errorf("expected body %v, got %v\ndiff:%v", expectNoHistoryBody, gotBodyString, diff)
}

p.EnsureFSIExists = true
// History with no history, still returns ErrNoHistory since this route ignores fsi param
gotStatusCode, gotBodyString = APICall("/history/peer/test_ds?fsi=true", logHandler.LogHandler, map[string]string{"peername": "peer", "name": "test_ds"})
gotStatusCode, gotBodyString = JSONAPICallWithBody("POST", lib.AEHistory.String(), p, historyHandler, nil)
if gotStatusCode != 422 {
t.Errorf("expected status code 422, got %d", gotStatusCode)
}
Expand Down
144 changes: 0 additions & 144 deletions api/log.go

This file was deleted.

46 changes: 0 additions & 46 deletions api/log_test.go

This file was deleted.

10 changes: 6 additions & 4 deletions api/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
// "math/rand"
"context"
"testing"

"github.com/qri-io/qri/lib"
)

func TestRemoteClientHandlers(t *testing.T) {
Expand All @@ -16,7 +18,7 @@ func TestRemoteClientHandlers(t *testing.T) {
defer cancel()

inst := newTestInstanceWithProfileFromNode(ctx, node)
l := NewLogHandlers(inst)
historyHandler := lib.NewHTTPRequestHandler(inst, "log.history")
h := NewRemoteClientHandlers(inst, false)

publishCases := []handlerTestCase{
Expand All @@ -28,8 +30,8 @@ func TestRemoteClientHandlers(t *testing.T) {

// tests getting a list of logs from a remote
fetchCases := []handlerTestCase{
{"GET", "/history/", nil, nil},
{"GET", "/history/me/cities", nil, nil},
{"POST", "/history/", nil, nil},
{"POST", "/history/me/cities", nil, nil},
}
runHandlerTestCases(t, "fetch", l.LogHandler, fetchCases, true)
runHandlerTestCases(t, "fetch", historyHandler, fetchCases, true)
}
4 changes: 2 additions & 2 deletions api/testdata/api.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -5062,12 +5062,12 @@ HTTP/1.1 200 OK
Connection: close

/* snapshot: TestFSIHandlers init case 0: POST / */
HTTP/1.1 500 Internal Server Error
HTTP/1.1 400 Bad Request
Connection: close

{
"meta": {
"code": 500,
"code": 400,
"error": "dataset name must start with a lower-case letter, and only contain lower-case letters, numbers, dashes, and underscore. Maximum length is 144 characters"
}
}
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)
LogMethods() (*lib.LogMethods, error)
ProfileMethods() (*lib.ProfileMethods, error)
SearchMethods() (*lib.SearchMethods, error)
RenderMethods() (*lib.RenderMethods, error)
Expand Down
5 changes: 0 additions & 5 deletions cmd/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ func (t TestFactory) RegistryClientMethods() (*lib.RegistryClientMethods, error)
return lib.NewRegistryClientMethods(t.inst), nil
}

// LogMethods generates a lib.LogMethods from internal state
func (t TestFactory) LogMethods() (*lib.LogMethods, error) {
return lib.NewLogMethods(t.inst), nil
}

// ProfileMethods generates a lib.ProfileMethods from internal state
func (t TestFactory) ProfileMethods() (*lib.ProfileMethods, error) {
return lib.NewProfileMethods(t.inst), nil
Expand Down
19 changes: 10 additions & 9 deletions cmd/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,21 @@ func TestFetchCommand(t *testing.T) {
lib.OptStdIOStreams(),
lib.OptSetIPFSPath(b.RepoRoot.IPFSPath),
)
localLogHandler := api.NewLogHandlers(localInst)
localLogHandler := lib.NewHTTPRequestHandler(localInst, "log.history")

//
// Validate the outputs of history and fetch
//

remoteLogHandler := api.NewLogHandlers(remoteServer.Instance)
remoteLogHandler := lib.NewHTTPRequestHandler(remoteServer.Instance, "log.history")

// Validates output of history for a remote dataset getting history for a
// dataset in its own namespace in its own repo
actualStatusCode, actualBody := APICall(
"GET",
"/history/peer_a/test_movies",
nil,
remoteLogHandler.LogHandler)
"POST",
"/history",
map[string]string{"refstr": "peer_a/test_movies"},
remoteLogHandler)
if actualStatusCode != 200 {
t.Errorf("expected status code 200, got %d", actualStatusCode)
}
Expand All @@ -177,12 +177,13 @@ func TestFetchCommand(t *testing.T) {

// Validates output of fetching from a remote for a remote dataset
actualStatusCode, actualBody = APICall(
"GET",
"/history/peer_a/test_movies",
"POST",
"/history",
map[string]string{
"refstr": "peer_a/test_movies",
"remote": "a_node",
},
localLogHandler.LogHandler)
localLogHandler)
if actualStatusCode != 200 {
t.Errorf("expected status code 200, got %d", actualStatusCode)
}
Expand Down
Loading

0 comments on commit 75b42e1

Please sign in to comment.