Skip to content

Commit

Permalink
Merge pull request #39 from 47deg/js-add-user-agent-through-kleisli
Browse files Browse the repository at this point in the history
Adding custom user headers at exec time
  • Loading branch information
Javier de Silóniz Sandino committed Nov 3, 2016
2 parents a714e61 + e0ba903 commit 0abc720
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 107 deletions.
15 changes: 9 additions & 6 deletions docs/src/main/tut/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ WIP: Every Github4s api returns a `Free[GHResponse[A], A]` where `GHResponse[A]`
case class GHResult[A](result: A, statusCode: Int, headers: Map[String, IndexedSeq[String]])
```

For getting an user
To get an user

```tut:silent
val user1 = Github(accessToken).users.get("rafaparadela")
Expand All @@ -45,7 +45,7 @@ import github4s.Github._
import scalaj.http._
object ProgramEval {
val u1 = user1.exec[Eval, HttpResponse[String]].value
val u1 = user1.exec[Eval, HttpResponse[String]]().value
}
```
Expand All @@ -71,7 +71,7 @@ import cats.Id
import scalaj.http._
object ProgramId {
val u2 = Github(accessToken).users.get("raulraja").exec[Id, HttpResponse[String]]
val u2 = Github(accessToken).users.get("raulraja").exec[Id, HttpResponse[String]]()
}
```

Expand All @@ -86,7 +86,8 @@ import scala.concurrent.Await
import scalaj.http._
object ProgramFuture {
val u3 = Github(accessToken).users.get("dialelo").exec[Future, HttpResponse[String]]
// execFuture[C] is a convenience access to exec[Future, C]
val u3 = Github(accessToken).users.get("dialelo").execFuture[HttpResponse[String]]()
Await.result(u3, 2.seconds)
}
```
Expand All @@ -100,7 +101,7 @@ import scalaj.http._
import github4s.jvm.Implicits._
object ProgramTask {
val u4 = Github(accessToken).users.get("franciscodr").exec[Task, HttpResponse[String]]
val u4 = Github(accessToken).users.get("franciscodr").exec[Task, HttpResponse[String]]()
u4.attemptRun
}
```
Expand All @@ -120,7 +121,9 @@ val accessToken = sys.props.get("token")

