Skip to content

Commit 0abc720

Browse files
author
Javier de Silóniz Sandino
authored
Merge pull request #39 from 47deg/js-add-user-agent-through-kleisli
Adding custom user headers at exec time
2 parents a714e61 + e0ba903 commit 0abc720

19 files changed

Lines changed: 213 additions & 107 deletions

File tree

docs/src/main/tut/docs.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ WIP: Every Github4s api returns a `Free[GHResponse[A], A]` where `GHResponse[A]`
3131
case class GHResult[A](result: A, statusCode: Int, headers: Map[String, IndexedSeq[String]])
3232
```
3333

34-
For getting an user
34+
To get an user
3535

3636
```tut:silent
3737
val user1 = Github(accessToken).users.get("rafaparadela")
@@ -45,7 +45,7 @@ import github4s.Github._
4545
import scalaj.http._
4646
4747
object ProgramEval {
48-
val u1 = user1.exec[Eval, HttpResponse[String]].value
48+
val u1 = user1.exec[Eval, HttpResponse[String]]().value
4949
}
5050
5151
```
@@ -71,7 +71,7 @@ import cats.Id
7171
import scalaj.http._
7272
7373
object ProgramId {
74-
val u2 = Github(accessToken).users.get("raulraja").exec[Id, HttpResponse[String]]
74+
val u2 = Github(accessToken).users.get("raulraja").exec[Id, HttpResponse[String]]()
7575
}
7676
```
7777

@@ -86,7 +86,8 @@ import scala.concurrent.Await
8686
import scalaj.http._
8787
8888
object ProgramFuture {
89-
val u3 = Github(accessToken).users.get("dialelo").exec[Future, HttpResponse[String]]
89+
// execFuture[C] is a convenience access to exec[Future, C]
90+
val u3 = Github(accessToken).users.get("dialelo").execFuture[HttpResponse[String]]()
9091
Await.result(u3, 2.seconds)
9192
}
9293
```
@@ -100,7 +101,7 @@ import scalaj.http._
100101
import github4s.jvm.Implicits._
101102
102103
object ProgramTask {
103-
val u4 = Github(accessToken).users.get("franciscodr").exec[Task, HttpResponse[String]]
104+
val u4 = Github(accessToken).users.get("franciscodr").exec[Task, HttpResponse[String]]()
104105
u4.attemptRun
105106
}
106107
```
@@ -120,7 +121,9 @@ val accessToken = sys.props.get("token")
120121

