-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
405 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
ofs { | ||
base-url = "http://localhost:8080" | ||
request-timeout = 20s | ||
} |
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,7 @@ | ||
package com.github.mmvpm.bot | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
|
||
case class Config(ofs: OfsConfig) | ||
|
||
case class OfsConfig(baseUrl: String, requestTimeout: FiniteDuration) |
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
15 changes: 15 additions & 0 deletions
15
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/OfsClient.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,15 @@ | ||
package com.github.mmvpm.bot.client.ofs | ||
|
||
import cats.data.EitherT | ||
import com.github.mmvpm.model.{OfferDescription, Session} | ||
import com.github.mmvpm.bot.client.ofs.error.OfsClientError | ||
import com.github.mmvpm.bot.client.ofs.response.{CreateOfferResponse, SignInResponse, SignUpResponse, UserIdResponse} | ||
|
||
trait OfsClient[F[_]] { | ||
|
||
def signUp(name: String, login: String, password: String): EitherT[F, OfsClientError, SignUpResponse] | ||
def signIn(login: String, password: String): EitherT[F, OfsClientError, SignInResponse] | ||
def whoami(session: Session): EitherT[F, OfsClientError, UserIdResponse] | ||
|
||
def createOffer(session: Session, description: OfferDescription): EitherT[F, OfsClientError, CreateOfferResponse] | ||
} |
86 changes: 86 additions & 0 deletions
86
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/OfsClientSttp.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,86 @@ | ||
package com.github.mmvpm.bot.client.ofs | ||
|
||
import cats.data.EitherT | ||
import cats.MonadThrow | ||
import cats.implicits.{toBifunctorOps, toFunctorOps} | ||
import com.github.mmvpm.model.{OfferDescription, Session} | ||
import com.github.mmvpm.bot.OfsConfig | ||
import com.github.mmvpm.bot.client.ofs.request._ | ||
import com.github.mmvpm.bot.client.ofs.response._ | ||
import com.github.mmvpm.bot.client.ofs.error._ | ||
import io.circe.generic.auto._ | ||
import io.circe.Error | ||
import sttp.client3._ | ||
import sttp.client3.circe._ | ||
|
||
class OfsClientSttp[F[_]: MonadThrow](ofsConfig: OfsConfig, sttpBackend: SttpBackend[F, Any]) extends OfsClient[F] { | ||
|
||
def signUp(name: String, login: String, password: String): EitherT[F, OfsClientError, SignUpResponse] = { | ||
val requestUri = uri"${ofsConfig.baseUrl}/api/v1/auth/sign-up" | ||
val request = SignUpRequest(name, login, password) | ||
|
||
val response = basicRequest | ||
.post(requestUri) | ||
.body(request) | ||
.response(asJsonEither[OfsApiClientError, SignUpResponse]) | ||
.readTimeout(ofsConfig.requestTimeout) | ||
.send(sttpBackend) | ||
.map(_.body.leftMap(parseFailure)) | ||
|
||
EitherT(response) | ||
} | ||
|
||
override def signIn(login: String, password: String): EitherT[F, OfsClientError, SignInResponse] = { | ||
val requestUri = uri"${ofsConfig.baseUrl}/api/v1/auth/sign-in" | ||
|
||
val response = basicRequest | ||
.post(requestUri) | ||
.auth | ||
.basic(login, password) | ||
.response(asJsonEither[OfsApiClientError, SignInResponse]) | ||
.readTimeout(ofsConfig.requestTimeout) | ||
.send(sttpBackend) | ||
.map(_.body.leftMap(parseFailure)) | ||
|
||
EitherT(response) | ||
} | ||
|
||
override def createOffer( | ||
session: Session, | ||
description: OfferDescription | ||
): EitherT[F, OfsClientError, CreateOfferResponse] = { | ||
val requestUri = uri"${ofsConfig.baseUrl}/api/v1/offer" | ||
val request = CreateOfferRequest(description) | ||
|
||
val response = basicRequest | ||
.post(requestUri) | ||
.body(request) | ||
.header(SessionHeaderName, session.toString) | ||
.response(asJsonEither[OfsApiClientError, CreateOfferResponse]) | ||
.readTimeout(ofsConfig.requestTimeout) | ||
.send(sttpBackend) | ||
.map(_.body.leftMap(parseFailure)) | ||
|
||
EitherT(response) | ||
} | ||
|
||
def whoami(session: Session): EitherT[F, OfsClientError, UserIdResponse] = { | ||
val requestUri = uri"${ofsConfig.baseUrl}/api/v1/auth/whoami/$session" | ||
|
||
val response = basicRequest | ||
.get(requestUri) | ||
.response(asJsonEither[OfsApiClientError, UserIdResponse]) | ||
.readTimeout(ofsConfig.requestTimeout) | ||
.send(sttpBackend) | ||
.map(_.body.leftMap(parseFailure)) | ||
|
||
EitherT(response) | ||
} | ||
|
||
// internal | ||
|
||
private def parseFailure: ResponseException[OfsApiClientError, Error] => OfsClientError = { | ||
case HttpError(body, _) => body | ||
case error => OfsUnknownClientError(error.getMessage) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/error/package.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,12 @@ | ||
package com.github.mmvpm.bot.client.ofs | ||
|
||
package object error { | ||
|
||
trait OfsClientError { | ||
def details: String | ||
} | ||
|
||
case class OfsApiClientError(id: String, code: Int, details: String) extends OfsClientError | ||
|
||
case class OfsUnknownClientError(details: String) extends OfsClientError | ||
} |
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,5 @@ | ||
package com.github.mmvpm.bot.client | ||
|
||
package object ofs { | ||
val SessionHeaderName = "X-Auth-Session" | ||
} |
5 changes: 5 additions & 0 deletions
5
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/request/CreateOfferRequest.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,5 @@ | ||
package com.github.mmvpm.bot.client.ofs.request | ||
|
||
import com.github.mmvpm.model.OfferDescription | ||
|
||
case class CreateOfferRequest(description: OfferDescription) |
3 changes: 3 additions & 0 deletions
3
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/request/SignUpRequest.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,3 @@ | ||
package com.github.mmvpm.bot.client.ofs.request | ||
|
||
case class SignUpRequest(name: String, login: String, password: String) |
5 changes: 5 additions & 0 deletions
5
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/request/WhoamiRequest.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,5 @@ | ||
package com.github.mmvpm.bot.client.ofs.request | ||
|
||
import com.github.mmvpm.model.Session | ||
|
||
case class WhoamiRequest(session: Session) |
7 changes: 7 additions & 0 deletions
7
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/response/CreateOfferResponse.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,7 @@ | ||
package com.github.mmvpm.bot.client.ofs.response | ||
|
||
import com.github.mmvpm.model.OfferID | ||
|
||
case class CreateOfferResponse(offer: OfsOffer) | ||
|
||
case class OfsOffer(id: OfferID) // it's not necessary to copy all fields |
5 changes: 5 additions & 0 deletions
5
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/response/SignInResponse.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,5 @@ | ||
package com.github.mmvpm.bot.client.ofs.response | ||
|
||
import com.github.mmvpm.model.Session | ||
|
||
case class SignInResponse(session: Session) |
5 changes: 5 additions & 0 deletions
5
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/response/SignUpResponse.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,5 @@ | ||
package com.github.mmvpm.bot.client.ofs.response | ||
|
||
case class SignUpResponse(user: OfsUser) | ||
|
||
case class OfsUser(id: String) // not necessary to copy all fields |
5 changes: 5 additions & 0 deletions
5
bot/src/main/scala/com/github/mmvpm/bot/client/ofs/response/UserIdResponse.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,5 @@ | ||
package com.github.mmvpm.bot.client.ofs.response | ||
|
||
import com.github.mmvpm.model.UserID | ||
|
||
case class UserIdResponse(userId: UserID) |
13 changes: 13 additions & 0 deletions
13
bot/src/main/scala/com/github/mmvpm/bot/manager/ofs/OfsManager.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 com.github.mmvpm.bot.manager.ofs | ||
|
||
import cats.data.EitherT | ||
import com.bot4s.telegram.models.{Chat, Message} | ||
import com.github.mmvpm.bot.manager.ofs.error.OfsError | ||
import com.github.mmvpm.bot.manager.ofs.response.LoginOrRegisterResponse | ||
import com.github.mmvpm.model.OfferDescription | ||
|
||
trait OfsManager[F[_]] { | ||
def login(implicit message: Message): EitherT[F, OfsError, LoginOrRegisterResponse.LoggedIn] | ||
def loginOrRegister(implicit message: Message): EitherT[F, OfsError, LoginOrRegisterResponse] | ||
def createOffer(description: OfferDescription)(implicit message: Message): EitherT[F, OfsError, Unit] | ||
} |
Oops, something went wrong.