FEAT: Swicthed QUERY Method To GET
Build, Push and Deploy Discovery / build-push-deploy (push) Successful in 5m2s
Build, Push and Deploy Discovery / build-push-deploy (push) Successful in 5m2s
This commit is contained in:
+1
-1
@@ -77,7 +77,7 @@ Both steps of the upload flow, end to end:
|
||||
not there at all, and a video announced a second time is updated rather than
|
||||
colliding with its own row
|
||||
|
||||
- searching that catalogue over `QUERY /api/videos`: finding a video by a word
|
||||
- searching that catalogue over `GET /api/videos`: finding a video by a word
|
||||
in its title and by a part-typed one, narrowing to a category and being left
|
||||
out of another, several categories meaning "any of", paging by cursor so the
|
||||
pages tile the results exactly once, refusing a cursor no search issued, and
|
||||
|
||||
+38
-15
@@ -6,7 +6,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -139,15 +141,37 @@ type catalogueVideoBody struct {
|
||||
Categories []string `json:"categories"`
|
||||
}
|
||||
|
||||
// searchRequest is the body of QUERY /api/videos on discovery: what a reader
|
||||
// is looking for, plus where in the results to carry on from.
|
||||
// searchRequest is a search of GET /api/videos on discovery: what a reader is
|
||||
// looking for, plus where in the results to carry on from.
|
||||
//
|
||||
// Every field is optional. An empty body is the whole catalogue, newest first.
|
||||
// Every field is optional. A search with none of them set is the whole
|
||||
// catalogue, newest first.
|
||||
type searchRequest struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Categories []string `json:"categories,omitempty"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
Cursor string `json:"cursor,omitempty"`
|
||||
Title string
|
||||
Categories []string
|
||||
Limit int
|
||||
Cursor string
|
||||
}
|
||||
|
||||
// query renders the search as the query string discovery reads it from.
|
||||
// Categories are repeated rather than joined, since that is how the endpoint
|
||||
// takes several of them, and an unset field is left out entirely so that the
|
||||
// service applies its own default.
|
||||
func (r searchRequest) query() url.Values {
|
||||
values := url.Values{}
|
||||
if r.Title != "" {
|
||||
values.Set("title", r.Title)
|
||||
}
|
||||
for _, category := range r.Categories {
|
||||
values.Add("categories", category)
|
||||
}
|
||||
if r.Limit != 0 {
|
||||
values.Set("limit", strconv.Itoa(r.Limit))
|
||||
}
|
||||
if r.Cursor != "" {
|
||||
values.Set("cursor", r.Cursor)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
// searchResultsBody is what that query answers with: the page of videos, and
|
||||
@@ -177,15 +201,14 @@ func (b searchResultsBody) holds(id string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// queryJSON sends a QUERY request — the method the catalogue search is served
|
||||
// on, chosen because a search is safe and idempotent like a GET but carries a
|
||||
// structured body no query string would hold comfortably.
|
||||
func (c *client) queryJSON(path string, payload any) (response, error) {
|
||||
encoded, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return response{}, err
|
||||
// search sends the catalogue search: a plain GET with the search spelled out
|
||||
// in the query string.
|
||||
func (c *client) search(path string, request searchRequest) (response, error) {
|
||||
query := request.query().Encode()
|
||||
if query != "" {
|
||||
path += "?" + query
|
||||
}
|
||||
return c.send("QUERY", c.base+path, "application/json", encoded)
|
||||
return c.get(path)
|
||||
}
|
||||
|
||||
// postJSON marshals payload and posts it to a path on the CMS.
|
||||
|
||||
+1
-1
@@ -973,7 +973,7 @@ func searchTheCatalogue(t gobdd.StepTest, w *world, request searchRequest) {
|
||||
w.discovery = newDiscoveryClient()
|
||||
}
|
||||
|
||||
result, err := w.discovery.queryJSON("/api/videos", request)
|
||||
result, err := w.discovery.search("/api/videos", request)
|
||||
if err != nil {
|
||||
t.Fatalf("could not search the catalogue: %s", err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user