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

cask.Endpoint for Jackson #121

Open
gabrieljones opened this issue Mar 20, 2024 · 0 comments
Open

cask.Endpoint for Jackson #121

gabrieljones opened this issue Mar 20, 2024 · 0 comments

Comments

@gabrieljones
Copy link

gabrieljones commented Mar 20, 2024

I attempted to create my own cask.Endpoint using Jackson, but I got lost quickly. I was able to create a @getJackson() equivalent to @cask.getJson(). But @putJackson() eludes me. Please help.

Here is my getJackson()

trait JacksonData extends Response.Data
object JacksonData extends DataCompanion[JacksonData]{
  val (reader: ObjectReader, writer: ObjectWriter, mapper: JsonMapper) = buildReaderWriterMapper
  implicit class JacksonDataImpl(t: JsonNode) extends JacksonData {
    def headers: Seq[(String, String)] = Seq("Content-Type" -> "application/json")
    def write(out: OutputStream): Unit = {
      writer.writeValue(out, t)
      out.flush()
    }
  }
}

class getJackson(val path: String, override val subpath: Boolean = false)
  extends HttpEndpoint[Response[JacksonData], Seq[String]]{
  val methods: Seq[String] = Seq("get")
  type InputParser[T] = QueryParamReader[T]
  def wrapFunction(ctx: Request, delegate: Delegate): Result[Response.Raw] = {
    delegate(WebEndpoint.buildMapFromQueryParams(ctx))
  }
  def wrapPathSegment(s: String): Seq[String] = Seq(s)
}
  def buildReaderWriterMapper: (ObjectReader, ObjectWriter, JsonMapper) = {
    val mapper = JsonMapper.builder()
      .addModule(new JavaTimeModule())
      .addModule(DefaultScalaModule)
      .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
      .enable(MapperFeature.APPLY_DEFAULT_VALUES)
      .build()
    (mapper.reader, mapper.writer, mapper)
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant