-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow sending different auth headers to GitHub4s (#875)
* Allow sending different auth headers to GitHub4s * Adds mima and keeps compatibility on public API * Reoves unused import [skip ci] * Changes for improving compatibilty * Fixes auth * Removes comment block * scalafmt
- Loading branch information
1 parent
7589d64
commit dbc6012
Showing
23 changed files
with
346 additions
and
203 deletions.
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
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
60 changes: 60 additions & 0 deletions
60
github4s/shared/src/main/scala/github4s/GithubClient.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,60 @@ | ||
/* | ||
* Copyright 2016-2023 47 Degrees Open Source <https://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package github4s | ||
|
||
import cats.effect.Concurrent | ||
import github4s.algebras._ | ||
import github4s.interpreters.StaticAccessToken | ||
import github4s.modules._ | ||
import org.http4s.client.Client | ||
|
||
private[github4s] class GithubClient[F[_]: Concurrent]( | ||
client: Client[F], | ||
authHeader: AccessHeader[F] | ||
)(implicit config: GithubConfig) | ||
extends GithubAPIs[F] { | ||
|
||
private lazy val module: GithubAPIs[F] = new GithubAPIsV3[F](client, config, authHeader) | ||
|
||
lazy val users: Users[F] = module.users | ||
lazy val repos: Repositories[F] = module.repos | ||
lazy val auth: Auth[F] = module.auth | ||
lazy val gists: Gists[F] = module.gists | ||
lazy val issues: Issues[F] = module.issues | ||
lazy val activities: Activities[F] = module.activities | ||
lazy val gitData: GitData[F] = module.gitData | ||
lazy val pullRequests: PullRequests[F] = module.pullRequests | ||
lazy val organizations: Organizations[F] = module.organizations | ||
lazy val teams: Teams[F] = module.teams | ||
lazy val projects: Projects[F] = module.projects | ||
lazy val search: Search[F] = module.search | ||
} | ||
|
||
object GithubClient { | ||
|
||
def apply[F[_]: Concurrent]( | ||
client: Client[F], | ||
accessToken: Option[String] = None | ||
)(implicit config: GithubConfig): GithubAPIs[F] = | ||
new GithubClient[F](client, AccessHeader.from(new StaticAccessToken(accessToken))) | ||
|
||
def apply[F[_]: Concurrent]( | ||
client: Client[F], | ||
authHeader: AccessHeader[F] | ||
)(implicit config: GithubConfig): GithubAPIs[F] = | ||
new GithubClient[F](client, authHeader) | ||
} |
16 changes: 16 additions & 0 deletions
16
github4s/shared/src/main/scala/github4s/algebras/AccessHeader.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,16 @@ | ||
package github4s.algebras | ||
|
||
import github4s.GHResponse | ||
|
||
trait AccessHeader[F[_]] { | ||
def withAccessHeader[T](f: Map[String, String] => F[GHResponse[T]]): F[GHResponse[T]] | ||
} | ||
|
||
object AccessHeader { | ||
def from[F[_]](accessToken: AccessToken[F]): AccessHeader[F] = new AccessHeader[F] { | ||
override def withAccessHeader[T](f: Map[String, String] => F[GHResponse[T]]): F[GHResponse[T]] = | ||
accessToken.withAccessToken { token => | ||
f(token.fold(Map.empty[String, String])(t => Map("Authorization" -> s"token $t"))) | ||
} | ||
} | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
github4s/shared/src/main/scala/github4s/interpreters/StaticAccessHeader.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,13 @@ | ||
package github4s.interpreters | ||
|
||
import github4s.GHResponse | ||
import github4s.algebras.AccessHeader | ||
|
||
case class StaticAccessHeader[F[_]](accessHeader: Map[String, String]) extends AccessHeader[F] { | ||
override def withAccessHeader[T](f: Map[String, String] => F[GHResponse[T]]): F[GHResponse[T]] = | ||
f(accessHeader) | ||
} | ||
|
||
object StaticAccessHeader { | ||
def noHeader[F[_]] = new StaticAccessHeader[F](Map.empty) | ||
} |
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
Oops, something went wrong.