Skip to content

Commit

Permalink
fix codec response and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat Losoponkul committed Apr 17, 2023
1 parent dabc9dc commit 06c344c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.iohk.atala.api.http.codec

import sttp.model.MediaType

object CustomMediaTypes {

val `application/did+ld+json`: MediaType = MediaType(
mainType = "application",
subType = "did+ld+json"
)

val `application/ld+json;did-resolution`: MediaType = MediaType(
mainType = "application",
subType = "ld+json",
otherParameters = Map("profile" -> "https://w3id.org/did-resolution")
)

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.iohk.atala.api.http.codec

import io.iohk.atala.castor.controller.http.DIDResolutionResult
import io.iohk.atala.castor.controller.http.{DIDDocument, DIDResolutionResult}
import sttp.model.MediaType
import sttp.tapir.*
import sttp.tapir.DecodeResult.Error.{JsonDecodeException, JsonError}
Expand All @@ -9,36 +9,16 @@ import zio.json.{EncoderOps, JsonCodec, JsonDecoder, JsonEncoder}

object DIDCodec {

val `application/did+ld+json`: MediaType = MediaType(
mainType = "application",
subType = "did+ld+json"
)

val `application/ld+json;did-resolution`: MediaType = MediaType(
mainType = "application",
subType = "ld+json",
otherParameters = Map("profile" -> "https://w3id.org/did-resolution")
)

final case class DIDJsonLD() extends CodecFormat {
override val mediaType: MediaType = `application/did+ld+json`
override val mediaType: MediaType = CustomMediaTypes.`application/did+ld+json`
}

final case class DIDResolutionJsonLD() extends CodecFormat {
override val mediaType: MediaType = `application/ld+json;did-resolution`
override val mediaType: MediaType = CustomMediaTypes.`application/ld+json;did-resolution`
}

def didJsonLD: Codec[String, DIDResolutionResult, DIDJsonLD] = {
val zioJsonCodec = sttp.tapir.json.zio.zioCodec[DIDResolutionResult]
Codec
.json(_ =>
DecodeResult.Error(
"Decoding is not supported for DID resource codec",
Exception("Decoding is not supported for DID resource codec")
)
)(zioJsonCodec.encode)
.format(DIDJsonLD())
}
def didJsonLD: Codec[String, DIDDocument, DIDJsonLD] =
sttp.tapir.json.zio.zioCodec[DIDDocument].format(DIDJsonLD())

def didResolutionJsonLD: Codec[String, DIDResolutionResult, DIDResolutionJsonLD] =
sttp.tapir.json.zio.zioCodec[DIDResolutionResult].format(DIDResolutionJsonLD())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.iohk.atala.castor.controller

import io.iohk.atala.api.http.codec.CustomMediaTypes
import io.iohk.atala.api.http.codec.DIDCodec.{didJsonLD, didResolutionJsonLD}
import io.iohk.atala.api.http.{ErrorResponse, RequestContext}
import sttp.model.StatusCode
import sttp.model.{Header, StatusCode}
import sttp.tapir.*
import io.iohk.atala.castor.controller.http.DIDResolutionResult
import io.iohk.atala.castor.controller.http.{DIDResolutionResult, ResolutionResponse}
import sttp.tapir.json.zio.jsonBody

object DIDEndpoints {
Expand All @@ -13,7 +14,7 @@ object DIDEndpoints {
val getDID: PublicEndpoint[
String,
Nothing,
(StatusCode, DIDResolutionResult),
(StatusCode, ResolutionResponse),
Any
] = infallibleEndpoint.get
.in("dids" / path[String]("didRef"))
Expand All @@ -27,7 +28,7 @@ object DIDEndpoints {
.description(StatusCode.NotImplemented, "The DID method is not supported")
.description(StatusCode.InternalServerError, "Internal error")
.and(
oneOf[DIDResolutionResult](
oneOf[ResolutionResponse](
oneOfVariant(stringBodyUtf8AnyFormat(didJsonLD)),
oneOfVariant(stringBodyUtf8AnyFormat(didResolutionJsonLD)),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ import sttp.tapir.Schema
import zio.json.{DeriveJsonDecoder, DeriveJsonEncoder, JsonEncoder, JsonDecoder}
import io.iohk.atala.castor.core.model.did.w3c

sealed trait ResolutionResponse

final case class DIDResolutionResult(
`@context`: String,
didDocument: Option[DIDDocument] = None,
didDocumentMetadata: DIDDocumentMetadata,
didResolutionMetadata: DIDResolutionMetadata
) extends ResolutionResponse

object DIDResolutionResult {
given encoder: JsonEncoder[DIDResolutionResult] = DeriveJsonEncoder.gen[DIDResolutionResult]
given decoder: JsonDecoder[DIDResolutionResult] = DeriveJsonDecoder.gen[DIDResolutionResult]
given schema: Schema[DIDResolutionResult] = Schema.derived
}

final case class DIDResolutionMetadata(
error: Option[String] = None,
errorMessage: Option[String] = None,
contentType: Option[String] = None
)

object DIDResolutionMetadata {
given encoder: JsonEncoder[DIDResolutionMetadata] = DeriveJsonEncoder.gen[DIDResolutionMetadata]
given decoder: JsonDecoder[DIDResolutionMetadata] = DeriveJsonDecoder.gen[DIDResolutionMetadata]
given schema: Schema[DIDResolutionMetadata] = Schema.derived
}

final case class DIDDocument(
`@context`: Seq[String],
id: String,
Expand All @@ -15,7 +42,7 @@ final case class DIDDocument(
capabilityInvocation: Option[Seq[String]] = None,
capabilityDelegation: Option[Seq[String]] = None,
service: Option[Seq[Service]] = None
)
) extends ResolutionResponse

object DIDDocument {
given encoder: JsonEncoder[DIDDocument] = DeriveJsonEncoder.gen[DIDDocument]
Expand Down

This file was deleted.

0 comments on commit 06c344c

Please sign in to comment.