#!/usr/bin/python # -*- coding: utf-8 -*- # __author__ = 'szhdanoff@gmail.com' import os import json import httpx # local imports import app import requests app_token = app.APP_TOKEN pos_token = app.POS_TOKEN merchant_id = app.MERCHANT_ID url = app.API_URI __version__ = '1.0.0' HEADERS = { 'Authorization': f'dmtoken {pos_token}', 'DM-Authorization': f'dmapptoken {app_token}', 'User-Agent': f'bonus-import-tools-2024 v.{__version__}', 'Accept': 'application/json', # 'Accept-Language': 'ru,ru-RU;q=0.8,en-gb;q=0.5,en;q=0.3', 'Accept-Charset': 'utf-8', 'Connection': 'close', 'Content-Type': 'application/json', # 'Content-Type': 'application/x-www-form-urlencoded', } # GET /20130701/tokens/?next=/20130701/logon # https://pos-api.dinect.com/20130701/tokens/3b01228843d115ae8c03a4d3b20dcb545dbb228c def get_user(search_id, get_type='auto', headers=None): """ Retrieves user information based on the provided search ID. Args: search_id (str): The ID used to search for the user. get_type (str, optional): The type of search to perform. Defaults to 'auto'. 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. Raises: None Example: # >>> 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 r = requests.get( base_url, headers=headers, params={ get_type: search_id } ) if r.status_code == 200: return True, r.json() else: return False, r.json() 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/' params = {} r = requests.post(base_url, headers=headers, json=params) if r.status_code == 201: return True, r.json() else: return False, r.json() 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 base_url = url + 'users/' + str(user_id) + '/purchases/' params = { "doc_id": doc_id, "bonus_amount": bonus_amount, "sum_total": summ_total, "sum_discount": sum_discount, "sum_with_discount ": sum_with_discount, "commit": 'True', "curr_iso_name": 'RUB', "override": 'True', # "date": '2024-08-03 12:53:07', } r = requests.post(base_url, headers=headers, json=params) if r.status_code == 201: return True, r.json() else: return False, r.json() result, data = get_user('1234567890123') if result: user_id = data[0].get('id') # добавление внешней карты лояльности # print(get_user('1002')) # print(new_user()) # (True, '{"DIN":3152300,"ID":"4620011139016570939672611"}') # 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, # ))