```tut:book
object ProgramEval {
val user1 = Github(accessToken).users.get("rafaparadela").exec[Eval, HttpResponse[String]].value
// exec methods let users to include optional headers for any GitHub API request:
val userHeaders = Map("user-agent" -> "github4s")
val user1 = Github(accessToken).users.get("rafaparadela").exec[Eval, HttpResponse[String]](userHeaders).value
}
ProgramEval.user1 should be ('right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils {
validNote,
validClientId,
invalidClientSecret)
.exec[Future, SimpleHttpResponse]
.execFuture(headerUserAgent)

testFutureIsLeft(response)
}
Expand All @@ -52,7 +52,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils {
val response =
Github().auth
.authorizeUrl(validClientId, validRedirectUri, validScopes)
.exec[Future, SimpleHttpResponse]
.execFuture(headerUserAgent)

testFutureIsRight[Authorize](response, { r =>
r.result.url.contains(validRedirectUri) shouldBe true
Expand All @@ -63,7 +63,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils {
"Auth >> GetAccessToken" should "return error on Left for invalid code value" in {
val response = Github().auth
.getAccessToken(validClientId, invalidClientSecret, "", validRedirectUri, "")
.exec[Future, SimpleHttpResponse]
.execFuture(headerUserAgent)

testFutureIsLeft(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class GHGistsSpec extends AsyncFlatSpec with Matchers with TestUtils {
.newGist(validGistDescription,
validGistPublic,
Map(validGistFilename -> GistFile(validGistFileContent)))
.exec[Future, SimpleHttpResponse]
.execFuture(headerUserAgent)

testFutureIsRight[Gist](response, { r =>
r.result.description shouldBe validGistDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
"Repos >> Get" should "return the expected name when valid repo is provided" in {

val response =
Github(accessToken).repos.get(validRepoOwner, validRepoName).exec[Future, SimpleHttpResponse]
Github(accessToken).repos.get(validRepoOwner, validRepoName).execFuture(headerUserAgent)

testFutureIsRight[Repository](response, { r =>
r.result.name shouldBe validRepoName
Expand All @@ -48,9 +48,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {

it should "return error when an invalid repo name is passed" in {
val response =
Github(accessToken).repos
.get(validRepoOwner, invalidRepoName)
.exec[Future, SimpleHttpResponse]
Github(accessToken).repos.get(validRepoOwner, invalidRepoName).execFuture(headerUserAgent)

testFutureIsLeft(response)
}
Expand All @@ -59,7 +57,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
val response =
Github(accessToken).repos
.listCommits(validRepoOwner, validRepoName)
.exec[Future, SimpleHttpResponse]
.execFuture(headerUserAgent)

testFutureIsRight[List[Commit]](response, { r =>
r.result.nonEmpty shouldBe true
Expand All @@ -71,7 +69,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
val response =
Github(accessToken).repos
.listCommits(invalidRepoName, validRepoName)
.exec[Future, SimpleHttpResponse]
.execFuture(headerUserAgent)

testFutureIsLeft(response)
}
Expand All @@ -80,7 +78,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
val response =
Github(accessToken).repos
.listContributors(validRepoOwner, validRepoName)
.exec[Future, SimpleHttpResponse]
.execFuture(headerUserAgent)

testFutureIsRight[List[User]](response, { r =>
r.result shouldNot be(empty)
Expand All @@ -92,7 +90,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
val response =
Github(accessToken).repos
.listContributors(invalidRepoName, validRepoName)
.exec[Future, SimpleHttpResponse]
.execFuture(headerUserAgent)

testFutureIsLeft(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils {
override implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global

"Users >> Get" should "return the expected login for a valid username" in {
val response = Github(accessToken).users.get(validUsername).exec[Future, SimpleHttpResponse]
val response = Github(accessToken).users.get(validUsername).execFuture(headerUserAgent)

testFutureIsRight[User](response, { r =>
r.result.login shouldBe validUsername
Expand All @@ -44,18 +44,20 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils {
}

it should "return error on Left for invalid username" in {
val response = Github(accessToken).users.get(invalidUsername).exec[Future, SimpleHttpResponse]
val response = Github(accessToken).users.get(invalidUsername).execFuture(headerUserAgent)

testFutureIsLeft(response)
}

"Users >> GetAuth" should "return error on Left when no accessToken is provided" in {
val response = Github().users.getAuth.exec[Future, SimpleHttpResponse]
val response = Github().users.getAuth.exec[Future, SimpleHttpResponse](headerUserAgent)

testFutureIsLeft(response)
}

"Users >> GetUsers" should "return users for a valid since value" in {
val response =
Github(accessToken).users.getUsers(validSinceInt).exec[Future, SimpleHttpResponse]
Github(accessToken).users.getUsers(validSinceInt).execFuture(headerUserAgent)

testFutureIsRight[List[User]](response, { r =>
r.result.nonEmpty shouldBe true
Expand All @@ -65,7 +67,7 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils {

it should "return an empty list when a invalid since value is provided" in {
val response =
Github(accessToken).users.getUsers(invalidSinceInt).exec[Future, SimpleHttpResponse]
Github(accessToken).users.getUsers(invalidSinceInt).execFuture(headerUserAgent)

testFutureIsRight[List[User]](response, { r =>
r.result.isEmpty shouldBe true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ trait TestUtils extends Matchers {
}
}

val accessToken = Option(github4s.BuildInfo.token)
def tokenHeader = "token " + accessToken.getOrElse("")
val accessToken = Option(github4s.BuildInfo.token)
def tokenHeader = "token " + accessToken.getOrElse("")
val headerUserAgent = Map("user-agent" -> "github4s")

val validUsername = "rafaparadela"
val invalidUsername = "GHInvalidaUserName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ class GHAuthSpec extends FlatSpec with Matchers with TestUtils {
validNote,
validClientId,
invalidClientSecret)
.exec[Id, HttpResponse[String]]
.exec[Id, HttpResponse[String]](headerUserAgent)

response should be('left)
}

"Auth >> AuthorizeUrl" should "return the expected URL for valid username" in {
val response =
Github().auth
.authorizeUrl(validClientId, validRedirectUri, validScopes)
.exec[Id, HttpResponse[String]]
.exec[Id, HttpResponse[String]](headerUserAgent)

response should be('right)

response.toOption map { r
Expand All @@ -61,7 +63,7 @@ class GHAuthSpec extends FlatSpec with Matchers with TestUtils {
"Auth >> GetAccessToken" should "return error on Left for invalid code value" in {
val response = Github().auth
.getAccessToken(validClientId, invalidClientSecret, "", validRedirectUri, "")
.exec[Id, HttpResponse[String]]
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('left)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class GHGistsSpec extends FlatSpec with Matchers with TestUtils {
.newGist(validGistDescription,
validGistPublic,
Map(validGistFilename -> GistFile(validGistFileContent)))
.exec[Id, HttpResponse[String]]
.exec[Id, HttpResponse[String]](headerUserAgent)

response should be('right)
response.toOption map { r
r.result.description shouldBe validGistDescription
Expand Down
16 changes: 10 additions & 6 deletions github4s/jvm/src/test/scala/github4s/integration/GHReposSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
"Repos >> Get" should "return the expected name when valid repo is provided" in {

val response =
Github(accessToken).repos.get(validRepoOwner, validRepoName).exec[Id, HttpResponse[String]]
Github(accessToken).repos
.get(validRepoOwner, validRepoName)
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('right)
response.toOption map { r
r.result.name shouldBe validRepoName
Expand All @@ -47,14 +49,16 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {

it should "return error when an invalid repo name is passed" in {
val response =
Github(accessToken).repos.get(validRepoOwner, invalidRepoName).exec[Id, HttpResponse[String]]
Github(accessToken).repos
.get(validRepoOwner, invalidRepoName)
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('left)
}

"Repos >> ListCommits" should "return the expected list of commits for valid data" in {
val response = Github(accessToken).repos
.listCommits(validRepoOwner, validRepoName)
.exec[Id, HttpResponse[String]]
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('right)

response.toOption map { r
Expand All @@ -66,15 +70,15 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
it should "return error for invalid repo name" in {
val response = Github(accessToken).repos
.listCommits(invalidRepoName, validRepoName)
.exec[Id, HttpResponse[String]]
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('left)
}

"Repos >> ListContributors" should "return the expected list of contributors for valid data" in {
val response =
Github(accessToken).repos
.listContributors(validRepoOwner, validRepoName)
.exec[Id, HttpResponse[String]]
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('right)

response.toOption map { r
Expand All @@ -88,7 +92,7 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
val response =
Github(accessToken).repos
.listContributors(invalidRepoName, validRepoName)
.exec[Id, HttpResponse[String]]
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('left)
}

Expand Down
19 changes: 14 additions & 5 deletions github4s/jvm/src/test/scala/github4s/integration/GHUsersSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import scalaj.http.HttpResponse
class GHUsersSpec extends FlatSpec with Matchers with TestUtils {

"Users >> Get" should "return the expected login for a valid username" in {
val response = Github(accessToken).users.get(validUsername).exec[Id, HttpResponse[String]]
val response =
Github(accessToken).users.get(validUsername).exec[Id, HttpResponse[String]](headerUserAgent)

response should be('right)
response.toOption map { r
r.result.login shouldBe validUsername
Expand All @@ -43,17 +45,22 @@ class GHUsersSpec extends FlatSpec with Matchers with TestUtils {
}

it should "return error on Left for invalid username" in {
val response = Github(accessToken).users.get(invalidUsername).exec[Id, HttpResponse[String]]
val response = Github(accessToken).users
.get(invalidUsername)
.exec[Id, HttpResponse[String]](headerUserAgent)

response should be('left)
}

"Users >> GetAuth" should "return error on Left when no accessToken is provided" in {
val response = Github().users.getAuth.exec[Id, HttpResponse[String]]
val response = Github().users.getAuth.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('left)
}

"Users >> GetUsers" should "return users for a valid since value" in {
val response = Github(accessToken).users.getUsers(validSinceInt).exec[Id, HttpResponse[String]]
val response = Github(accessToken).users
.getUsers(validSinceInt)
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('right)

response.toOption map { r
Expand All @@ -64,7 +71,9 @@ class GHUsersSpec extends FlatSpec with Matchers with TestUtils {

it should "return an empty list when a invalid since value is provided" in {
val response =
Github(accessToken).users.getUsers(invalidSinceInt).exec[Id, HttpResponse[String]]
Github(accessToken).users
.getUsers(invalidSinceInt)
.exec[Id, HttpResponse[String]](headerUserAgent)
response should be('right)

response.toOption map { r
Expand Down

0 comments on commit 0abc720

Please sign in to comment.