diff --git a/dinect_api.py b/dinect_api.py index 5567bb9..cde8b8a 100644 --- a/dinect_api.py +++ b/dinect_api.py @@ -42,21 +42,23 @@ def get_user(search_id, get_type='auto', headers=None): headers (dict, optional): The headers to include in the request. Defaults to None. Returns: - tuple: A tuple containing a boolean indicating if the user was found and the response text. + tuple: A tuple containing a boolean indicating the success of the request and the JSON response. + If the request is successful, the boolean is True and the JSON response is returned. + If the request is unsuccessful, the boolean is False and the JSON response is returned. Raises: None Example: - # >>> get_user('1234567890123', get_type='auto') - (True, '[{"id":1002,"card":"4620011139016364102713436","discount":0,"amount":"0.00","purchases":0,"bonus":10,"first_name":"Сергей Жданов","last_name":"","middle_name":"","photo_urls":{"100x125":"https://static.dinect.com/load/usr/ava/000/000/001/002/43ebeec7c6ee/avatar-100x100.png","50x62":"https://static.dinect.com/load/usr/ava/000/000/001/002/43ebeec7c6ee/avatar-50x50.png"},"coupons_url":"https://pos-api.dinect.com/20130701/users/1002/coupons/","purchases_url":"https://pos-api.dinect.com/20130701/users/1002/purchases/byforeigncard/1234567890123/","loyalty_url":"https://pos-api.dinect.com/20130701/loyalties/byforeigncard/1234567890123","url":"https://pos-api.dinect.com/20130701/users/1002"}]') + # >>> get_user('1234567890', 'card') + #(True, {'id': 1002, 'card': '4620011139016364102713436', ...}) """ if headers is None: headers = HEADERS base_url = url + '/users/' # get_type = auto, card, phone, email, foreigncard - response = httpx.get( + r = requests.get( base_url, headers=headers, params={ @@ -64,89 +66,102 @@ def get_user(search_id, get_type='auto', headers=None): } ) - if response.status_code == 200: - # print('User found', user_id, 'response.text', response.text) - return True, response.text + if r.status_code == 200: + return True, r.json() else: - # print('get_headers', headers) - # print('response_headers', response.headers) - # print('User not found', search_id, 'response.text', response.text) - return False, response.text + return False, r.json() -def new_user(foreign_card, headers=None): +def new_user(headers=None): + """ + A function that creates a new user with optional headers. + + Args: + headers (dict, optional): The headers to include in the request. Defaults to None. + + Returns: + tuple: A tuple containing a boolean indicating the success of the request and the JSON response. + If the request is successful, the boolean is True and the JSON response is returned. + If the request is unsuccessful, the boolean is False and the JSON response is returned. + """ if headers is None: headers = HEADERS base_url = url + '/users/' - response = httpx.post( - base_url, - headers=headers, - json={ - 'foreigncard': foreign_card - } - ) - if response.status_code == 200: - return True, response.text + params = {} + + r = requests.post(base_url, headers=headers, json=params) + if r.status_code == 201: + return True, r.json() else: - return False, response.text + return False, r.json() -def bonuses_update(user_id, summ_total, bonus_amount, doc_id, headers=None): +def bonuses_update( + user_id, + summ_total, + bonus_amount, + sum_with_discount, + sum_discount, + doc_id, + headers=None): + """ + Updates the bonuses for a user. + + Args: + user_id (int): The ID of the user. + summ_total (float): The total amount. + bonus_amount (float): The bonus amount. + sum_with_discount (float): The amount with discount. + sum_discount (float): The discount amount. + doc_id (str): The document ID. + headers (dict, optional): The headers to include in the request. Defaults to None. + + Returns: + tuple: A tuple containing a boolean indicating the success of the request and the JSON response. + If the request is successful, the boolean is True and the JSON response is returned. + If the request is unsuccessful, the boolean is False and the JSON response is returned. + """ if headers is None: headers = HEADERS - print(headers) - base_url = url + 'users/' + str(user_id) + '/purchases/' params = { "doc_id": doc_id, "bonus_amount": bonus_amount, "sum_total": summ_total, - # "sum_total": '0.00', - "sum_discount": '10.00', - "sum_with_discount ": '90.00', + "sum_discount": sum_discount, + "sum_with_discount ": sum_with_discount, "commit": 'True', - # "curr_iso_code": '643', - # "redeem_auto": 'True', "curr_iso_name": 'RUB', - # "override": 'True', - # 'password': 'True', - "date": '2024-08-03 12:53:07', + "override": 'True', + # "date": '2024-08-03 12:53:07', } - # data = { - # 'bonus_amount': '10.00', - # 'commit': 'True', - # 'curr_iso_code': '643', - # 'override': 'True', - # # 'password': 'True', - # 'sum_total': '0', - # } r = requests.post(base_url, headers=headers, json=params) - # {"doc_id": "test1", "bonus_amount": "10", "commit": "False", "curr_iso_code": "643", "override": "True", "sum_total": "100"} - - # response = httpx.post( - # base_url, - # headers=headers, - # params=params - # ) - - # if response.status_code == 200: - # return True, response.text - # else: - # print('params', params) - # print('base_url', base_url) - # return False, response.text - print('params', params) if r.status_code == 201: - return True, r.text + return True, r.json() else: - return False, r.text + return False, r.json() + + +result, data = get_user('1234567890123') +if result: + user_id = data[0].get('id') + # добавление внешней карты лояльности -# print(get_user('1234567890123')) # print(get_user('1002')) -# print(new_user('1234567890123')) +# print(new_user()) +# (True, '{"DIN":3152300,"ID":"4620011139016570939672611"}') -print(bonuses_update('1002', '100.00', '10.00', doc_id='test11')) + + +# print(bonuses_update( +# user_id=int('1002'), +# summ_total=100.00, +# bonus_amount=10.00, +# doc_id='test12', +# sum_with_discount=90.00, +# sum_discount=10.00, +# ))