Skip to content

Commit

Permalink
wip: add http statuscode binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat Losoponkul committed Apr 17, 2023
1 parent e3e7a4c commit b2212c1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ object DIDEndpoints {
.in(extractFromRequest[RequestContext](RequestContext.apply))
.tag("DID")

private val resolutionError = {
val errorMesssageMatcher: String => PartialFunction[Any, Boolean] = (err: String) => {
case res: DIDResolutionResult if res.didResolutionMetadata.error.contains(err) => true
}
oneOf[DIDResolutionResult](
oneOfVariantValueMatcher(
StatusCode.BadRequest,
jsonBody[DIDResolutionResult]
)(errorMesssageMatcher("notFound"))
)
}

val getDID: PublicEndpoint[
(RequestContext, String),
Unit, // TODO: resolution error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.iohk.atala.castor.controller.http

import sttp.model.StatusCode
import sttp.tapir.*
import sttp.tapir.json.zio.jsonBody
import io.iohk.atala.castor.controller.http.DIDResolutionResult

object EndpointOutputs {

private def resolutionErrorMather(err: String): PartialFunction[Any, Boolean] = {
case res: DIDResolutionResult if res.didResolutionMetadata.error.contains(err) => true
}

// https://w3c-ccg.github.io/did-resolution/#bindings-https
private val resolutionErrorHttpStatusBinding = Seq[(String, StatusCode)](
"invalidDid" -> StatusCode.BadRequest,
"invalidDidUrl" -> StatusCode.BadRequest,
"notFound" -> StatusCode.NotFound,
"representationNotSupported" -> StatusCode.NotAcceptable,
"methodNotSupported" -> StatusCode.NotImplemented,
"internalError" -> StatusCode.InternalServerError
).map { case (msg, code) =>
oneOfVariantValueMatcher(code, jsonBody[DIDResolutionResult])(resolutionErrorMather(msg))
}

val resolutionError: EndpointOutput[DIDResolutionResult] = oneOf(
resolutionErrorHttpStatusBinding.head,
resolutionErrorHttpStatusBinding.tail: _*
)

}

0 comments on commit b2212c1

Please sign in to comment.