Skip to content

Commit

Permalink
feat(api): implement on-the-fly conversion of the stamps via IAM service
Browse files Browse the repository at this point in the history
  • Loading branch information
nutrina committed Sep 11, 2023
1 parent cf2dd79 commit 4cb2390
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions api/.env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CELERY_BROKER_URL=redis://localhost:6379/0
CERAMIC_CACHE_API_KEY=supersecret

CERAMIC_CACHE_CACAO_VALIDATION_URL=http://127.0.0.1:8001/verify
CERAMIC_CACHE_CONVERT_STAMP_TO_V2_URL=http://localhost:8003/api/v0.0.0/convert

GOOGLE_OAUTH_CLIENT_ID=supersecret
GOOGLE_CLIENT_SECRET=supersecret
Expand Down
40 changes: 27 additions & 13 deletions api/ceramic_cache/api/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List

import api_logging as logging
import requests
from django.conf import settings
from ninja import Router
from registry.api.v1 import DetailedScoreResponse
Expand Down Expand Up @@ -34,16 +35,30 @@


def migrate_stamp_to_v2(v1_stamp: CeramicCache) -> CeramicCache:
# TODO: dummy implementation, needs to be updated
v2_stamp = CeramicCache(
type=CeramicCache.StampType.V2,
address=v1_stamp.address,
provider=v1_stamp.provider,
created_at=v1_stamp.created_at,
updated_at=v1_stamp.updated_at,
v2_stamp_response = requests.post(
settings.CERAMIC_CACHE_CONVERT_STAMP_TO_V2_URL, json=v1_stamp.stamp
)
v2_stamp.save()
return v2_stamp

if v2_stamp_response.status_code == 200:
v2_stamp = CeramicCache(
type=CeramicCache.StampType.V2,
address=v1_stamp.address,
provider=v1_stamp.provider,
created_at=v1_stamp.created_at,
updated_at=v1_stamp.updated_at,
stamp=v2_stamp_response.json(),
)
v2_stamp.save()
return v2_stamp
else:
log.error(
"Error converting stamp to V2: %s: %s",
v2_stamp_response.status_code,
v2_stamp_response.text,
exc_info=True,
)

return None


def get_passport_state(address: str) -> list[CeramicCache]:
Expand All @@ -64,7 +79,8 @@ def get_passport_state(address: str) -> list[CeramicCache]:
if v1_stamp.provider not in v2_stamps:
# the v1 stamp is missing in v2_stamps, so we need to create that entry
v2_stamp = migrate_stamp_to_v2(v1_stamp)
v2_stamps[v2_stamp.provider] = v2_stamp
if v2_stamp:
v2_stamps[v2_stamp.provider] = v2_stamp

# There is also the edge case wher where the V1 stamp is not identical to the V2 stamp
# (for example V1 stamp expires after the v2 stamp)
Expand Down Expand Up @@ -218,9 +234,7 @@ def delete_stamps(request, payload: List[DeleteStampPayload]):
@router.get("stamp", response=GetStampResponse)
def get_stamps(request, address):
try:
stamps = CeramicCache.objects.filter(
type=CeramicCache.StampType.V2, address=address
)
stamps = get_passport_state(address)

scorer_id = settings.CERAMIC_CACHE_SCORER_ID
if (
Expand Down
4 changes: 4 additions & 0 deletions api/scorer/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@
}

CERAMIC_CACHE_SCORER_ID = env("CERAMIC_CACHE_SCORER_ID")
CERAMIC_CACHE_CONVERT_STAMP_TO_V2_URL = env(
"CERAMIC_CACHE_CONVERT_STAMP_TO_V2_URL",
default="http://localhost:8003/api/v0.0.0/convert",
)

PASSPORT_PUBLIC_URL = env("PASSPORT_PUBLIC_URL", default="http://localhost:80")

Expand Down

0 comments on commit 4cb2390

Please sign in to comment.