From e982cd3445c5410d7da209431ab2e235c70ddb02 Mon Sep 17 00:00:00 2001 From: phzhik Date: Thu, 2 Nov 2023 20:41:40 +0400 Subject: [PATCH] + Poizon API --- external_api/poizon.py | 51 +++++++++++++++++++++++++++++++++++++++++ poizonstore/settings.py | 2 ++ store/urls.py | 1 + store/views.py | 20 ++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 external_api/poizon.py diff --git a/external_api/poizon.py b/external_api/poizon.py new file mode 100644 index 0000000..6ee29a6 --- /dev/null +++ b/external_api/poizon.py @@ -0,0 +1,51 @@ +from urllib3.util import parse_url +from urllib.parse import urljoin, parse_qs + +import requests + + +class PoizonClient: + SPU_GET_DATA_ENDPOINT = 'spu_get_data' + + MAX_RETRIES = 2 + + def __init__(self, token: str): + self.api_url = 'http://124.222.99.75/' + self.token = token + self.session = requests.Session() + + def request(self, method, url, *args, **kwargs): + params = kwargs.pop('params', {}) + params.update({"token": self.token}) + + joined_url = urljoin(self.api_url, url) + request = requests.Request(method, joined_url, params=params, *args, **kwargs) + + retries = 0 + while retries < self.MAX_RETRIES: + prepared = self.session.prepare_request(request) + r = self.session.send(prepared) + + # TODO: handle/log errors + return r + + @staticmethod + def get_spu_id(url): + try: + # Go to short dw4.co url to get the full one from redirect + if 'dw4.co' in url: + r = requests.get(url) + url = r.url + + url = parse_url(url) + qs = parse_qs(url.query) + spu_id = qs.get('spuId') + return spu_id.pop(0) if spu_id else None + except Exception as exc: + # TODO: handle/log errors + return None + + def get_good_info(self, spu_id): + params = {'spuId': str(spu_id)} + return self.request('GET', self.SPU_GET_DATA_ENDPOINT, params=params) + diff --git a/poizonstore/settings.py b/poizonstore/settings.py index c025a3d..f0dc0af 100644 --- a/poizonstore/settings.py +++ b/poizonstore/settings.py @@ -25,6 +25,8 @@ SECRET_KEY = 'django-insecure-e&9j(^9z7p7qs-@d)vftjz4%xqu0#3mmn@+$wzwh!%-dwjecm- CDEK_CLIENT_ID = 'wZWtjnWtkX7Fin2tvDdUE6eqYz1t1GND' CDEK_CLIENT_SECRET = 'lc2gmrmK5s1Kk6FhZbNqpQCaATQRlsOy' +POIZON_TOKEN = 'IRwNgBxb8YQ' + # SECURITY WARNING: don't run with debug turned on in production! DEBUG = bool(int(os.environ.get("DJANGO_DEBUG") or 0)) DISABLE_PERMISSIONS = False diff --git a/store/urls.py b/store/urls.py index c96ac00..55e0801 100644 --- a/store/urls.py +++ b/store/urls.py @@ -9,6 +9,7 @@ router = DefaultRouter() router.register(r'statistics', views.StatisticsAPI, basename='statistics') router.register(r'cdek', views.CDEKAPI, basename='cdek') router.register(r'gifts', views.GiftAPI, basename='gifts') +router.register(r'poizon', views.PoizonAPI, basename='poizon') urlpatterns = [ path("checklist/", views.ChecklistAPI.as_view()), diff --git a/store/views.py b/store/views.py index 1357676..b195eca 100644 --- a/store/views.py +++ b/store/views.py @@ -12,6 +12,7 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from external_api.cdek import CDEKClient +from external_api.poizon import PoizonClient from store.exceptions import CRMException from store.filters import GiftFilter from store.models import Checklist, GlobalSettings, Category, PaymentMethod, Promocode, Gift @@ -363,3 +364,22 @@ class CDEKAPI(viewsets.GenericViewSet): raise CRMException('json data is required') r = self.client.calculate_tariff(data) return Response(r.json()) + + +class PoizonAPI(viewsets.GenericViewSet): + client = PoizonClient(settings.POIZON_TOKEN) + permission_classes = [permissions.AllowAny] + + @action(url_path='good', detail=False, methods=['post']) + def get_good_info(self, request, *args, **kwargs): + spu_id = None + if 'url' in request.data: + spu_id = self.client.get_spu_id(request.data.get('url')) + if 'spuId' in request.data: + spu_id = request.data.get('spuId') + + if spu_id is None: + raise CRMException('url or spuId is required') + + r = self.client.get_good_info(spu_id) + return Response(r.json())