Skip to content

Commit 0646b4d

Browse files
author
Rafa Paradela
committed
Merge pull request #3 from 47deg/rafa-ghresponse-simplification
Remove unused implementation
2 parents 1bab446 + ebfec71 commit 0646b4d

9 files changed

Lines changed: 25 additions & 119 deletions

File tree

src/main/scala/com/fortysevendeg/github4s/GithubResponses.scala

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ object GithubResponses {
1616

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

19-
20-
sealed trait GHResult[A]
21-
22-
final case class GHItemResult[A](value: A, statusCode: Int, headers: Map[String, IndexedSeq[String]]) extends GHResult[A]
23-
24-
final case class GHListResult[A](value: A, statusCode: Int, headers: Map[String, IndexedSeq[String]], decoder: Decoder[A]) extends GHResult[A]
25-
19+
case class GHResult[A](value: A, statusCode: Int, headers: Map[String, IndexedSeq[String]])
2620

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

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

35-
36-
def toEntity[A](response: HttpResponse[String], d: Decoder[A]): GHResponse[A] = response match {
37-
case r if r.isSuccess => {
38-
implicit val D: Decoder[A] = d
39-
decode[A](r.body).fold(e => JsonParsingException(e.getMessage).left[GHResult[A]], (result) => {
40-
result match {
41-
case Nil => Xor.Right(GHListResult(result, r.code, toLowerCase(r.headers), d))
42-
case _ :: _ => Xor.Right(GHListResult(result, r.code, toLowerCase(r.headers), d))
43-
case _ => Xor.Right(GHItemResult(result, r.code, toLowerCase(r.headers)))
44-
}
45-
})
46-
}
29+
def toEntity[A](response: HttpResponse[String])(implicit D: Decoder[A]): GHResponse[A] = response match {
30+
case r if r.isSuccess => decode[A](r.body)
31+
.fold(e => JsonParsingException(e.getMessage).left[GHResult[A]],
32+
result => Xor.Right(GHResult(result, r.code, toLowerCase(r.headers))))
4733
case r => UnexpectedException(s"Failed invoking get with status : ${r.code}, body : \n ${r.body}").left[GHResult[A]]
4834
}
4935

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

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

57-
implicit class GHEntity[A](result: GHResult[A]) {
58-
def entity[A] = result match {
59-
case GHListResult(r, _, _, _) => r
60-
case GHItemResult(r, _, _) => r
61-
}
62-
63-
def statusCode[A] = result match {
64-
case GHListResult(_, sc, _, _) => sc
65-
case GHItemResult(_, sc, _) => sc
66-
}
67-
68-
}
69-
7043
}

src/main/scala/com/fortysevendeg/github4s/HttpClient.scala

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,14 @@ class HttpClient {
8989
GithubResponses.toEntity(HttpRequestBuilder(buildURL(method))
9090
.withAuth(accessToken)
9191
.withParams(params ++ pagination.fold(Map.empty[String, String])(p => Map("page" -> p.page.toString, "per_page" -> p.per_page.toString)))
92-
.run, D)
93-
94-
def getByUrl[A](accessToken: Option[String] = None, url: String, d: Decoder[A]): GHResponse[A] =
95-
GithubResponses.toEntity(HttpRequestBuilder(url)
96-
.withAuth(accessToken)
97-
.run, d)
98-
92+
.run)
9993

10094
def patch[A](accessToken: Option[String] = None, method: String, data: String)(implicit D: Decoder[A]): GHResponse[A] =
10195
GithubResponses.toEntity(HttpRequestBuilder(buildURL(method))
10296
.patchMethod
10397
.withAuth(accessToken)
10498
.withData(data)
105-
.run, D)
99+
.run)
106100

107101
def put(accessToken: Option[String] = None, method: String): GHResponse[Unit] =
108102
GithubResponses.toEmpty(HttpRequestBuilder(buildURL(method))
@@ -121,7 +115,7 @@ class HttpClient {
121115
.withAuth(accessToken)
122116
.withHeaders(headers)
123117
.withData(data)
124-
.run, D)
118+
.run)
125119

