-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Align search API with search page #656
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,11 +28,24 @@ object SearchApi { | |
implicit val formatReleaseOptions: OFormat[ReleaseOptions] = | ||
Json.format[ReleaseOptions] | ||
|
||
implicit val formatResult: OFormat[Result] = | ||
Json.format[Result] | ||
|
||
case class Result(currentPage: Int, pageTotal: Int, items: Seq[Project]) | ||
case class Project( | ||
organization: String, | ||
repository: String, | ||
logo: Option[String] = None, | ||
artifacts: List[String] = Nil | ||
logo: Option[String], | ||
stars: Option[Int], | ||
forks: Option[Int], | ||
contributorCount: Option[Int], | ||
topics: Set[String], | ||
defaultArtifact: Option[String], | ||
artifacts: List[String], | ||
scalaVersion: List[String], | ||
scalaJsVersion: List[String], | ||
scalaNativeVersion: List[String], | ||
sbtVersion: List[String] | ||
) | ||
|
||
case class ReleaseOptions( | ||
|
@@ -91,7 +104,8 @@ class SearchApi( | |
val routes: Route = | ||
pathPrefix("api") { | ||
cors() { | ||
path("search") { | ||
// deprecated endpoint replaced by api/search endpoint | ||
path("search-old") { | ||
get { | ||
parameters( | ||
( | ||
|
@@ -125,17 +139,6 @@ class SearchApi( | |
sbtVersion | ||
) | ||
|
||
def convert(project: Project): SearchApi.Project = { | ||
import project._ | ||
val artifacts0 = if (cli) cliArtifacts.toList else artifacts | ||
SearchApi.Project( | ||
organization, | ||
repository, | ||
project.github.flatMap(_.logo.map(_.target)), | ||
artifacts0 | ||
) | ||
} | ||
|
||
scalaTarget match { | ||
case Some(_) => | ||
val searchParams = SearchParams( | ||
|
@@ -147,7 +150,7 @@ class SearchApi( | |
) | ||
val result = dataRepository | ||
.findProjects(searchParams) | ||
.map(page => page.items.map(p => convert(p))) | ||
.map(page => page.items.map(convertProject(cli))) | ||
complete(OK, result) | ||
|
||
case None => | ||
|
@@ -158,6 +161,25 @@ class SearchApi( | |
} | ||
} | ||
} ~ | ||
path("search") { | ||
get { | ||
optionalSession(refreshable, usingCookies) { userId => | ||
val user = session.getUser(userId) | ||
searchParams(user) { params => | ||
val result = dataRepository | ||
.findProjects(params) | ||
.map { page => | ||
SearchApi.Result( | ||
currentPage = page.pagination.current, | ||
pageTotal = page.pagination.pageCount, | ||
items = page.items.map(convertProject(params.cli)) | ||
) | ||
} | ||
complete(result) | ||
} | ||
} | ||
} | ||
} ~ | ||
path("project") { | ||
get { | ||
parameters( | ||
|
@@ -201,16 +223,37 @@ class SearchApi( | |
optionalSession(refreshable, usingCookies) { userId => | ||
val user = session.getUser(userId) | ||
searchParams(user) { params => | ||
complete { | ||
autocomplete(params) | ||
} | ||
val autoCompletion = autocomplete(params) | ||
complete(autoCompletion) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private def convertProject( | ||
cli: Boolean | ||
)(project: Project): SearchApi.Project = { | ||
val artifacts = if (cli) project.cliArtifacts.toList else project.artifacts | ||
val defaultArtifact = project.defaultArtifact.filter(artifacts.contains) | ||
SearchApi.Project( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also include defaultArtifact since search API under the hood redirects to it anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the This field is not set automatically, it is set manually by the library maintainer. So it can be empty or completely nonsense. |
||
project.organization, | ||
project.repository, | ||
project.github.flatMap(_.logo.map(_.target)), | ||
project.github.flatMap(_.stars), | ||
project.github.flatMap(_.forks), | ||
project.github.map(_.contributorCount), | ||
project.github.map(_.topics).getOrElse(Set.empty), | ||
defaultArtifact, | ||
artifacts, | ||
project.scalaVersion, | ||
project.sbtVersion, | ||
project.scalaJsVersion, | ||
project.scalaNativeVersion | ||
) | ||
} | ||
|
||
private def getReleaseOptions( | ||
projectRef: Project.Reference, | ||
scalaTarget: Option[ScalaTarget], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,29 +10,33 @@ package object routes { | |
( | ||
"q" ? "*", | ||
"page".as[Int] ? 1, | ||
"total".as[Int] ? SearchParams.resultsPerPage, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also include information about available pages? Currently one needs to call next page until the response is empty (but this is probably not in the scope of this PR). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the |
||
"sort".?, | ||
"topics".as[String].*, | ||
"targetTypes".as[String].*, | ||
"scalaVersions".as[String].*, | ||
"scalaJsVersions".as[String].*, | ||
"scalaNativeVersions".as[String].*, | ||
"sbtVersions".as[String].*, | ||
"you".?, | ||
"contributingSearch".as[Boolean] ? false | ||
"contributingSearch".as[Boolean] ? false, | ||
"cli".as[Boolean] ? false, | ||
"you".? | ||
) | ||
).tmap { | ||
case ( | ||
q, | ||
page, | ||
total, | ||
sort, | ||
topics, | ||
targetTypes, | ||
scalaVersions, | ||
scalaJsVersions, | ||
scalaNativeVersions, | ||
sbtVersions, | ||
you, | ||
contributingSearch | ||
contributingSearch, | ||
cli, | ||
you | ||
) => | ||
val userRepos = you | ||
.flatMap(_ => user.map(_.repos)) | ||
|
@@ -42,8 +46,10 @@ package object routes { | |
page, | ||
sort, | ||
userRepos, | ||
total = total, | ||
topics = topics.toList, | ||
targetTypes = targetTypes.toList, | ||
cli = cli, | ||
scalaVersions = scalaVersions.toList, | ||
scalaJsVersions = scalaJsVersions.toList, | ||
scalaNativeVersions = scalaNativeVersions.toList, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we instead keep this one and introduce a new endpoint
api/search/v2
, to not break existing users?