fixing bugs
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
b22a1230c7
commit
7b3be31271
@ -1,21 +1,37 @@
|
||||
package org.tbasket.handler
|
||||
|
||||
import io.netty.handler.codec.http.QueryStringDecoder
|
||||
import org.tbasket.error.*
|
||||
import zio.{Task, ZIO}
|
||||
import zio.http.Body
|
||||
import zio.http.api.openapi.OpenAPI.Parameter.QueryStyle.Form
|
||||
import zio.json.*
|
||||
import zio.json.ast.{Json, JsonCursor}
|
||||
import zio.{Task, ZIO}
|
||||
|
||||
import java.net.URLDecoder
|
||||
import java.nio.charset.StandardCharsets
|
||||
import scala.jdk.CollectionConverters.*
|
||||
import scala.language.reflectiveCalls
|
||||
|
||||
|
||||
object HandlerUtils {
|
||||
def parseAttribute[V, T <: Json {def value: V}](json: Json, name: String, cursor: JsonCursor[Json, T]): Task[V] =
|
||||
ZIO.fromEither(json.get[T](cursor).map(_.value))
|
||||
.mapError(InvalidRequest(s"Missing or invalid field $name.", _))
|
||||
|
||||
def parseAttributeOpt[V, T <: Json {def value: V}](json: Json, name: String, cursor: JsonCursor[Json, T]) =
|
||||
ZIO.fromEither(json.get[T](cursor).map(_.value)).option
|
||||
|
||||
|
||||
def errorBody(errorType: String, msg: String) = Body.fromString(s"""{"error": "$errorType","msg": "$msg"}""")
|
||||
|
||||
def parseRequestForm(body: Body) =
|
||||
(for
|
||||
decoded <- body.asString.map(URLDecoder.decode(_, StandardCharsets.UTF_8.name()))
|
||||
params = decoded.split('&').collect { case s"$k=$v" => (k, v)}.toMap
|
||||
yield params)
|
||||
.mapError(s => InvalidRequest("Invalid request body", s.getMessage))
|
||||
|
||||
def search(map: Map[String, String])(name: String) =
|
||||
ZIO.attempt(map.get(name)).someOrFail(InvalidRequest(s"Missing or invalid field $name."))
|
||||
|
||||
}
|
||||
|
@ -1,15 +1,25 @@
|
||||
package org.tbasket.test
|
||||
|
||||
import io.netty.handler.codec.http.QueryStringEncoder
|
||||
import zio.*
|
||||
import zio.http.{Body, Response}
|
||||
import zio.json.*
|
||||
import zio.json.ast.Json
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
object TestUtils {
|
||||
def getJsonBody(r: Response): Task[Json] = {
|
||||
for
|
||||
body <- r.body.asString
|
||||
json <- ZIO.fromEither(body.fromJson[Json]).mapError(new Exception(_)).orElseSucceed(Json.Null)
|
||||
json <- ZIO.fromEither(body.fromJson[Json]).orElseSucceed(Json.Obj())
|
||||
yield json
|
||||
}
|
||||
|
||||
implicit def makeFormBody(params: (String, String)*): Body = {
|
||||
val qse = new QueryStringEncoder("")
|
||||
params.foreach(qse.addParam(_, _))
|
||||
Body.fromString(qse.toString)
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in new issue