-
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a922b56
Moves the code to the different modules. WIP
juanpedromoreno aed4702
Merge branch 'master' into g4s-10-support-for-scalajs
juanpedromoreno 34cbe91
Merge branch 'master' into g4s-10-support-for-scalajs
juanpedromoreno 042ef1c
WIP
2e3a545
Merge branch 'g4s-10-support-for-scalajs' of github.com:47deg/github4…
ef4298e
Still WIP - solves major problems with implicits
3d266d3
Still WIP - solves major problems with implicits
311661b
WIP - fixed problems with implicits
586586a
WIP - Fixed compilation issues in tests for JVM
c9515eb
WIP - tests compiling/passing
af091aa
Fixed publishing to local - Switched to latest version of ROSHTTP
0a84652
Added user-agent header to requests from the JS side
683cda3
Fixed issues with tests both in JVM and JS sides
b2b09f2
Excluding JS side from scoverage, as it's not compatible with ScalaJS…
7040a3c
Fixing issues with tut docs
8ba724e
Fixing changes from PRs
fcaf13c
Updating docs to reflect the use of GitHub access tokens to perform t…
544652b
Added objects for easier implicit handling for both JVM and JS projec…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ target/ | |
.idea | ||
docs/src/jekyll/_site/ | ||
docs/src/jekyll/*.md | ||
.DS_Store | ||
|
||
# PGP keys | ||
*.gpg |
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 |
---|---|---|
|
@@ -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,23 @@ 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") | ||
``` | ||
|
||
# Test credentials | ||
|
||
Note that for github4s to have access to the GitHub API during the test phases, you need to provide a valid access token with the right credentials (i.e.: users + gists scopes), through the sbt configuration variable "token": | ||
|
||
sbt -Dtoken=ACCESS_TOKEN_STRING | ||
``` |
103 changes: 103 additions & 0 deletions
103
github4s/js/src/main/scala/github4s/HttpRequestBuilderExtensionJS.scala
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,103 @@ | ||
/* | ||
* 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 github4s.HttpClient.HttpCode400 | ||
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 | ||
|
||
val userAgent = { | ||
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 | ||
.map(d => request.send(CirceJSONBody(d))) | ||
.getOrElse(request.send()) | ||
.map(toEntity[A]) | ||
.recoverWith { | ||
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 < HttpCode400.statusCode ⇒ | ||
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))) | ||
|
||
} |
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,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] | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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