-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compatibility with scala-js #38
Changes from 15 commits
a922b56
aed4702
34cbe91
042ef1c
2e3a545
ef4298e
3d266d3
311661b
586586a
c9515eb
af091aa
0a84652
683cda3
b2b09f2
7040a3c
8ba724e
fcaf13c
544652b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ lazy val buildSettings = Seq( | |
"scala" -> MIT("2016", "47 Degrees, LLC. <http://www.47deg.com>") | ||
) | ||
) ++ reformatOnCompileSettings ++ | ||
sharedCommonSettings ++ | ||
miscSettings ++ | ||
sharedReleaseProcess ++ | ||
credentialSettings ++ | ||
|
@@ -39,32 +40,54 @@ lazy val micrositeSettings = Seq( | |
includeFilter in makeSite := "*.html" | "*.css" | "*.png" | "*.jpg" | "*.gif" | "*.js" | "*.swf" | "*.md" | ||
) | ||
|
||
lazy val dependencies = addLibs(vAll, | ||
lazy val commonDeps = addLibs(vAll, | ||
"cats-free", | ||
"circe-core", | ||
"circe-generic", | ||
"circe-parser", | ||
"simulacrum") ++ | ||
addTestLibs(vAll, "scalatest") ++ | ||
addCompilerPlugins(vAll, "paradise") ++ | ||
Seq( | ||
Seq(libraryDependencies ++= Seq( | ||
"org.scalatest" %%% "scalatest" % "3.0.0" % "test", | ||
"com.github.marklister" %%% "base64" % "0.2.2" | ||
)) | ||
|
||
lazy val jvmDeps = Seq( | ||
libraryDependencies ++= Seq( | ||
"org.scalaj" %% "scalaj-http" % "2.2.1", | ||
"org.mock-server" % "mockserver-netty" % "3.10.4" % "test" | ||
)) | ||
|
||
lazy val jsDeps = Seq( | ||
libraryDependencies ++= Seq( | ||
"fr.hmil" %%% "roshttp" % "2.0.0-RC1" | ||
) | ||
) | ||
|
||
lazy val docsDependencies = libraryDependencies ++= Seq( | ||
"com.ironcorelabs" %% "cats-scalatest" % "1.1.2" % "test", | ||
"org.mock-server" % "mockserver-netty" % "3.10.4" % "test" | ||
) | ||
|
||
lazy val scalazDependencies = addLibs(vAll, "scalaz-concurrent") | ||
|
||
lazy val github4s = (project in file(".")) | ||
/** github4s - cross project that provides cross platform support.*/ | ||
lazy val github4s = (crossProject in file("github4s")) | ||
.settings(moduleName := "github4s") | ||
.settings(buildSettings: _*) | ||
.settings(dependencies: _*) | ||
.enablePlugins(AutomateHeaderPlugin) | ||
.enablePlugins(BuildInfoPlugin). | ||
settings( | ||
buildInfoKeys := Seq[BuildInfoKey](name, version, "token" -> sys.props("token")), | ||
buildInfoPackage := "github4s" | ||
) | ||
.settings(buildSettings: _*) | ||
.settings(commonDeps: _*) | ||
.jvmSettings(jvmDeps: _*) | ||
.jsSettings(sharedJsSettings: _*) | ||
.jsSettings(jsDeps: _*) | ||
|
||
lazy val github4sJVM = github4s.jvm | ||
lazy val github4sJS = github4s.js.settings(testSettings: _*) | ||
|
||
lazy val docs = (project in file("docs")) | ||
.dependsOn(scalaz) | ||
|
@@ -79,5 +102,11 @@ lazy val scalaz = (project in file("scalaz")) | |
.settings(moduleName := "github4s-scalaz") | ||
.settings(buildSettings: _*) | ||
.settings(scalazDependencies: _*) | ||
.dependsOn(github4s) | ||
.dependsOn(github4sJVM) | ||
.enablePlugins(AutomateHeaderPlugin) | ||
|
||
lazy val testSettings = Seq( | ||
fork in Test := false | ||
) | ||
|
||
addCommandAlias("testAllCoverable", ";docs/test;scalaz/test;github4sJVM/test") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did it that way because I was under the impression that scoverage wasn't compatible with scala-js, which was the case until this: scoverage/scalac-scoverage-plugin#118. Do you know which version of the sbt-scoverage plugin are we using on sbt-catalyst-extras? (I haven't been able to find it). I'm not really familiar on how it works... Maybe we need to update it for the JS tests to be coverable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I switched to using a higher version of the sbt-scoverage plugin and proved to be compatible with ScalaJS, so I removed the extra command and all the tests go through the coverage process. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,38 +3,56 @@ layout: docs | |
title: Getting Started | ||
--- | ||
|
||
# Get started | ||
# Getting started | ||
|
||
WIP: Import | ||
|
||
```tut:silent | ||
import github4s.Github | ||
``` | ||
|
||
In order for github4s to work in both JVM and scala-js environments, you'll need to place different implicits in your scope: | ||
|
||
```tut:silent | ||
object JVMProgram extends github4s.ImplicitsJVM { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should provide objects for implicits imports such as |
||
// Your JVM-compatible code... | ||
} | ||
|
||
/* | ||
object JSProgram extends github4s.ImplicitsJS { | ||
// Your scala-js compatible code... | ||
} | ||
*/ | ||
``` | ||
|
||
```tut:invisible | ||
val accessToken = sys.props.get("token") | ||
``` | ||
|
||
WIP: Every Github4s api returns a `Free[GHResponse[A], A]` where `GHResonse[A]` is a type alias for `Either[GHException, GHResult[A]]`. GHResult contains the result `[A]` given by Github, but also the status code of the response and headers: | ||
WIP: Every Github4s api returns a `Free[GHResponse[A], A]` where `GHResponse[A]` is a type alias for `Either[GHException, GHResult[A]]`. GHResult contains the result `[A]` given by GitHub, but also the status code of the response and headers: | ||
|
||
```scala | ||
case class GHResult[A](result: A, statusCode: Int, headers: Map[String, IndexedSeq[String]]) | ||
``` | ||
|
||
For geting an user | ||
For getting an user | ||
|
||
```tut:silent | ||
val user1 = Github(accessToken).users.get("rafaparadela") | ||
``` | ||
|
||
user1 in this case `Free[GHException Xor GHResult[User], User]` and we can run (`foldMap`) with `exec[M[_]]` where `M[_]` represent any type container that implements `MonadError[M, Throwable]`, for instance `cats.Eval`. | ||
user1 in this case `Free[GHException Xor GHResult[User], User]` and we can run (`foldMap`) with `exec[M[_], C]` where `M[_]` represent any type container that implements `MonadError[M, Throwable]`, for instance `cats.Eval`; and C represents a valid implementation of an HttpClient. The previously mentioned implicit classes carry already set up instances for working with `scalaj` (for JVM-compatible apps) and `roshttp` (for scala-js-compatible apps). Take into account that in the latter case, you can only use `Future` in the place of `M[_]`: | ||
|
||
```tut:silent | ||
import cats.Eval | ||
import github4s.Github._ | ||
import github4s.implicits._ | ||
import scalaj.http._ | ||
|
||
object ProgramEval extends github4s.ImplicitsJVM { | ||
val u1 = user1.exec[Eval, HttpResponse[String]].value | ||
} | ||
|
||
val u1 = user1.exec[Eval].value | ||
``` | ||
|
||
WIP: As mentioned above `u1` should have an `GHResult[User]` in the right. | ||
|
@@ -45,7 +63,7 @@ import github4s.GithubResponses.GHResult | |
``` | ||
|
||
```tut:book | ||
u1 match { | ||
ProgramEval.u1 match { | ||
case Right(GHResult(result, status, headers)) => result.login | ||
case Left(e) => e.getMessage | ||
} | ||
|
@@ -55,8 +73,11 @@ WIP: With `Id` | |
|
||
```tut:silent | ||
import cats.Id | ||
import scalaj.http._ | ||
|
||
val u2 = Github(accessToken).users.get("raulraja").exec[Id] | ||
object ProgramId extends github4s.ImplicitsJVM { | ||
val u2 = Github(accessToken).users.get("raulraja").exec[Id, HttpResponse[String]] | ||
} | ||
``` | ||
|
||
WIP: With `Future` | ||
|
@@ -67,19 +88,25 @@ import scala.concurrent.Future | |
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.Await | ||
import scalaj.http._ | ||
|
||
val u3 = Github(accessToken).users.get("dialelo").exec[Future] | ||
Await.result(u3, 2.seconds) | ||
object ProgramFuture extends github4s.ImplicitsJVM { | ||
val u3 = Github(accessToken).users.get("dialelo").exec[Future, HttpResponse[String]] | ||
Await.result(u3, 2.seconds) | ||
} | ||
``` | ||
|
||
WIP: With `scalaz.Task` | ||
|
||
```tut:silent | ||
import scalaz.concurrent.Task | ||
import github4s.scalaz.implicits._ | ||
import scalaj.http._ | ||
|
||
val u4 = Github(accessToken).users.get("franciscodr").exec[Task] | ||
u4.attemptRun | ||
object ProgramTask extends github4s.ImplicitsJVM { | ||
val u4 = Github(accessToken).users.get("franciscodr").exec[Task, HttpResponse[String]] | ||
u4.attemptRun | ||
} | ||
``` | ||
|
||
```tut:invisible | ||
|
@@ -90,13 +117,16 @@ import cats.implicits._ | |
import github4s.Github | ||
import github4s.Github._ | ||
import github4s.implicits._ | ||
import scalaj.http._ | ||
|
||
val accessToken = sys.props.get("token") | ||
``` | ||
|
||
```tut:book | ||
val user1 = Github(accessToken).users.get("rafaparadela").exec[Eval].value | ||
object ProgramEval extends github4s.ImplicitsJVM { | ||
val user1 = Github(accessToken).users.get("rafaparadela").exec[Eval, HttpResponse[String]].value | ||
} | ||
|
||
user1 should be ('right) | ||
user1.toOption map (_.result.login shouldBe "rafaparadela") | ||
ProgramEval.user1 should be ('right) | ||
ProgramEval.user1.toOption map (_.result.login shouldBe "rafaparadela") | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2016 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package github4s | ||
|
||
import scala.concurrent.Future | ||
import fr.hmil.roshttp._ | ||
import fr.hmil.roshttp.body.{BodyPart, BulkBodyPart} | ||
import java.nio.ByteBuffer | ||
|
||
import cats.implicits._ | ||
import fr.hmil.roshttp.response.SimpleHttpResponse | ||
import fr.hmil.roshttp.util.HeaderMap | ||
import fr.hmil.roshttp.body.Implicits._ | ||
import fr.hmil.roshttp.exceptions.HttpException | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import github4s.GithubResponses.{GHResponse, GHResult, JsonParsingException, UnexpectedException} | ||
import github4s.GithubDefaultUrls._ | ||
import github4s.Decoders._ | ||
import io.circe.Decoder | ||
import io.circe.parser._ | ||
import monix.reactive.Observable | ||
|
||
import scala.util.{Failure, Success} | ||
|
||
case class CirceJSONBody(value: String) extends BulkBodyPart { | ||
override def contentType: String = s"application/json; charset=utf-8" | ||
override def contentData: ByteBuffer = ByteBuffer.wrap(value.getBytes("utf-8")) | ||
} | ||
|
||
trait HttpRequestBuilderExtensionJS { | ||
|
||
import monix.execution.Scheduler.Implicits.global | ||
|
||
def userAgent = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be this method a |
||
val name = github4s.BuildInfo.name | ||
val version = github4s.BuildInfo.version | ||
s"$name/$version" | ||
} | ||
|
||
implicit def extensionJS: HttpRequestBuilderExtension[SimpleHttpResponse, Future] = | ||
new HttpRequestBuilderExtension[SimpleHttpResponse, Future] { | ||
def run[A](rb: HttpRequestBuilder[SimpleHttpResponse, Future])( | ||
implicit D: Decoder[A]): Future[GHResponse[A]] = { | ||
val request = HttpRequest(rb.url) | ||
.withMethod(Method(rb.httpVerb.verb)) | ||
.withQueryParameters(rb.params.toSeq: _*) | ||
.withHeader("content-type", "application/json") | ||
.withHeader("user-agent", userAgent) | ||
.withHeaders(rb.authHeader.toList: _*) | ||
.withHeaders(rb.headers.toList: _*) | ||
|
||
(rb.data match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just giving here my personal preferences for the following piece of code ;) :
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Elegant! Totally changing it to this! |
||
case Some(d) ⇒ | ||
request.send(CirceJSONBody(d)) | ||
case _ ⇒ request.send() | ||
}).map(r => toEntity[A](r)).recoverWith[GHResponse[A]] { | ||
case e => Future.successful(Either.left(UnexpectedException(e.getMessage))) | ||
} | ||
} | ||
} | ||
|
||
def toEntity[A](response: SimpleHttpResponse)(implicit D: Decoder[A]): GHResponse[A] = | ||
response match { | ||
case r if r.statusCode < 400 ⇒ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Status codes should be placed in single place, we prefer to do not have literals along the code when possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could move somewhere here: Does it make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally! |
||
decode[A](r.body).fold( | ||
e ⇒ Either.left(JsonParsingException(e.getMessage, r.body)), | ||
result ⇒ | ||
Either.right( | ||
GHResult(result, r.statusCode, rosHeaderMapToRegularMap(r.headers)) | ||
) | ||
) | ||
case r ⇒ | ||
Either.left( | ||
UnexpectedException( | ||
s"Failed invoking get with status : ${r.statusCode}, body : \n ${r.body}")) | ||
} | ||
|
||
private def rosHeaderMapToRegularMap( | ||
headers: HeaderMap[String]): Map[String, IndexedSeq[String]] = | ||
headers.flatMap(m => Map(m._1.toLowerCase -> IndexedSeq(m._2))) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2016 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package github4s | ||
|
||
import cats.Id | ||
import cats.instances.FutureInstances | ||
import github4s.free.interpreters.Interpreters | ||
import fr.hmil.roshttp.response.SimpleHttpResponse | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Future | ||
import github4s.implicits._ | ||
|
||
trait ImplicitsJS extends FutureInstances with HttpRequestBuilderExtensionJS { | ||
|
||
implicit val intInstanceFutureRosHttp = new Interpreters[Future, SimpleHttpResponse] | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this
fork in Test := false
be moved tosharedJsSettings
? In this way, the github4s module can be defined simply withlazy val github4sJS = github4s.js