Skip to content

Commit

Permalink
feat: interoperable schema api (#834)
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <bassam.riman@iohk.io>
  • Loading branch information
CryptoKnightIOG authored Jan 11, 2024
1 parent 3ac1cb8 commit 214b36a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.iohk.atala.pollux.credentialschema.http.{
}
import sttp.model.StatusCode
import sttp.tapir.json.zio.jsonBody
import sttp.tapir.json.zio.schemaForZioJsonValue
import sttp.tapir.{
Endpoint,
EndpointInput,
Expand All @@ -27,6 +28,7 @@ import sttp.tapir.{
statusCode,
stringToPath
}
import zio.json.ast.Json

import java.util.UUID

Expand Down Expand Up @@ -128,6 +130,26 @@ object SchemaRegistryEndpoints {
)
.tag("Schema Registry")

val getRawSchemaByIdEndpoint: PublicEndpoint[
(RequestContext, UUID),
ErrorResponse,
Json, // changed to generic Json type
Any
] =
endpoint.get
.in(extractFromRequest[RequestContext](RequestContext.apply))
.in(
"schema-registry" / "schemas" / path[UUID]("guid") / "schema".description(
"Globally unique identifier of the credential schema record"
)
)
.out(jsonBody[Json].description("Raw JSON response of the CredentialSchema")) // changed to Json
.errorOut(basicFailuresAndNotFound)
.name("getRawSchemaById")
.summary("Fetch the schema from the registry by `guid`")
.description("Fetch the credential schema by the unique identifier")
.tag("Schema Registry")

private val credentialSchemaFilterInput: EndpointInput[FilterInput] = EndpointInput.derived[FilterInput]
private val paginationInput: EndpointInput[PaginationInput] = EndpointInput.derived[PaginationInput]
val lookupSchemasByQueryEndpoint: Endpoint[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class SchemaRegistryServerEndpoints(
credentialSchemaController.getSchemaByGuid(guid)(ctx)
}

val getRawSchemaByIdServerEndpoint: ZServerEndpoint[Any, Any] =
getRawSchemaByIdEndpoint
.zServerLogic { case (ctx: RequestContext, guid: UUID) =>
credentialSchemaController.getSchemaJsonByGuid(guid)(ctx)
}

val lookupSchemasByQueryServerEndpoint: ZServerEndpoint[Any, Any] =
lookupSchemasByQueryEndpoint
.zServerSecurityLogic(SecurityLogic.authorizeWith(_)(authenticator, authorizer))
Expand Down Expand Up @@ -87,6 +93,7 @@ class SchemaRegistryServerEndpoints(
createSchemaServerEndpoint,
updateSchemaServerEndpoint,
getSchemaByIdServerEndpoint,
getRawSchemaByIdServerEndpoint,
lookupSchemasByQueryServerEndpoint,
testServerEndpoint
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import io.iohk.atala.pollux.credentialschema.http.{
FilterInput
}
import zio.*
import scala.language.implicitConversions

import scala.language.implicitConversions
import java.util.UUID
import io.iohk.atala.shared.models.WalletAccessContext
import zio.json.ast.Json

trait CredentialSchemaController {
def createSchema(in: CredentialSchemaInput)(implicit
Expand All @@ -29,6 +30,10 @@ trait CredentialSchemaController {
rc: RequestContext
): IO[ErrorResponse, CredentialSchemaResponse]

def getSchemaJsonByGuid(id: UUID)(implicit
rc: RequestContext
): IO[ErrorResponse, Json]

def delete(guid: UUID)(implicit
rc: RequestContext
): ZIO[WalletAccessContext, ErrorResponse, CredentialSchemaResponse]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import io.iohk.atala.pollux.credentialschema.http.{
CredentialSchemaResponsePage,
FilterInput
}
import io.iohk.atala.shared.models.WalletAccessContext
import zio.*
import zio.json.ast.Json

import java.util.UUID
import io.iohk.atala.shared.models.WalletAccessContext

class CredentialSchemaControllerImpl(service: CredentialSchemaService, managedDIDService: ManagedDIDService)
extends CredentialSchemaController {
Expand Down Expand Up @@ -65,6 +66,16 @@ class CredentialSchemaControllerImpl(service: CredentialSchemaService, managedDI
)
}

override def getSchemaJsonByGuid(guid: UUID)(implicit
rc: RequestContext
): IO[ErrorResponse, Json] = {
service
.getByGUID(guid)
.map(
_.schema
)
}

override def delete(guid: UUID)(implicit
rc: RequestContext
): ZIO[WalletAccessContext, ErrorResponse, CredentialSchemaResponse] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ object CredentialSchemaBasicSpec extends ZIOSpecDefault with CredentialSchemaTes
.response(asJsonAlways[CredentialSchemaResponse])
.send(backend)
} yield response

def getSchemaZIO(uuid: UUID) = for {
backend <- backendZIO
response <- basicRequest
Expand All @@ -87,6 +88,16 @@ object CredentialSchemaBasicSpec extends ZIOSpecDefault with CredentialSchemaTes
fetchedSchema <- fromEither(response.body)
} yield fetchedSchema

def getRawSchemaZIO(uuid: UUID) = for {
backend <- backendZIO
response <- basicRequest
.get(credentialSchemaUriBase.addPath(uuid.toString).addPath("schema"))
.response(asJson)
.send(backend)

fetchedSchema <- fromEither(response.body)
} yield fetchedSchema

suite("schema-registry create and get by ID operations logic")(
test("create the new schema") {
for {
Expand All @@ -108,9 +119,12 @@ object CredentialSchemaBasicSpec extends ZIOSpecDefault with CredentialSchemaTes

fetchedSchema <- getSchemaZIO(credentialSchema.guid)

fetchedRawSchema <- getRawSchemaZIO(credentialSchema.guid)

credentialSchemaIsFetched = assert(fetchedSchema)(equalTo(credentialSchema))
rawCredentialSchemaIsFetched = assert(fetchedRawSchema)(equalTo(credentialSchema.schema))

} yield statusCodeIs201 && credentialSchemaIsCreated && credentialSchemaIsFetched
} yield statusCodeIs201 && credentialSchemaIsCreated && credentialSchemaIsFetched && rawCredentialSchemaIsFetched
},
test("get the schema by the wrong id") {
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ trait CredentialSchemaTestTools extends PostgresTestContainerSupport {
.thenRunLogic()
.whenServerEndpoint(schemaRegistryEndpoints.getSchemaByIdServerEndpoint)
.thenRunLogic()
.whenServerEndpoint(schemaRegistryEndpoints.getRawSchemaByIdServerEndpoint)
.thenRunLogic()
.whenServerEndpoint(
schemaRegistryEndpoints.lookupSchemasByQueryServerEndpoint
)
Expand Down

0 comments on commit 214b36a

Please sign in to comment.