121122
```tut:book
122123
object ProgramEval {
123-
val user1 = Github(accessToken).users.get("rafaparadela").exec[Eval, HttpResponse[String]].value
124+
// exec methods let users to include optional headers for any GitHub API request:
125+
val userHeaders = Map("user-agent" -> "github4s")
126+
val user1 = Github(accessToken).users.get("rafaparadela").exec[Eval, HttpResponse[String]](userHeaders).value
124127
}
125128
126129
ProgramEval.user1 should be ('right)

github4s/js/src/test/scala/github4s/utils/integration/GHAuthSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils {
4343
validNote,
4444
validClientId,
4545
invalidClientSecret)
46-
.exec[Future, SimpleHttpResponse]
46+
.execFuture(headerUserAgent)
4747

4848
testFutureIsLeft(response)
4949
}
@@ -52,7 +52,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils {
5252
val response =
5353
Github().auth
5454
.authorizeUrl(validClientId, validRedirectUri, validScopes)
55-
.exec[Future, SimpleHttpResponse]
55+
.execFuture(headerUserAgent)
5656

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

6868
testFutureIsLeft(response)
6969
}

github4s/js/src/test/scala/github4s/utils/integration/GHGistsSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GHGistsSpec extends AsyncFlatSpec with Matchers with TestUtils {
3939
.newGist(validGistDescription,
4040
validGistPublic,
4141
Map(validGistFilename -> GistFile(validGistFileContent)))
42-
.exec[Future, SimpleHttpResponse]
42+
.execFuture(headerUserAgent)
4343

4444
testFutureIsRight[Gist](response, { r =>
4545
r.result.description shouldBe validGistDescription

github4s/js/src/test/scala/github4s/utils/integration/GHReposSpec.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
3838
"Repos >> Get" should "return the expected name when valid repo is provided" in {
3939

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

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

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

5553
testFutureIsLeft(response)
5654
}
@@ -59,7 +57,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
5957
val response =
6058
Github(accessToken).repos
6159
.listCommits(validRepoOwner, validRepoName)
62-
.exec[Future, SimpleHttpResponse]
60+
.execFuture(headerUserAgent)
6361

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

7674
testFutureIsLeft(response)
7775
}
@@ -80,7 +78,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
8078
val response =
8179
Github(accessToken).repos
8280
.listContributors(validRepoOwner, validRepoName)
83-
.exec[Future, SimpleHttpResponse]
81+
.execFuture(headerUserAgent)
8482

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

9795
testFutureIsLeft(response)
9896
}

github4s/js/src/test/scala/github4s/utils/integration/GHUsersSpec.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils {
3535
override implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global
3636

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

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

4646
it should "return error on Left for invalid username" in {
47-
val response = Github(accessToken).users.get(invalidUsername).exec[Future, SimpleHttpResponse]
47+
val response = Github(accessToken).users.get(invalidUsername).execFuture(headerUserAgent)
48+
4849
testFutureIsLeft(response)
4950
}
5051

5152
"Users >> GetAuth" should "return error on Left when no accessToken is provided" in {
52-
val response = Github().users.getAuth.exec[Future, SimpleHttpResponse]
53+
val response = Github().users.getAuth.exec[Future, SimpleHttpResponse](headerUserAgent)
54+
5355
testFutureIsLeft(response)
5456
}
5557

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

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

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

7072
testFutureIsRight[List[User]](response, { r =>
7173
r.result.isEmpty shouldBe true

github4s/js/src/test/scala/github4s/utils/utils/TestUtils.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ trait TestUtils extends Matchers {
5151
}
5252
}
5353

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

5758
val validUsername = "rafaparadela"
5859
val invalidUsername = "GHInvalidaUserName"

github4s/jvm/src/test/scala/github4s/integration/GHAuthSpec.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ class GHAuthSpec extends FlatSpec with Matchers with TestUtils {
4040
validNote,
4141
validClientId,
4242
invalidClientSecret)
43-
.exec[Id, HttpResponse[String]]
43+
.exec[Id, HttpResponse[String]](headerUserAgent)
44+
4445
response should be('left)
4546
}
4647

4748
"Auth >> AuthorizeUrl" should "return the expected URL for valid username" in {
4849
val response =
4950
Github().auth
5051
.authorizeUrl(validClientId, validRedirectUri, validScopes)
51-
.exec[Id, HttpResponse[String]]
52+
.exec[Id, HttpResponse[String]](headerUserAgent)
53+
5254
response should be('right)
5355

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

github4s/jvm/src/test/scala/github4s/integration/GHGistsSpec.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class GHGistsSpec extends FlatSpec with Matchers with TestUtils {
3838
.newGist(validGistDescription,
3939
validGistPublic,
4040
Map(validGistFilename -> GistFile(validGistFileContent)))
41-
.exec[Id, HttpResponse[String]]
41+
.exec[Id, HttpResponse[String]](headerUserAgent)
42+
4243
response should be('right)
4344
response.toOption map { r
4445
r.result.description shouldBe validGistDescription

github4s/jvm/src/test/scala/github4s/integration/GHReposSpec.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
3737
"Repos >> Get" should "return the expected name when valid repo is provided" in {
3838

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

4850
it should "return error when an invalid repo name is passed" in {
4951
val response =
50-
Github(accessToken).repos.get(validRepoOwner, invalidRepoName).exec[Id, HttpResponse[String]]
52+
Github(accessToken).repos
53+
.get(validRepoOwner, invalidRepoName)
54+
.exec[Id, HttpResponse[String]](headerUserAgent)
5155
response should be('left)
5256
}
5357

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

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

7377
"Repos >> ListContributors" should "return the expected list of contributors for valid data" in {
7478
val response =
7579
Github(accessToken).repos
7680
.listContributors(validRepoOwner, validRepoName)
77-
.exec[Id, HttpResponse[String]]
81+
.exec[Id, HttpResponse[String]](headerUserAgent)
7882
response should be('right)
7983

8084
response.toOption map { r
@@ -88,7 +92,7 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
8892
val response =
8993
Github(accessToken).repos
9094
.listContributors(invalidRepoName, validRepoName)
91-
.exec[Id, HttpResponse[String]]
95+
.exec[Id, HttpResponse[String]](headerUserAgent)
9296
response should be('left)
9397
}
9498

github4s/jvm/src/test/scala/github4s/integration/GHUsersSpec.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ import scalaj.http.HttpResponse
3434
class GHUsersSpec extends FlatSpec with Matchers with TestUtils {
3535

3636
"Users >> Get" should "return the expected login for a valid username" in {
37-
val response = Github(accessToken).users.get(validUsername).exec[Id, HttpResponse[String]]
37+
val response =
38+
Github(accessToken).users.get(validUsername).exec[Id, HttpResponse[String]](headerUserAgent)
39+
3840
response should be('right)
3941
response.toOption map { r
4042
r.result.login shouldBe validUsername
@@ -43,17 +45,22 @@ class GHUsersSpec extends FlatSpec with Matchers with TestUtils {
4345
}
4446

4547
it should "return error on Left for invalid username" in {
46-
val response = Github(accessToken).users.get(invalidUsername).exec[Id, HttpResponse[String]]
48+
val response = Github(accessToken).users
49+
.get(invalidUsername)
50+
.exec[Id, HttpResponse[String]](headerUserAgent)
51+
4752
response should be('left)
4853
}
4954

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

5560
"Users >> GetUsers" should "return users for a valid since value" in {
56-
val response = Github(accessToken).users.getUsers(validSinceInt).exec[Id, HttpResponse[String]]
61+
val response = Github(accessToken).users
62+
.getUsers(validSinceInt)
63+
.exec[Id, HttpResponse[String]](headerUserAgent)
5764
response should be('right)
5865

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

6572
it should "return an empty list when a invalid since value is provided" in {
6673
val response =
67-
Github(accessToken).users.getUsers(invalidSinceInt).exec[Id, HttpResponse[String]]
74+
Github(accessToken).users
75+
.getUsers(invalidSinceInt)
76+
.exec[Id, HttpResponse[String]](headerUserAgent)
6877
response should be('right)
6978

7079
response.toOption map { r

0 commit comments

Comments
 (0)