-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#36 Implemented searchRepositories v1
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 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,25 @@ | ||
package codecheck.github.models | ||
|
||
import org.json4s.JValue | ||
import codecheck.github.models.SortDirection | ||
|
||
sealed abstract class SearchSort(val name: String) { | ||
override def toString = name | ||
} | ||
object SearchSort { | ||
case object stars extends SearchSort("stars") | ||
case object forks extends SearchSort("forks") | ||
case object updated extends SearchSort("updated") | ||
} | ||
|
||
case class SearchInput ( | ||
q: String, | ||
sort: Option[SearchSort] = None, | ||
order: SortDirection = SortDirection.desc | ||
) extends AbstractInput | ||
|
||
case class SearchRepositoryResult(value: JValue) extends AbstractJson(value) { | ||
def total_count: Long = get("total_count").toLong | ||
def incomplete_results: Boolean = boolean("incomplete_results") | ||
lazy val items = Repository(value \ "items") | ||
} |
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,22 @@ | ||
package codecheck.github.operations | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
import codecheck.github.api.GitHubAPI | ||
import codecheck.github.models.SearchInput | ||
import codecheck.github.models.SearchRepositoryResult | ||
|
||
trait SearchOp { | ||
self: GitHubAPI => | ||
|
||
def searchRepositories(input: SearchInput): Future[Option[SearchRepositoryResult]] = { | ||
val path = s"/search/repositories?q=${input.q}&sort=${input.sort}&order=${input.order}" | ||
exec("GET", path ).map { res => | ||
res.statusCode match { | ||
case 200 => Some(SearchRepositoryResult(res.body)) | ||
case 404 => None | ||
} | ||
} | ||
} | ||
} |
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,21 @@ | ||
import org.scalatest.path.FunSpec | ||
import scala.concurrent.Await | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import codecheck.github.models.SortDirection | ||
import codecheck.github.models.SearchInput | ||
import codecheck.github.models.SearchSort | ||
import codecheck.github.models.SearchRepositoryResult | ||
|
||
class SearchOpSpec extends FunSpec | ||
with Constants | ||
{ | ||
val input = SearchInput("tetris",sort=Some(SearchSort.stars),order=SortDirection.desc) | ||
describe("searchRepositories") { | ||
it("with valid SearchInput should succeed") { | ||
Await.result(api.searchRepositories(input), TIMEOUT).map { res => | ||
assert(res.total_count >= 1) | ||
println("RESULT" + res) | ||
} | ||
} | ||
} | ||
} |