Skip to content

Commit

Permalink
Merge pull request #3 from 47deg/rafa-ghresponse-simplification
Browse files Browse the repository at this point in the history
Remove unused implementation
  • Loading branch information
Rafa Paradela committed May 12, 2016
2 parents 1bab446 + ebfec71 commit 0646b4d
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 119 deletions.
39 changes: 6 additions & 33 deletions src/main/scala/com/fortysevendeg/github4s/GithubResponses.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ object GithubResponses {

type GHResponse[A] = GHException Xor GHResult[A]


sealed trait GHResult[A]

final case class GHItemResult[A](value: A, statusCode: Int, headers: Map[String, IndexedSeq[String]]) extends GHResult[A]

final case class GHListResult[A](value: A, statusCode: Int, headers: Map[String, IndexedSeq[String]], decoder: Decoder[A]) extends GHResult[A]

case class GHResult[A](value: A, statusCode: Int, headers: Map[String, IndexedSeq[String]])

sealed abstract class GHException(msg : String, cause : Option[Throwable] = None) extends Throwable(msg) {
cause foreach initCause
Expand All @@ -32,39 +26,18 @@ object GithubResponses {

case class UnexpectedException(msg : String) extends GHException(msg)


def toEntity[A](response: HttpResponse[String], d: Decoder[A]): GHResponse[A] = response match {
case r if r.isSuccess => {
implicit val D: Decoder[A] = d
decode[A](r.body).fold(e => JsonParsingException(e.getMessage).left[GHResult[A]], (result) => {
result match {
case Nil => Xor.Right(GHListResult(result, r.code, toLowerCase(r.headers), d))
case _ :: _ => Xor.Right(GHListResult(result, r.code, toLowerCase(r.headers), d))
case _ => Xor.Right(GHItemResult(result, r.code, toLowerCase(r.headers)))
}
})
}
def toEntity[A](response: HttpResponse[String])(implicit D: Decoder[A]): GHResponse[A] = response match {
case r if r.isSuccess => decode[A](r.body)
.fold(e => JsonParsingException(e.getMessage).left[GHResult[A]],
result => Xor.Right(GHResult(result, r.code, toLowerCase(r.headers))))
case r => UnexpectedException(s"Failed invoking get with status : ${r.code}, body : \n ${r.body}").left[GHResult[A]]
}

def toEmpty(response: HttpResponse[String]): GHResponse[Unit] = response match {
case r if r.isSuccess => Xor.Right(GHItemResult(Unit, r.code, toLowerCase(r.headers)))
case r if r.isSuccess => Xor.Right(GHResult(Unit, r.code, toLowerCase(r.headers)))
case r => UnexpectedException(s"Failed invoking get with status : ${r.code}, body : \n ${r.body}").left[GHResult[Unit]]
}

private def toLowerCase(headers: Map[String, IndexedSeq[String]]): Map[String, IndexedSeq[String]] = headers.map(e => (e._1.toLowerCase, e._2))

implicit class GHEntity[A](result: GHResult[A]) {
def entity[A] = result match {
case GHListResult(r, _, _, _) => r
case GHItemResult(r, _, _) => r
}

def statusCode[A] = result match {
case GHListResult(_, sc, _, _) => sc
case GHItemResult(_, sc, _) => sc
}

}

}
16 changes: 5 additions & 11 deletions src/main/scala/com/fortysevendeg/github4s/HttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,14 @@ class HttpClient {
GithubResponses.toEntity(HttpRequestBuilder(buildURL(method))
.withAuth(accessToken)
.withParams(params ++ pagination.fold(Map.empty[String, String])(p => Map("page" -> p.page.toString, "per_page" -> p.per_page.toString)))
.run, D)

def getByUrl[A](accessToken: Option[String] = None, url: String, d: Decoder[A]): GHResponse[A] =
GithubResponses.toEntity(HttpRequestBuilder(url)
.withAuth(accessToken)
.run, d)

.run)

def patch[A](accessToken: Option[String] = None, method: String, data: String)(implicit D: Decoder[A]): GHResponse[A] =
GithubResponses.toEntity(HttpRequestBuilder(buildURL(method))
.patchMethod
.withAuth(accessToken)
.withData(data)
.run, D)
.run)

def put(accessToken: Option[String] = None, method: String): GHResponse[Unit] =
GithubResponses.toEmpty(HttpRequestBuilder(buildURL(method))
Expand All @@ -121,7 +115,7 @@ class HttpClient {
.withAuth(accessToken)
.withHeaders(headers)
.withData(data)
.run, D)
.run)

def postAuth[A](
method: String,
Expand All @@ -131,7 +125,7 @@ class HttpClient {
GithubResponses.toEntity(HttpRequestBuilder(buildURL(method))
.withHeaders(headers)
.withData(data)
.run, D)
.run)

def postOAuth[A](
url: String,
Expand All @@ -140,7 +134,7 @@ class HttpClient {
GithubResponses.toEntity(HttpRequestBuilder(url)
.withHeaders(Map("Accept" -> "application/json"))
.withData(data)
.run, D)
.run)

def delete(accessToken: Option[String] = None, method: String): GHResponse[Unit] =
GithubResponses.toEmpty(HttpRequestBuilder(buildURL(method))
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/com/fortysevendeg/github4s/api/Auth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.fortysevendeg.github4s.api
import java.util.UUID

import cats.data.Xor
import com.fortysevendeg.github4s.GithubResponses.{GHItemResult, GHResponse}
import com.fortysevendeg.github4s.GithubResponses.{GHResult, GHResponse}
import com.fortysevendeg.github4s.free.domain._
import com.fortysevendeg.github4s.HttpClient
import io.circe.generic.auto._
Expand All @@ -21,6 +21,7 @@ object Auth {
/**
* Call to request a new authorization given a basic authentication, the returned object Authorization includes an
* access token
*
* @param username
* @param password
* @param scopes
Expand Down Expand Up @@ -56,7 +57,7 @@ object Auth {
scopes: List[String]): GHResponse[Authorize] = {
val state = UUID.randomUUID().toString
Xor.Right(
GHItemResult(
GHResult(
value = Authorize(authorizeUrl.format(client_id, redirect_uri, scopes.mkString(","), state), state),
statusCode = 200,
headers = Map.empty))
Expand All @@ -65,6 +66,7 @@ object Auth {

/**
* Requests an access token based on the code retrieved in the first step of the oAuth process
*
* @param client_id
* @param client_secret
* @param code
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/com/fortysevendeg/github4s/app.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import com.fortysevendeg.github4s.free.algebra._

object app {
type COGH01[A] = Coproduct[RepositoryOp, UserOp, A]
type COGH02[A] = Coproduct[RequestOp, COGH01, A]
type GitHub4s[A] = Coproduct[AuthOp, COGH02, A]
type GitHub4s[A] = Coproduct[AuthOp, COGH01, A]
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.fortysevendeg.github4s.free.interpreters
import cats.{MonadError, ApplicativeError, ~>, Eval}
import com.fortysevendeg.github4s.HttpClient
import com.fortysevendeg.github4s.api.{Auth, Repos}
import com.fortysevendeg.github4s.app.{COGH01, COGH02, GitHub4s}
import com.fortysevendeg.github4s.app.{COGH01, GitHub4s}
import com.fortysevendeg.github4s.free.algebra._
import io.circe.Decoder

Expand All @@ -13,9 +13,8 @@ trait Interpreters[M[_]] {
implicit
A: MonadError[M, Throwable]
): GitHub4s ~> M = {
val repositoryAndUserInterpreter: COGH01 ~> M = repositoryOpsInterpreter or userOpsInterpreter
val c01nterpreter: COGH02 ~> M = requestOpsInterpreter or repositoryAndUserInterpreter
val all: GitHub4s ~> M = authOpsInterpreter or c01nterpreter
val c01interpreter: COGH01 ~> M = repositoryOpsInterpreter or userOpsInterpreter
val all: GitHub4s ~> M = authOpsInterpreter or c01interpreter
all
}

Expand Down Expand Up @@ -54,17 +53,6 @@ trait Interpreters[M[_]] {
}
}

/** Lifts Request Ops to an effect capturing Monad such as Task via natural transformations
*/
def requestOpsInterpreter(implicit App: ApplicativeError[M, Throwable]): RequestOp ~> M = new (RequestOp ~> M) {
def apply[A](fa: RequestOp[A]): M[A] = fa match {
case Next(url: String, decoder: Decoder[A], accessToken) {
//implicit val d: Decoder[A] = decoder
App.pureEval(Eval.later(httpClient.getByUrl(accessToken, url, decoder)))
}
}
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.fortysevendeg.github4s.integration
import cats.Id
import cats.scalatest.{XorMatchers, XorValues}
import com.fortysevendeg.github4s.Github._
import com.fortysevendeg.github4s.GithubResponses._
import com.fortysevendeg.github4s.free.interpreters.IdInterpreters._
import com.fortysevendeg.github4s.{Github, TestUtils}
import org.scalatest._
Expand All @@ -18,7 +17,7 @@ class GHAuthSpec extends FlatSpec with Matchers with XorMatchers with XorValues
"Auth >> AuthorizeUrl" should "return the expected URL for valid username" in {
val response = Github().auth.authorizeUrl(validClientId, validRedirectUri, validScopes).exec[Id]
response shouldBe right
response.value.entity.url.contains(validRedirectUri) shouldBe true
response.value.value.url.contains(validRedirectUri) shouldBe true
response.value.statusCode shouldBe okStatusCode
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GHReposSpec extends FlatSpec with Matchers with XorMatchers with XorValues

val response = Github(accessToken).repos.get(validRepoOwner, validRepoName).exec[Id]
response shouldBe right
response.value.entity.name shouldBe validRepoName
response.value.value.name shouldBe validRepoName
response.value.statusCode shouldBe okStatusCode
}

Expand All @@ -27,7 +27,7 @@ class GHReposSpec extends FlatSpec with Matchers with XorMatchers with XorValues
"Repos >> ListCommits" should "return the expected list of commits for valid data" in {
val response = Github(accessToken).repos.listCommits(validRepoOwner, validRepoName).exec[Id]
response shouldBe right
response.value.entity.nonEmpty shouldBe true
response.value.value.nonEmpty shouldBe true
response.value.statusCode shouldBe okStatusCode
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GHUsersSpec extends FlatSpec with Matchers with XorMatchers with XorValues
"Users >> Get" should "return the expected login for a valid username" in {
val response = Github(accessToken).users.get(validUsername).exec[Id]
response shouldBe right
response.value.entity.login shouldBe validUsername
response.value.value.login shouldBe validUsername
response.value.statusCode shouldBe okStatusCode
}

Expand All @@ -31,14 +31,14 @@ class GHUsersSpec extends FlatSpec with Matchers with XorMatchers with XorValues
"Users >> GetUsers" should "return users for a valid since value" in {
val response = Github(accessToken).users.getUsers(validSinceInt).exec[Id]
response shouldBe right
response.value.entity.nonEmpty shouldBe true
response.value.value.nonEmpty shouldBe true
response.value.statusCode shouldBe okStatusCode
}

it should "return error on Left when a invalid since value is provided" in {
val response = Github(accessToken).users.getUsers(invalidSinceInt).exec[Id]
response shouldBe right
response.value.entity.nonEmpty shouldBe true
response.value.value.nonEmpty shouldBe true
response.value.statusCode shouldBe okStatusCode
}

Expand Down

0 comments on commit 0646b4d

Please sign in to comment.