Skip to content

Commit

Permalink
Add endpoint /tokens/lists
Browse files Browse the repository at this point in the history
- Closes #2342
  • Loading branch information
Uxio0 committed Nov 27, 2024
1 parent 42b381c commit 5215ac1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
8 changes: 7 additions & 1 deletion safe_transaction_service/tokens/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework import serializers
from safe_eth.eth.django.serializers import EthereumAddressField

from .models import Token
from .models import Token, TokenList


class TokenTransferInfoType(Enum):
Expand Down Expand Up @@ -37,3 +37,9 @@ class TokenPriceResponseSerializer(serializers.Serializer):
fiat_code = serializers.CharField()
fiat_price = serializers.CharField()
timestamp = serializers.DateTimeField()


class TokenListSerializer(serializers.ModelSerializer):
class Meta:
model = TokenList
fields = ["url", "description"]
1 change: 1 addition & 0 deletions safe_transaction_service/tokens/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ class Meta:
model = models.TokenList

url = factory.Faker("url")
description = factory.Faker("company")
23 changes: 22 additions & 1 deletion safe_transaction_service/tokens/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest import mock
from unittest.mock import MagicMock

from django.core.cache import cache
from django.urls import reverse

from eth_account import Account
Expand All @@ -12,7 +13,7 @@
from safe_eth.safe.tests.safe_test_case import SafeTestCaseMixin

from ..models import Token
from .factories import TokenFactory
from .factories import TokenFactory, TokenListFactory

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -104,3 +105,23 @@ def test_tokens_view(self):
}
],
)

def test_token_lists_view(self):
response = self.client.get(reverse("v1:tokens:token-lists"))
self.assertEqual(response.data["results"], [])
token_list = TokenListFactory()
# Check cache
self.assertEqual(response.data["results"], [])

cache.clear()

response = self.client.get(reverse("v1:tokens:token-lists"))
self.assertEqual(
response.data["results"],
[
{
"url": token_list.url,
"description": token_list.description,
}
],
)
1 change: 1 addition & 0 deletions safe_transaction_service/tokens/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

urlpatterns = [
path("", views.TokensView.as_view(), name="list"),
path("lists/", views.TokenListsView.as_view(), name="token-lists"),
path("<str:address>/", views.TokenView.as_view(), name="detail"),
]
15 changes: 14 additions & 1 deletion safe_transaction_service/tokens/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from ..history.serializers import CodeErrorResponse
from . import filters, serializers
from .models import Token
from .models import Token, TokenList


@extend_schema(
Expand Down Expand Up @@ -64,3 +64,16 @@ def get(self, request, *args, **kwargs):
Returns the list of tokens supported in the Safe Transaction Service
"""
return super().get(request, *args, **kwargs)


class TokenListsView(ListAPIView):
serializer_class = serializers.TokenListSerializer
ordering = ("pk",)
queryset = TokenList.objects.all()

@method_decorator(cache_page(60 * 15)) # Cache 15 minutes
def get(self, request, *args, **kwargs):
"""
Returns the list of tokens supported in the Safe Transaction Service
"""
return super().get(request, *args, **kwargs)

0 comments on commit 5215ac1

Please sign in to comment.