126120
def postAuth[A](
127121
method: String,
@@ -131,7 +125,7 @@ class HttpClient {
131125
GithubResponses.toEntity(HttpRequestBuilder(buildURL(method))
132126
.withHeaders(headers)
133127
.withData(data)
134-
.run, D)
128+
.run)
135129

136130
def postOAuth[A](
137131
url: String,
@@ -140,7 +134,7 @@ class HttpClient {
140134
GithubResponses.toEntity(HttpRequestBuilder(url)
141135
.withHeaders(Map("Accept" -> "application/json"))
142136
.withData(data)
143-
.run, D)
137+
.run)
144138

145139
def delete(accessToken: Option[String] = None, method: String): GHResponse[Unit] =
146140
GithubResponses.toEmpty(HttpRequestBuilder(buildURL(method))

src/main/scala/com/fortysevendeg/github4s/api/Auth.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.fortysevendeg.github4s.api
33
import java.util.UUID
44

55
import cats.data.Xor
6-
import com.fortysevendeg.github4s.GithubResponses.{GHItemResult, GHResponse}
6+
import com.fortysevendeg.github4s.GithubResponses.{GHResult, GHResponse}
77
import com.fortysevendeg.github4s.free.domain._
88
import com.fortysevendeg.github4s.HttpClient
99
import io.circe.generic.auto._
@@ -21,6 +21,7 @@ object Auth {
2121
/**
2222
* Call to request a new authorization given a basic authentication, the returned object Authorization includes an
2323
* access token
24+
*
2425
* @param username
2526
* @param password
2627
* @param scopes
@@ -56,7 +57,7 @@ object Auth {
5657
scopes: List[String]): GHResponse[Authorize] = {
5758
val state = UUID.randomUUID().toString
5859
Xor.Right(
59-
GHItemResult(
60+
GHResult(
6061
value = Authorize(authorizeUrl.format(client_id, redirect_uri, scopes.mkString(","), state), state),
6162
statusCode = 200,
6263
headers = Map.empty))
@@ -65,6 +66,7 @@ object Auth {
6566

6667
/**
6768
* Requests an access token based on the code retrieved in the first step of the oAuth process
69+
*
6870
* @param client_id
6971
* @param client_secret
7072
* @param code

src/main/scala/com/fortysevendeg/github4s/app.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ import com.fortysevendeg.github4s.free.algebra._
55

66
object app {
77
type COGH01[A] = Coproduct[RepositoryOp, UserOp, A]
8-
type COGH02[A] = Coproduct[RequestOp, COGH01, A]
9-
type GitHub4s[A] = Coproduct[AuthOp, COGH02, A]
8+
type GitHub4s[A] = Coproduct[AuthOp, COGH01, A]
109
}

src/main/scala/com/fortysevendeg/github4s/free/algebra/RequestOps.scala

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/main/scala/com/fortysevendeg/github4s/free/interpreters/Interpreters.scala

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.fortysevendeg.github4s.free.interpreters
33
import cats.{MonadError, ApplicativeError, ~>, Eval}
44
import com.fortysevendeg.github4s.HttpClient
55
import com.fortysevendeg.github4s.api.{Auth, Repos}
6-
import com.fortysevendeg.github4s.app.{COGH01, COGH02, GitHub4s}
6+
import com.fortysevendeg.github4s.app.{COGH01, GitHub4s}
77
import com.fortysevendeg.github4s.free.algebra._
88
import io.circe.Decoder
99

@@ -13,9 +13,8 @@ trait Interpreters[M[_]] {
1313
implicit
1414
A: MonadError[M, Throwable]
1515
): GitHub4s ~> M = {
16-
val repositoryAndUserInterpreter: COGH01 ~> M = repositoryOpsInterpreter or userOpsInterpreter
17-
val c01nterpreter: COGH02 ~> M = requestOpsInterpreter or repositoryAndUserInterpreter
18-
val all: GitHub4s ~> M = authOpsInterpreter or c01nterpreter
16+
val c01interpreter: COGH01 ~> M = repositoryOpsInterpreter or userOpsInterpreter
17+
val all: GitHub4s ~> M = authOpsInterpreter or c01interpreter
1918
all
2019
}
2120

@@ -54,17 +53,6 @@ trait Interpreters[M[_]] {
5453
}
5554
}
5655

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

6957
}
7058

src/test/scala/com.fortysevendeg.github4s/integration/GHAuthSpec.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.fortysevendeg.github4s.integration
33
import cats.Id
44
import cats.scalatest.{XorMatchers, XorValues}
55
import com.fortysevendeg.github4s.Github._
6-
import com.fortysevendeg.github4s.GithubResponses._
76
import com.fortysevendeg.github4s.free.interpreters.IdInterpreters._
87
import com.fortysevendeg.github4s.{Github, TestUtils}
98
import org.scalatest._
@@ -18,7 +17,7 @@ class GHAuthSpec extends FlatSpec with Matchers with XorMatchers with XorValues
1817
"Auth >> AuthorizeUrl" should "return the expected URL for valid username" in {
1918
val response = Github().auth.authorizeUrl(validClientId, validRedirectUri, validScopes).exec[Id]
2019
response shouldBe right
21-
response.value.entity.url.contains(validRedirectUri) shouldBe true
20+
response.value.value.url.contains(validRedirectUri) shouldBe true
2221
response.value.statusCode shouldBe okStatusCode
2322
}
2423

src/test/scala/com.fortysevendeg.github4s/integration/GHReposSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class GHReposSpec extends FlatSpec with Matchers with XorMatchers with XorValues
1515

1616
val response = Github(accessToken).repos.get(validRepoOwner, validRepoName).exec[Id]
1717
response shouldBe right
18-
response.value.entity.name shouldBe validRepoName
18+
response.value.value.name shouldBe validRepoName
1919
response.value.statusCode shouldBe okStatusCode
2020
}
2121

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

src/test/scala/com.fortysevendeg.github4s/integration/GHUsersSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GHUsersSpec extends FlatSpec with Matchers with XorMatchers with XorValues
1414
"Users >> Get" should "return the expected login for a valid username" in {
1515
val response = Github(accessToken).users.get(validUsername).exec[Id]
1616
response shouldBe right
17-
response.value.entity.login shouldBe validUsername
17+
response.value.value.login shouldBe validUsername
1818
response.value.statusCode shouldBe okStatusCode
1919
}
2020

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

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

0 commit comments

Comments
 (0)