Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stringbean/sttp-scribe
sttp backend for ScribeJava
https://github.com/stringbean/sttp-scribe
oauth2-client scala scribejava sttp
Last synced: 11 days ago
JSON representation
sttp backend for ScribeJava
- Host: GitHub
- URL: https://github.com/stringbean/sttp-scribe
- Owner: stringbean
- License: apache-2.0
- Created: 2018-07-19T08:53:14.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-05-22T13:40:39.000Z (6 months ago)
- Last Synced: 2024-05-22T13:54:10.266Z (6 months ago)
- Topics: oauth2-client, scala, scribejava, sttp
- Language: Scala
- Homepage:
- Size: 137 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sttp-scribe - sttp backend for ScribeJava
[![Build Status](https://img.shields.io/github/actions/workflow/status/stringbean/sttp-scribe/ci.yml)](https://github.com/stringbean/sttp-scribe/actions/workflows/ci.yml)
[![Codacy Grade](https://img.shields.io/codacy/grade/6becacf763074472b1360c0d41cd8478.svg?label=codacy)](https://www.codacy.com/app/stringbean/sttp-scribe)
[![Test Coverage](https://img.shields.io/codecov/c/github/stringbean/sttp-scribe/master.svg)](https://codecov.io/gh/stringbean/sttp-scribe)
[![Maven Central - Scala 2.12](https://img.shields.io/maven-central/v/software.purpledragon/sttp-scribe_2.12.svg?label=scala%202.12)](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22sttp-scribe_2.12%22)
[![Maven Central - Scala 2.13](https://img.shields.io/maven-central/v/software.purpledragon/sttp-scribe_2.13.svg?label=scala%202.13)](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22sttp-scribe_2.13%22)A backend implementation for [sttp](https://github.com/softwaremill/sttp) that allows you to use
[ScribeJava](https://github.com/scribejava/scribejava) as a backend. Now you can call OAuth endpoints using sttp!## Quickstart
Add sttp-scribe, sttp and Scribe to your project:
```scala
libraryDependencies ++= Seq(
"software.purpledragon" %% "sttp-scribe" % ,
"com.softwaremill.sttp.client" %% "core" % "2.3.0",
"com.github.scribejava" % "scribejava-apis" % "8.3.3",
)
```Setup your Scribe service:
```scala
val service = new ServiceBuilder("api-key")
.apiSecret("api-secret")
.callback("http://www.example.com/oauth_callback/")
.build(GitHubApi.instance())
```Create a token provider:
```scala
val tokenProvider = new OAuth2TokenProvider() {
private var currentToken: Option[OAuth2AccessToken] = Noneoverride def accessTokenForRequest: OAuth2AccessToken = {
currentToken.getOrElse {
// fetch from DB or another source
}
}override def tokenRenewed(newToken: OAuth2AccessToken): Unit = {
currentToken = Some(newToken)
// persist token to DB
}
}
```Then create a `ScribeOAuth20Backend` and use sttp to make authenticated calls:
```scala
import software.purpledragon.sttp.scribe.MonadAdaptor.Implicits.identityimplicit val monadError: MonadError[Identity] = IdMonad
implicit val backend: SttpBackend[Identity, Nothing, NothingT] =
new ScribeOAuth20Backend(service, tokenProvider)val response: Response = basicRequest
.get(uri"https://api.github.com/users/octocat")
.send()
```The Scribe backend will take care of refreshing the access token when it expires and retrying the current API call.
## Asynchronous Backend
Scribe provides an asynchronous NIO based implementation that can be used by switching the monad:
```scala
import software.purpledragon.sttp.scribe.MonadAdaptor.Implicits.futureimplicit val ec: ExecutionContext = ExecutionContext.Implicits.global
implicit val monadError: MonadError[Future] = new FutureMonad()
implicit val backend: SttpBackend[Future, Nothing, NothingT] =
new ScribeOAuth20Backend(service, tokenProvider)val response: Future[Response] = basicRequest
.get(uri"https://api.github.com/users/octocat")
.send()
```