Skip to content

Commit

Permalink
Prepare Search API for type gen (#2814)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyrombo committed Apr 1, 2022
1 parent 9843bc2 commit cf5d22d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
11 changes: 11 additions & 0 deletions discovery-provider/src/api/v1/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,17 @@ def __schema__(self):
search_parser = reqparse.RequestParser(argument_class=DescriptiveArgument)
search_parser.add_argument("query", required=True, description="The search query")

full_search_parser = pagination_with_current_user_parser.copy()
full_search_parser.add_argument("query", required=True, description="The search query")
full_search_parser.add_argument(
"kind",
required=False,
type=str,
default="all",
choices=("all", "users", "tracks", "playlists", "albums"),
description="The type of response, one of: all, users, tracks, playlists, or albums",
)

full_trending_parser = pagination_parser.copy()
full_trending_parser.add_argument(
"user_id", required=False, description="The user ID of the user making the request"
Expand Down
44 changes: 11 additions & 33 deletions discovery-provider/src/api/v1/search.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging # pylint: disable=C0302

from flask_restx import Namespace, Resource, fields, reqparse
from flask_restx import Namespace, Resource, fields
from src.api.v1.helpers import (
extend_playlist,
extend_track,
extend_user,
format_limit,
format_offset,
full_search_parser,
get_current_user_id,
make_full_response,
success_response,
Expand Down Expand Up @@ -43,41 +44,23 @@ def extend_search(resp):
return resp


search_route_parser = reqparse.RequestParser()
search_route_parser.add_argument("user_id", required=False)
search_route_parser.add_argument(
"kind",
required=False,
type=str,
default="all",
choices=("all", "users", "tracks", "playlists", "albums"),
)
search_route_parser.add_argument("query", required=True, type=str)
search_route_parser.add_argument("limit", required=False, type=int)
search_route_parser.add_argument("offset", required=False, type=int)
search_full_response = make_full_response(
"search_full_response", full_ns, fields.Nested(search_model)
)


@full_ns.route("/full")
class FullSearch(Resource):
@full_ns.expect(search_route_parser)
@full_ns.doc(
id="""Get Users/Tracks/Playlists/Albums that best match the search query""",
params={
"user_id": "A User ID of the requesting user to personalize the response",
"query": "Search query text",
"kind": "The type of response, one of: all, users, tracks, playlists, or albums",
"limit": "Limit",
"offset": "Offset",
},
id="Search",
description="""Get Users/Tracks/Playlists/Albums that best match the search query""",
responses={200: "Success", 400: "Bad request", 500: "Server error"},
)
@full_ns.expect(full_search_parser)
@full_ns.marshal_with(search_full_response)
@cache(ttl_sec=5)
def get(self):
args = search_route_parser.parse_args()
args = full_search_parser.parse_args()
offset = format_offset(args)
limit = format_limit(args)
current_user_id = get_current_user_id(args)
Expand Down Expand Up @@ -105,22 +88,17 @@ def get(self):

@full_ns.route("/autocomplete")
class FullSearchAutocomplete(Resource):
@full_ns.expect(search_route_parser)
@full_ns.doc(
id="""Get Users/Tracks/Playlists/albums that best match the search query""",
params={
"user_id": "A User ID of the requesting user to personalize the response",
"query": "Search query text",
"kind": "The type of response, one of: all, users, tracks, playlists, or albums",
"limit": "Limit",
"offset": "Offset",
},
id="Search Autocomplete",
summary="""Get Users/Tracks/Playlists/Albums that best match the search query""",
description="""Same as search but optimized for quicker response at the cost of some entity information.""",
responses={200: "Success", 400: "Bad request", 500: "Server error"},
)
@full_ns.expect(full_search_parser)
@full_ns.marshal_with(search_autocomplete_response)
@cache(ttl_sec=5)
def get(self):
args = search_route_parser.parse_args()
args = full_search_parser.parse_args()
offset = format_offset(args)
limit = format_limit(args)
current_user_id = get_current_user_id(args)
Expand Down

0 comments on commit cf5d22d

Please sign in to comment.