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.
- Loading branch information
Showing
7 changed files
with
128 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,76 @@ | ||
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]]() | ||
|
||
val proxyError: PartialFunction[Throwable, Unit] = { | ||
//TODO: wrap errors | ||
case err => resultPromise.failure(err) | ||
} | ||
val getFirstPage = getPage(accountSettings, page = 1, perPage = PER_PAGE) | ||
|
||
finish success List(new Repo("a", accountSettings.account), | ||
new Repo("b", accountSettings.account)) | ||
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 proxyError | ||
} | ||
} | ||
|
||
// finish failure new ConfigError("ou!") | ||
getFirstPage onFailure proxyError | ||
resultPromise.future | ||
} | ||
|
||
finish.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 | ||
) | ||
httpClient.get map { | ||
case response => | ||
response.status match { | ||
case 200 => { | ||
GithubReades.repoSeqReads.reads(response.json) match { | ||
case JsSuccess(res, _) => { | ||
val nextLastTMP = if (account.name == "hhru") Some(2) else None //FIXME: tmp | ||
PageRes(res, nextPage = nextLastTMP, lastPage = nextLastTMP) | ||
} | ||
case JsError(err) => throw new Exception(s"Unable to parse json $err") | ||
} | ||
} | ||
//TODO: custom exceptions | ||
case code: Int if code >= 400 && code < 599 => throw new Exception("no data") | ||
case _ => throw new Exception("unknown error") | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
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
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