Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
#34 start api
Browse files Browse the repository at this point in the history
  • Loading branch information
llybin committed Jul 3, 2019
1 parent b1530a3 commit ca53835
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ vcrpy = "==2.0.1"
celery = {version = "==4.3.0",extras = ["redis"]}
django = "==2.2.2"
django-cache-memoize = "==0.1.6"
django-cors-headers = "==3.0.2"
django-redis = "==4.10.0"
djangorestframework = "==3.9.4"
jsonschema = "==3.0.1"
mysqlclient = "==1.4.2.post1"
python-dotenv = "==0.10.3"
Expand Down
40 changes: 28 additions & 12 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added api/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls import include
from django.conf.urls import url


urlpatterns = [
url(r'^v1/', include(('api.v1.urls', 'api'), namespace='v1')),
url(r'', include(('api.v1.urls', 'api'), namespace='last')),
]
Empty file added api/v1/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions api/v1/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from datetime import datetime

from rest_framework import serializers

from java_wallet.constants import BLOCK_CHAIN_START_AT, TxSubtypePayment


class PendingTxsSerializer(serializers.Serializer):
type = serializers.IntegerField(read_only=True)
subtype = serializers.IntegerField(read_only=True)
timestamp = serializers.SerializerMethodField(read_only=True)
amountNQT = serializers.IntegerField(read_only=True)
feeNQT = serializers.IntegerField(read_only=True)
sender = serializers.IntegerField(read_only=True)
recipient = serializers.IntegerField(read_only=True)
recipients = serializers.SerializerMethodField(read_only=True)

@staticmethod
def get_timestamp(data):
return datetime.fromtimestamp(data['timestamp'] + BLOCK_CHAIN_START_AT)

def get_recipients(self, data):
result = []

if data['subtype'] == TxSubtypePayment.MULTI_OUT:
for x in data['attachment']['recipients']:
result.append({'address': int(x[0]), 'amount': int(x[1])})

elif data['subtype'] == TxSubtypePayment.MULTI_OUT_SAME:
amount = int(data['amountNQT']) / len(data['attachment']['recipients'])
for x in data['attachment']['recipients']:
result.append({'address': int(x), 'amount': amount})

return result or None

def update(self, instance, validated_data):
pass

def create(self, validated_data):
pass
9 changes: 9 additions & 0 deletions api/v1/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import routers

from api.v1 import views

router = routers.DefaultRouter()

router.register('pending_txs', views.PendingTxsList, basename='pending_txs')

urlpatterns = router.urls
24 changes: 24 additions & 0 deletions api/v1/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.conf import settings
from rest_framework import viewsets
from rest_framework.response import Response
from sentry_sdk import capture_exception

from burst.api.brs.api import BrsApi
from burst.api.exceptions import APIException
from api.v1.serializers import PendingTxsSerializer


class PendingTxsList(viewsets.ViewSet):
@staticmethod
def list(request):
try:
txs = BrsApi(settings.BRS_NODE).get_unconfirmed_transactions()
txs.sort(key=lambda _x: int(_x['feeNQT']), reverse=True)

serializer = PendingTxsSerializer(txs, many=True)
result = serializer.data
except (APIException, ValueError) as e:
capture_exception(e)
result = []

return Response(result)
11 changes: 10 additions & 1 deletion explorer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
INTERNAL_IPS = os.getenv('DEBUG_TOOLBAR_INTERNAL_IPS')

ALLOWED_HOSTS = ['*']

CORS_ORIGIN_ALLOW_ALL = True

# Application definition

Expand All @@ -48,6 +48,9 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'corsheaders',
'rest_framework',
'api',
'scan',
'java_wallet',
]
Expand All @@ -58,6 +61,7 @@
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
Expand Down Expand Up @@ -163,6 +167,11 @@
}
}

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 25
}

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
Expand Down
1 change: 1 addition & 0 deletions explorer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
path('at/<str:id>', AtDetailView.as_view(), name='at-detail'),
path('search/', search_view, name='search'),
path('admin/', admin.site.urls),
path('api/', include(('api.urls', 'api'), namespace='api')),
]

if settings.DEBUG:
Expand Down

0 comments on commit ca53835

Please sign in to comment.