Add basic rest calls
This commit is contained in:
parent
680b78e904
commit
2e8be04328
|
|
@ -0,0 +1,96 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# ITerativ GmbH
|
||||||
|
# http://www.iterativ.ch/
|
||||||
|
#
|
||||||
|
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
|
||||||
|
#
|
||||||
|
# Created on 23.01.20
|
||||||
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
||||||
|
from django.conf import settings
|
||||||
|
import logging
|
||||||
|
import requests
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class MagentoException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MagentoClient:
|
||||||
|
URL = 'https://stage.hep-verlag.ch'
|
||||||
|
# URL = 'https://www.hep-verlag.ch'
|
||||||
|
WEBSITE_ID = 1
|
||||||
|
HEADERS = {
|
||||||
|
'accept': 'application/json',
|
||||||
|
'content-type': 'application/json'
|
||||||
|
}
|
||||||
|
|
||||||
|
def _call(self, url, method='get', data=None, additional_headers=None):
|
||||||
|
|
||||||
|
request_url = '{}{}'.format(self.URL, url)
|
||||||
|
|
||||||
|
if additional_headers:
|
||||||
|
headers = {**additional_headers, **self.HEADERS}
|
||||||
|
else:
|
||||||
|
headers = self.HEADERS
|
||||||
|
|
||||||
|
if method == 'post':
|
||||||
|
response = requests.post(request_url, json=data, headers=headers)
|
||||||
|
elif method == 'get':
|
||||||
|
if data:
|
||||||
|
response = requests.get(request_url, headers=headers, data=data)
|
||||||
|
else:
|
||||||
|
response = requests.get(request_url, headers=headers)
|
||||||
|
|
||||||
|
if response.status_code != 200:
|
||||||
|
raise MagentoException(response.status_code, response.json())
|
||||||
|
|
||||||
|
logger.info(response.json())
|
||||||
|
return response
|
||||||
|
|
||||||
|
def is_email_available(self, email):
|
||||||
|
response = self._call('/rest/deutsch/V1/customers/isEmailAvailable', method='post',
|
||||||
|
data={'customerEmail': email, 'websiteId': self.WEBSITE_ID})
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def customer_verify_email(self, confirmation_key):
|
||||||
|
response = self._call('/rest/V1/customers/me', method='put', data={'confirmationKey': confirmation_key})
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def customer_create(self, customer_data, address):
|
||||||
|
|
||||||
|
if customer_data['prefix'] == 'Herr':
|
||||||
|
customer_data['gender'] = 1
|
||||||
|
else:
|
||||||
|
customer_data['gender'] = 2
|
||||||
|
|
||||||
|
address['country_id'] = 'CH'
|
||||||
|
address['default_billing'] = True
|
||||||
|
address['default_shipping'] = True
|
||||||
|
|
||||||
|
customer_data['addresses'] = [address]
|
||||||
|
|
||||||
|
response = self._call('/rest/deutsch/V1/customers', method='post', data=customer_data)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def customer_token(self, username, password):
|
||||||
|
response = self._call('/rest/deutsch/V1/integration/customer/token', 'post',
|
||||||
|
data={'username': username, 'password': password})
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def customer_me(self, token):
|
||||||
|
response = self._call('/rest/V1/customers/me', additional_headers={'authorization': 'Bearer {}'.format(token)})
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def customer_orders(self, admin_token, customer_id):
|
||||||
|
url = ("/rest/V1/orders/?searchCriteria[filterGroups][0][filters][0]["
|
||||||
|
"field]=customer_id&searchCriteria[filterGroups][0][filters][0][value]={}".format(customer_id))
|
||||||
|
|
||||||
|
response = self._call(url, additional_headers={'authorization': 'Bearer {}'.format(admin_token)})
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
def coupon_redeem(self, coupon, customer_id):
|
||||||
|
response = self._call('/rest/deutsch/V1/coupon/{}/customer/{}'.format(coupon, customer_id), method='put')
|
||||||
|
return response
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# ITerativ GmbH
|
||||||
|
# http://www.iterativ.ch/
|
||||||
|
#
|
||||||
|
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
|
||||||
|
#
|
||||||
|
# Created on 23.01.20
|
||||||
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
||||||
|
import json
|
||||||
|
|
||||||
|
from django.test import TestCase, Client
|
||||||
|
from core.factories import UserFactory
|
||||||
|
|
||||||
|
|
||||||
|
class HepClientTestCases(TestCase):
|
||||||
|
pass
|
||||||
Loading…
Reference in New Issue