Skip to content

Commit

Permalink
add restful api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaoz committed May 6, 2023
1 parent 319044c commit 97a5aae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions server/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,26 @@ func (s *RestServer) CreateWebService() {
Returns(http.StatusOK, "OK", []cache.Document{}).
Writes([]cache.Document{}))

// Get top items
ws.Route(ws.GET("/top/{name}").To(s.getTopItem).
Doc("Get top items.").
Metadata(restfulspec.KeyOpenAPITags, []string{RecommendationAPITag}).
Param(ws.HeaderParameter("X-API-Key", "API key").DataType("string")).
Param(ws.PathParameter("name", "Name of the top item recommender.").DataType("string")).
Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")).
Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")).
Returns(http.StatusOK, "OK", []cache.Document{}).
Writes([]cache.Document{}))
ws.Route(ws.GET("/top/{name}/{category}").To(s.getTopItem).
Doc("Get top items in category.").
Metadata(restfulspec.KeyOpenAPITags, []string{RecommendationAPITag}).
Param(ws.HeaderParameter("X-API-Key", "API key").DataType("string")).
Param(ws.PathParameter("name", "Name of the top item recommender.").DataType("string")).
Param(ws.PathParameter("category", "Category of returned items.").DataType("string")).
Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")).
Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")).
Returns(http.StatusOK, "OK", []cache.Document{}).
Writes([]cache.Document{}))
// Get popular items
ws.Route(ws.GET("/popular").To(s.getPopular).
Doc("Get popular items.").
Expand Down Expand Up @@ -614,6 +634,13 @@ func (s *RestServer) searchDocuments(collection, subset, category string, isItem
Ok(response, items)
}

func (s *RestServer) getTopItem(request *restful.Request, response *restful.Response) {
name := request.PathParameter("name")
category := request.PathParameter("category")
log.ResponseLogger(response).Debug("get top items", zap.String("name", name), zap.String("category", category))
s.searchDocuments(cache.TopItems, name, category, true, request, response)
}

func (s *RestServer) getPopular(request *restful.Request, response *restful.Response) {
category := request.PathParameter("category")
log.ResponseLogger(response).Debug("get category popular items in category", zap.String("category", category))
Expand Down
2 changes: 2 additions & 0 deletions storage/cache/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const (
// Categorized the latest items - latest_items/{category}
LatestItems = "latest_items"

TopItems = "top_items"

// ItemCategories is the set of item categories. The format of key:
// Global item categories - item_categories
ItemCategories = "item_categories"
Expand Down

0 comments on commit 97a5aae

Please sign in to comment.