This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iss #22: fetch repos with github client that jush work (without timeo…
…uts, cache, unittests ...)
- Loading branch information
Showing
10 changed files
with
144 additions
and
30 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 |
---|---|---|
@@ -1,23 +1,87 @@ | ||
package hedgehog.clients.github | ||
|
||
import scala.concurrent.{Future, Promise} | ||
import hedgehog.models.{AccountSettings, Repo} | ||
import play.api.libs.ws.WS | ||
import play.api.libs.json.{JsSuccess, JsError} | ||
|
||
import hedgehog.models.{AccountSettings, Repo} | ||
|
||
/** | ||
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014 | ||
* See LICENSE.txt for details. | ||
*/ | ||
class Client(val config: Config, implicit val context: scala.concurrent.ExecutionContext) { | ||
|
||
val PER_PAGE = 100 | ||
|
||
def getRepos(accountSettings: AccountSettings): Future[Seq[Repo]] = { | ||
val finish = Promise[Seq[Repo]]() | ||
val resultPromise = Promise[Seq[Repo]]() | ||
|
||
finish success List(new Repo("a", accountSettings.account), | ||
new Repo("b", accountSettings.account)) | ||
val wrapError: PartialFunction[Throwable, Unit] = { | ||
//TODO: custom error subclasses | ||
case err => resultPromise.failure(new FetchError(cause = err)) | ||
} | ||
val getFirstPage = getPage(accountSettings, page = 1, perPage = PER_PAGE) | ||
|
||
// finish failure new ConfigError("ou!") | ||
getFirstPage onSuccess { | ||
case PageRes(res, None, None) => resultPromise.success(res) | ||
case PageRes(firstPageRes, _, Some(last)) => | ||
require(last > 1) | ||
val otherPagesRes = Future.sequence( | ||
for { | ||
page <- 2 to last | ||
} yield getPage(accountSettings, page, perPage = PER_PAGE) | ||
) | ||
otherPagesRes onSuccess { | ||
case otherPages => resultPromise success (firstPageRes ++ otherPages.flatMap(_.repos)) | ||
} | ||
otherPagesRes onFailure wrapError | ||
} | ||
getFirstPage onFailure wrapError | ||
|
||
finish.future | ||
resultPromise.future | ||
} | ||
|
||
private def getPage(accountSettings: AccountSettings, page: Int, perPage: Int): Future[PageRes] = { | ||
val account = accountSettings.account | ||
val url = config.replaceBaseUrl(account.apiReposUrl) | ||
val httpClient = ( | ||
WS.url(url).withQueryString( | ||
"per_page" -> perPage.toString, | ||
"page" -> page.toString, | ||
"type" -> "open" | ||
) | ||
match { | ||
case h if accountSettings.includePrivateRepos => h.withQueryString("type" ->"private", "type" -> "open") | ||
case h => h | ||
} | ||
) match { | ||
case h if config.accessToken.isDefined => h.withHeaders("Authorization" -> s"token ${config.accessToken.get}") | ||
case h => h | ||
} | ||
|
||
httpClient.get map { | ||
case response => | ||
response.status match { | ||
case 200 => | ||
GithubReades.repoSeqReads.reads(response.json) match { | ||
case JsSuccess(res, _) => | ||
val rels = response.header("Link").map(parseLinkHeader) | ||
PageRes( | ||
res, | ||
nextPage = rels.flatMap(_.get("next").flatMap(_.page)), | ||
lastPage = rels.flatMap(_.get("last")).flatMap(_.page)) | ||
case JsError(err) => throw new FetchError(s"Unable to parse repo json $err") | ||
} | ||
//TODO: custom exceptions subclasses | ||
case code: Int if code >= 400 && code < 599 => throw new FetchError("No data for repos request") | ||
case _ => throw new FetchError("Unknown error when fetching repos") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
private case class PageRes(repos: Seq[Repo], nextPage: Option[Int], lastPage: Option[Int]) { | ||
val currentPage: Option[Int] = nextPage map (_ - 1) | ||
} |
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,44 @@ | ||
package hedgehog.clients.github | ||
|
||
import play.api.libs.functional.syntax._ | ||
import play.api.libs.json.{Reads, __} | ||
|
||
import hedgehog.models.{GithubOrg, Account, GithubUser, Repo, ProgrammingLang} | ||
/** | ||
* Copyright (c) Nikita Kovaliov, maizy.ru, 2014 | ||
* See LICENSE.txt for details. | ||
*/ | ||
object GithubReades { | ||
|
||
val accountReads: Reads[Account] = ( | ||
(__ \ "type").read[String] ~ | ||
(__ \ "login").read[String] ~ | ||
(__ \ "avatar_url").readNullable[String] | ||
) apply { | ||
(accountType, login, avatarUrl) => accountType match { | ||
case "User" => GithubUser(login, avatarUrl) | ||
case "Organization" => GithubOrg(login, avatarUrl) | ||
case _ => throw new FormatError(s"Unknown owner type "+ accountType) | ||
} | ||
} | ||
|
||
val repoReads: Reads[Repo] = ( | ||
(__ \ "name").read[String] ~ | ||
(__ \ "owner").read[Account](accountReads) ~ | ||
(__ \ "description").readNullable[String] ~ | ||
(__ \ "private").read[Boolean] ~ | ||
(__ \ "language").readNullable[String] | ||
) apply { | ||
(name, owner, description, isPrivate, lang) => { | ||
Repo( | ||
name, | ||
owner, | ||
description = description, | ||
isPrivate = Some(isPrivate), | ||
primaryLang = lang.map(l => ProgrammingLang(l.toLowerCase)) | ||
) | ||
} | ||
} | ||
|
||
val repoSeqReads: Reads[Seq[Repo]] = Reads.seq[Repo](repoReads) | ||
} |
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
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
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