Skip to content

Commit

Permalink
Merge pull request #794 from bcgov/feat/untp-vc
Browse files Browse the repository at this point in the history
feat: endpoints for json-ld
  • Loading branch information
WadeBarnes authored Sep 12, 2024
2 parents 08059a2 + dd95e87 commit dbbb261
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
1 change: 1 addition & 0 deletions server/vcr-server/agent_webhooks/utils/credential_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _build_processor_config(
processor_config = {
"cardinality_fields": credential_type_def.get("cardinality_fields"),
"credential": credential_type_def.get("credential"),
"mapping": credential_type_def.get('mapping'),
"mappings": credential_type_def.get("mappings"),
"topic": credential_type_def.get("topic"),
}
Expand Down
1 change: 0 additions & 1 deletion server/vcr-server/api/v4/serializers/rest/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class Meta:
"claim_descriptions",
"logo_b64",
"processor_config",
"schema",
)


Expand Down
6 changes: 3 additions & 3 deletions server/vcr-server/api/v4/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# DEPRECATED: this should not be used in new code and will be removed imminently
autocomplete as search_autocomplete,
)
from api.v4.views.rest import credential_type, issuer, topic, schemas
from api.v4.views.rest import credential_type, issuer, topic, schema
from api.v4.views.misc.contact import send_contact
from api.v4.views.misc.feedback import send_feedback

Expand All @@ -38,8 +38,8 @@
router.register(r"credential-type",
credential_type.RestView, "Credential Type")
router.register(r"issuer", issuer.RestView, "Issuer")
router.register(r"schemas", schemas.RestView)
router.register(r"topic", topic.RestView)
router.register(r"schema", schema.RestView, "Schema")
router.register(r"topic", topic.RestView, "Topic")
router.register(r"search/credential",
search_credential.SearchView, "Credential Search")
router.register(r"search/topic", search_topic.SearchView, "Topic Search")
Expand Down
71 changes: 55 additions & 16 deletions server/vcr-server/api/v4/views/rest/topic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from django.conf import settings
from django.http import Http404
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -28,16 +27,15 @@ def list(self, request):

@swagger_auto_schema(responses={200: CredentialSetSerializer(many=True)})
@action(detail=True, url_path="credential-set", methods=["get"])
def list_credential_sets(self, request, pk=None):
def credential_set_list(self, request, *args, **kwargs):
item = self.get_object()

credential_sets = (
item.credential_sets
.prefetch_related(
item.credential_sets.prefetch_related(
"credentials__attributes",
"credentials__names",
"credentials__credential_type",
"credentials__credential_type__issuer"
"credentials__credential_type__issuer",
)
.order_by("first_effective_date")
.all()
Expand All @@ -46,15 +44,27 @@ def list_credential_sets(self, request, pk=None):
data = [
{
"id": credential_set.id,
"create_timestamp": credential_set.create_timestamp.isoformat()
if credential_set.create_timestamp is not None else None,
"update_timestamp": credential_set.update_timestamp.isoformat()
if credential_set.update_timestamp is not None else None,
"create_timestamp": (
credential_set.create_timestamp.isoformat()
if credential_set.create_timestamp is not None
else None
),
"update_timestamp": (
credential_set.update_timestamp.isoformat()
if credential_set.update_timestamp is not None
else None
),
"latest_credential_id": credential_set.latest_credential_id,
"first_effective_date": credential_set.first_effective_date.isoformat()
if credential_set.first_effective_date is not None else None,
"last_effective_date": credential_set.last_effective_date.isoformat()
if credential_set.last_effective_date is not None else None,
"first_effective_date": (
credential_set.first_effective_date.isoformat()
if credential_set.first_effective_date is not None
else None
),
"last_effective_date": (
credential_set.last_effective_date.isoformat()
if credential_set.last_effective_date is not None
else None
),
"credentials": [
CredentialSerializer(credential).data
for credential in credential_set.credentials.all()
Expand All @@ -67,21 +77,50 @@ def list_credential_sets(self, request, pk=None):
response["item_count"] = len(data)
return response

@swagger_auto_schema(operation_id="topic_type_read")
@action(
detail=False,
url_path="(?P<source_id>[^/.]+)/type/(?P<type>[^/]+)",
methods=["get"],
)
def read(self, request, *args, **kwargs):
item = self.get_object()
return Response(self.get_serializer(item).data)

@action(
detail=False,
methods=["get"],
url_path="(?P<source_id>[^/.]+)/type/(?P<type>[^/]+)/credential/(?P<credential_id>[^/.]+)",
)
def raw_credential_read(self, request, *args, **kwargs):
item = self.get_object()

credential_id = self.kwargs.get("credential_id")

credential = item.credentials.filter(
format="vc_di", credential_id=credential_id, revoked=False
).first()

if not (credential and credential.raw_data):
raise Http404()

return Response(credential.raw_data, content_type="application/ld+json")

def get_object(self):
if self.kwargs.get("pk"):
return super(RestView, self).get_object()

type = self.kwargs.get("type")
source_id = self.kwargs.get("source_id")
type = self.kwargs.get("type")
if not type or not source_id:
raise Http404()

# map type to a schema name, if an "old style" type is used
# Map type to a schema name, if an "old style" type is used
if settings.CRED_TYPE_SYNONYMS and type.lower() in settings.CRED_TYPE_SYNONYMS:
type = settings.CRED_TYPE_SYNONYMS[type.lower()]

queryset = self.filter_queryset(self.get_queryset())
obj = get_object_or_404(queryset, type=type, source_id=source_id)
obj = get_object_or_404(queryset, source_id=source_id, type=type)

# May raise a permission denied
self.check_object_permissions(self.request, obj)
Expand Down

0 comments on commit dbbb261

Please sign in to comment.