Skip to content

Commit

Permalink
#69 Proxy service. Add search pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
arturalbov committed Oct 25, 2018
1 parent e99a088 commit bf9eded
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
3 changes: 0 additions & 3 deletions cosmos/poc/app/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import (
)

func (app *CyberdApp) Search(cid string, page, perPage int) ([]RankedCid, int, error) {
if perPage == 0 {
perPage = 100
}
result, totalSize, err := app.memStorage.GetCidRankedLinks(Cid(cid), page, perPage)
if err != nil {
return nil, totalSize, err
Expand Down
9 changes: 7 additions & 2 deletions cosmos/poc/cyberd/rpc/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import "github.com/cybercongress/cyberd/cosmos/poc/app/storage"

type ResultSearch struct {
Cids []storage.RankedCid `json:"cids"`
TotalCount int `json:"totalCount"`
TotalCount int `json:"total"`
Page int `json:"page"`
PerPage int `json:"perPage"`
}

func Search(cid string, page, perPage int) (*ResultSearch, error) {
if perPage == 0 {
perPage = 100
}
links, totalSize, err := cyberdApp.Search(cid, page, perPage)
return &ResultSearch{links, totalSize}, err
return &ResultSearch{links, totalSize, page, perPage}, err
}
6 changes: 3 additions & 3 deletions cosmos/poc/http/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func GetSingleParamValue(name string, r *http.Request) (string, error) {
return param, nil
}

func GetBooleanParamValue(name string, r *http.Request) (bool, error) {
func GetBooleanParamValue(name string, defaultVal bool, r *http.Request) (bool, error) {
paramStr, err := GetSingleParamValue(name, r)
if err != nil {
return false, nil
return defaultVal, nil
}

param, err := strconv.ParseBool(paramStr)
if err != nil {
return false, err
return defaultVal, err
}

return param, nil
Expand Down
6 changes: 3 additions & 3 deletions cosmos/poc/proxy/client/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func StartCmd() *cobra.Command {
mux := http.NewServeMux()
mux.HandleFunc("/link", core.TxHandlerFn(ctx, core.UnmarshalLinkRequest))
mux.HandleFunc("/send", core.TxHandlerFn(ctx, core.UnmarshalSendRequest))
mux.HandleFunc("/search", core.GetWithParamHandlerFn(ctx, "/search", "cid"))
mux.HandleFunc("/account", core.GetWithParamHandlerFn(ctx, "/account", "address"))
mux.HandleFunc("/search", core.GetWithParamsHandlerFn(ctx, "/search", []string{"cid", "page", "perPage"}))
mux.HandleFunc("/account", core.GetWithParamsHandlerFn(ctx, "/account", []string{"address"}))
mux.HandleFunc("/health", core.GetHandlerFn(ctx, "/health"))
mux.HandleFunc("/status", core.GetHandlerFn(ctx, "/status"))

Expand Down Expand Up @@ -59,4 +59,4 @@ func StartCmd() *cobra.Command {

return cmd

}
}
14 changes: 8 additions & 6 deletions cosmos/poc/proxy/core/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ func GetHandlerFn(ctx ProxyContext, endpoint string) func(http.ResponseWriter, *
}
}

func GetWithParamHandlerFn(ctx ProxyContext, endpoint string, param string) func(http.ResponseWriter, *http.Request) {
func GetWithParamsHandlerFn(ctx ProxyContext, endpoint string, params []string) func(http.ResponseWriter, *http.Request) {

return func(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

paramValue, err := util.GetSingleParamValue(param, r)
if err != nil {
util.HandleError(err, w)
return
paramValues := make(url.Values)
for _, param := range params {
value, err := util.GetSingleParamValue(param, r)
if err == nil {
paramValues[param] = []string{"\"" + value + "\""}
}
}

resp, err := ctx.Get(endpoint + "?" + param + "=\"" + url.QueryEscape(paramValue) + "\"")
resp, err := ctx.Get(endpoint + "?" + paramValues.Encode())
if err != nil {
util.HandleError(err, w)
return
Expand Down
2 changes: 1 addition & 1 deletion cosmos/poc/proxy/core/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TxHandlerFn(ctx ProxyContext, unmarshal UnmarshalTxRequest) func(http.Respo
return
}

commit, err := util.GetBooleanParamValue("commit", r)
commit, err := util.GetBooleanParamValue("commit", false, r)
if err != nil {
util.HandleError(err, w)
return
Expand Down

0 comments on commit bf9eded

Please sign in to comment.