+ Poizon API
This commit is contained in:
parent
dd9dfb3704
commit
e982cd3445
51
external_api/poizon.py
Normal file
51
external_api/poizon.py
Normal file
|
|
@ -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)
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()),
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user