-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from admcsiro/main
Start integrating Search API
- Loading branch information
Showing
5 changed files
with
192 additions
and
4 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,39 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/maxott/magda-cli/pkg/adapter" | ||
"github.com/maxott/magda-cli/pkg/search" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
func init() { | ||
cmd := App().Command("search", "Magda full-text search") | ||
cliSearchDatasets(cmd) | ||
} | ||
|
||
/**** SEARCH DATASETS ****/ | ||
|
||
func cliSearchDatasets(topCmd *kingpin.CmdClause) { | ||
r := &search.DatasetRequest{Offset: -1, Limit: -1} | ||
c := topCmd.Command("datasets", "Fulltext search dataset datasets").Action(func(_ *kingpin.ParseContext) error { | ||
if pyld, err := search.DatasetRaw(context.Background(), r, Adapter(), Logger()); err != nil { | ||
return err | ||
} else { | ||
return adapter.ReplyPrinter(pyld, *useYaml) | ||
} | ||
}) | ||
c.Flag("query", "Full text search query"). | ||
Short('q'). | ||
StringVar(&r.Query) | ||
c.Flag("offset", "Index of first dataset retrieved"). | ||
Short('o'). | ||
IntVar(&r.Offset) | ||
c.Flag("limit", "The maximumm number of datasets to retrieve"). | ||
Short('l'). | ||
IntVar(&r.Limit) | ||
c.Flag("publisher", "Filter search query by names of organisations"). | ||
Short('p'). | ||
StringVar(&r.Publisher) | ||
} |
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
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,86 @@ | ||
package search | ||
|
||
import ( | ||
"encoding/json" | ||
//"fmt" | ||
"context" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/maxott/magda-cli/pkg/adapter" | ||
log "go.uber.org/zap" | ||
) | ||
|
||
/**** DATASETS ****/ | ||
|
||
type DatasetRequest struct { | ||
Query string | ||
Offset int | ||
Limit int | ||
Publisher string | ||
} | ||
|
||
type DatasetResult struct { | ||
HitCount int `json:"hitCount"` | ||
DataSets []struct { | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
Issued string `json:"issued"` | ||
Modified string `json:"modified"` | ||
Languages []string `json:"languages"` | ||
Publisher string `json:"publisher"` | ||
AccrualPeriodicity string `json:"accrualPeriodicity"` | ||
AccrualPeriodicityRecurrenceRule string `json:"accrualPeriodicityRecurrenceRule"` | ||
Themes []string `json:"themes"` | ||
Keywords []string `json:"keywords"` | ||
ContactPoint string `json:"contactPoint"` | ||
LandingPage string `json:"landingPage"` | ||
DefaultLicense string `json:"defaultLicense"` | ||
} `json:"dataSets"` | ||
} | ||
|
||
func Dataset(ctxt context.Context, cmd *DatasetRequest, adpt *adapter.Adapter, logger *log.Logger) (DatasetResult, error) { | ||
pyl, err := DatasetRaw(ctxt, cmd, adpt, logger) | ||
if err != nil { | ||
return DatasetResult{}, err | ||
} | ||
res := DatasetResult{} | ||
_ = json.Unmarshal(pyl.AsBytes(), &res) | ||
return res, nil | ||
} | ||
|
||
func DatasetRaw(ctxt context.Context, cmd *DatasetRequest, adpt *adapter.Adapter, logger *log.Logger) (adapter.Payload, error) { | ||
path := searchPath(nil, adpt) | ||
|
||
q := []string{} | ||
|
||
if cmd.Query != "" { | ||
q = append(q, "query="+url.QueryEscape(cmd.Query)) | ||
} | ||
if cmd.Offset >= 0 { | ||
q = append(q, "start="+url.QueryEscape(strconv.Itoa(cmd.Offset))) | ||
} | ||
if cmd.Limit >= 0 { | ||
q = append(q, "limit="+url.QueryEscape(strconv.Itoa(cmd.Limit))) | ||
} | ||
if cmd.Publisher != "" { | ||
q = append(q, "publisher="+url.QueryEscape(cmd.Publisher)) | ||
} | ||
|
||
if len(q) > 0 { | ||
path = path + "?" + strings.Join(q, "&") | ||
} | ||
|
||
return (*adpt).Get(ctxt, path, logger) | ||
} | ||
|
||
/**** UTILS ****/ | ||
|
||
func searchPath(id *string, adpt *adapter.Adapter) string { | ||
path := "/api/v0/search/datasets" | ||
if id != nil { | ||
path = path + "/" + *id | ||
} | ||
return path | ||
} |