-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(feed): add feed methods to lib backed by registry
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
util "github.com/qri-io/apiutil" | ||
"github.com/qri-io/dataset" | ||
"github.com/qri-io/qri/lib" | ||
) | ||
|
||
// FeedHandlers provides HTTP API handlers for fetching content | ||
// from the qri network | ||
type FeedHandlers struct { | ||
lib.FeedMethods | ||
} | ||
|
||
// NewFeedHandlers allocates a FeedHandlers pointer | ||
func NewFeedHandlers(inst *lib.Instance) *FeedHandlers { | ||
req := lib.NewFeedMethods(inst) | ||
return &FeedHandlers{*req} | ||
} | ||
|
||
// HomeHandler shows popular content across the network | ||
func (h *FeedHandlers) HomeHandler(w http.ResponseWriter, r *http.Request) { | ||
switch r.Method { | ||
case "OPTIONS": | ||
util.EmptyOkHandler(w, r) | ||
case "GET": | ||
h.homeHandler(w, r) | ||
default: | ||
util.NotFoundHandler(w, r) | ||
} | ||
} | ||
|
||
func (h *FeedHandlers) homeHandler(w http.ResponseWriter, r *http.Request) { | ||
res := map[string][]*dataset.Dataset{} | ||
p := false | ||
if err := h.Home(&p, &res); err != nil { | ||
log.Infof("home error: %s", err.Error()) | ||
util.WriteErrResponse(w, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
util.WriteResponse(w, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package lib | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/qri-io/dataset" | ||
) | ||
|
||
// FeedMethods enapsulates functions for accessing feeds of content from the | ||
// network. For example: a list of popular datasets is a feed. | ||
type FeedMethods struct { | ||
inst *Instance | ||
} | ||
|
||
// NewFeedMethods creates a feed instance | ||
func NewFeedMethods(inst *Instance) *FeedMethods { | ||
m := &FeedMethods{ | ||
inst: inst, | ||
} | ||
|
||
return m | ||
} | ||
|
||
// CoreRequestsName specifies this is a Methods object | ||
func (m *FeedMethods) CoreRequestsName() string { | ||
return "feed" | ||
} | ||
|
||
// Home returns a listing of datasets from a number of feeds like featured and | ||
// popular. Each feed is keyed by string in the response | ||
func (m *FeedMethods) Home(p *bool, res *map[string][]*dataset.Dataset) error { | ||
if m.inst.rpc != nil { | ||
return m.inst.rpc.Call("FeedMethods.Home", p, res) | ||
} | ||
ctx := context.TODO() | ||
|
||
if m.inst.registry == nil { | ||
return fmt.Errorf("Feed isn't available without a configured registry") | ||
} | ||
|
||
feed, err := m.inst.registry.HomeFeed(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*res = feed | ||
return nil | ||
} | ||
|
||
// Featured asks a registry for a curated list of datasets | ||
func (m *FeedMethods) Featured(p *ListParams, res *[]*dataset.Dataset) error { | ||
return fmt.Errorf("featured dataset feed is not yet implemented") | ||
} | ||
|
||
// Recent is a feed of network datasets in reverse chronological order | ||
// it currently can only come from a registry, but could easily be assembled | ||
// via p2p methods | ||
func (m *FeedMethods) Recent(p *ListParams, res *[]*dataset.Dataset) error { | ||
return fmt.Errorf("recent dataset feed is not yet implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters