Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@command and @query annotations should be provided by tapiro #220

Open
giuscri opened this issue Apr 1, 2020 · 5 comments
Open

@command and @query annotations should be provided by tapiro #220

giuscri opened this issue Apr 1, 2020 · 5 comments

Comments

@giuscri
Copy link
Contributor

giuscri commented Apr 1, 2020

Asking users to implement them via

import scala.annotations.StaticAnnotation
class query extends StaticAnnotation

means they have to define them in random files of their package. They can't put the definitions at the top of each trait Controller because they'll clash as soon as the package has >= 2 controllers.

Why not defining them in a library such that users can import them as many times they need?

@gabro
Copy link
Member

gabro commented Apr 1, 2020

I agree in principle, although it's a bit strange to have a library dependency for two lines of code. @calippo what do you think?

@gabro
Copy link
Member

gabro commented Apr 1, 2020

Another idea we can explore is not to require annotations, but use comments instead, as in

trait MyController[F[_], AuthToken] {
  // tapiro:query
  def myGet(): F[Error, String]

  // tapiro:command
  def myPost(): F[Error, String]
}

@calippo
Copy link
Member

calippo commented Apr 1, 2020

Need to think more about this.

I don't love comments. I think we can write a small library for this, it might include also a tapiro auth token with deserialization

import sttp.tapir._
import sttp.tapir.Codec._

case class CustomAuth(token: String)

def decodeAuth(s: String): DecodeResult[CustomAuth] = {
  val TokenPattern = "Token token=(.+)".r
  s match {
    case TokenPattern(token) => DecodeResult.Value(CustomAuth(token))
    case _                   => DecodeResult.Error(s, new Exception("token not found"))
  }
}

def encodeAuth(auth: CustomAuth): String = auth.token

implicit val authCodec: PlainCodec[CustomAuth] = Codec.stringPlainCodecUtf8
  .mapDecode(decodeAuth)(encodeAuth)

@gabro
Copy link
Member

gabro commented Apr 1, 2020

Partially related: should tapiro define an AuthToken? I would start thinking to move it to toctoc

@calippo
Copy link
Member

calippo commented Apr 6, 2020

I don't think there's a way to completely decouple the authentication from an HTTP library. I think it's fine to embrace it to a certain extent.

To me it is ok having a lightweight "token handling module" in tapiro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants