|
@@ -16,8 +16,8 @@
|
|
"""
|
|
"""
|
|
import datetime
|
|
import datetime
|
|
import json
|
|
import json
|
|
-
|
|
|
|
import requests
|
|
import requests
|
|
|
|
+from Ansjer.config import LOGGER
|
|
|
|
|
|
STORE_ID = "LIKwYlx6uonKqN91fXiGFb4FHqVLpvMUdGNU5Zf9"
|
|
STORE_ID = "LIKwYlx6uonKqN91fXiGFb4FHqVLpvMUdGNU5Zf9"
|
|
YOTPO_URL = f"https://api.yotpo.com/core/v3/stores/{STORE_ID}/"
|
|
YOTPO_URL = f"https://api.yotpo.com/core/v3/stores/{STORE_ID}/"
|
|
@@ -27,46 +27,58 @@ secret = 'isb8EOQs8bkjrksLHN73o3HIkrhoW539IXhULfdh'
|
|
class YotpoCoreObject:
|
|
class YotpoCoreObject:
|
|
|
|
|
|
def get_token(self):
|
|
def get_token(self):
|
|
- url = YOTPO_URL + "access_tokens"
|
|
|
|
|
|
+ try:
|
|
|
|
+ url = YOTPO_URL + "access_tokens"
|
|
|
|
|
|
- payload = {'secret': secret}
|
|
|
|
- headers = {
|
|
|
|
- "accept": "application/json",
|
|
|
|
- "Content-Type": "application/json"
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- response = requests.post(url, json=payload, headers=headers)
|
|
|
|
- assert response.status_code == 200
|
|
|
|
|
|
+ payload = {'secret': secret}
|
|
|
|
+ headers = {
|
|
|
|
+ "accept": "application/json",
|
|
|
|
+ "Content-Type": "application/json"
|
|
|
|
+ }
|
|
|
|
|
|
- token = json.loads(response.text).get("access_token")
|
|
|
|
- print("token:", token)
|
|
|
|
- return token
|
|
|
|
|
|
+ response = requests.post(url, json=payload, headers=headers)
|
|
|
|
+ if response.status_code != 200:
|
|
|
|
+ LOGGER.info(f'yotpo获取token失败, response.status_code请求不为200')
|
|
|
|
+ return ""
|
|
|
|
+ token = json.loads(response.text).get("access_token")
|
|
|
|
+ return token
|
|
|
|
+ except Exception as e:
|
|
|
|
+ LOGGER.info(f'yotpo获取token失败, {e}')
|
|
|
|
+ return ""
|
|
|
|
|
|
def creat_and_update_customers(self, customers):
|
|
def creat_and_update_customers(self, customers):
|
|
"""
|
|
"""
|
|
创建和更新用户
|
|
创建和更新用户
|
|
"""
|
|
"""
|
|
- url = YOTPO_URL + "customers"
|
|
|
|
- yotpo_token = self.get_token()
|
|
|
|
- headers = {
|
|
|
|
- "accept": "application/json",
|
|
|
|
- "X-Yotpo-Token": yotpo_token,
|
|
|
|
- "Content-Type": "application/json"
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- payload = {
|
|
|
|
- "customer": customers
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- response = requests.patch(url, json=payload, headers=headers)
|
|
|
|
- assert response.status_code == 200
|
|
|
|
- return response.text
|
|
|
|
|
|
+ try:
|
|
|
|
+ url = YOTPO_URL + "customers"
|
|
|
|
+ yotpo_token = self.get_token()
|
|
|
|
+ if yotpo_token == "":
|
|
|
|
+ LOGGER.info(f'yotpo创建顾客失败, yotpo_token为空 ,customers:{customers}')
|
|
|
|
+ return False
|
|
|
|
+ headers = {
|
|
|
|
+ "accept": "application/json",
|
|
|
|
+ "X-Yotpo-Token": yotpo_token,
|
|
|
|
+ "Content-Type": "application/json"
|
|
|
|
+ }
|
|
|
|
+ payload = {
|
|
|
|
+ "customer": customers
|
|
|
|
+ }
|
|
|
|
+ response = requests.patch(url, json=payload, headers=headers)
|
|
|
|
+ if response.status_code != 200:
|
|
|
|
+ LOGGER.info(f'yotpo创建顾客失败,response.status_code != 200,customers:{customers}')
|
|
|
|
+ return False
|
|
|
|
+ LOGGER.info(f'yotpo创建顾客成功,customers:{customers}')
|
|
|
|
+ return True
|
|
|
|
+ except Exception as e:
|
|
|
|
+ LOGGER.info(f'yotpo创建顾客失败, customers:{customers},{e}')
|
|
|
|
+ return False
|
|
|
|
|
|
def get_customers_list(self, email):
|
|
def get_customers_list(self, email):
|
|
"""
|
|
"""
|
|
查用户信息
|
|
查用户信息
|
|
"""
|
|
"""
|
|
- url = YOTPO_URL + f"customers?email={email}"
|
|
|
|
|
|
+ url = YOTPO_URL + f"customers?include_custom_properties=true&email={email}"
|
|
yotpo_token = self.get_token()
|
|
yotpo_token = self.get_token()
|
|
headers = {
|
|
headers = {
|
|
"accept": "application/json",
|
|
"accept": "application/json",
|
|
@@ -82,6 +94,8 @@ class YotpoCoreObject:
|
|
创建订阅
|
|
创建订阅
|
|
"""
|
|
"""
|
|
url = f"https://api.yotpo.com/messaging/v3/stores/{STORE_ID}/subscribers"
|
|
url = f"https://api.yotpo.com/messaging/v3/stores/{STORE_ID}/subscribers"
|
|
|
|
+ if "custom_properties" in customer_params:
|
|
|
|
+ del customer_params["custom_properties"]
|
|
payload = {
|
|
payload = {
|
|
"customer": customer_params,
|
|
"customer": customer_params,
|
|
"channels": {
|
|
"channels": {
|
|
@@ -89,7 +103,6 @@ class YotpoCoreObject:
|
|
"marketing": {
|
|
"marketing": {
|
|
"consent": "subscribed",
|
|
"consent": "subscribed",
|
|
"list_id": list_id,
|
|
"list_id": list_id,
|
|
- "source": "post_purchase_popup"
|
|
|
|
},
|
|
},
|
|
"suppression": {
|
|
"suppression": {
|
|
"suppress_email": True,
|
|
"suppress_email": True,
|
|
@@ -98,7 +111,6 @@ class YotpoCoreObject:
|
|
}
|
|
}
|
|
},
|
|
},
|
|
},
|
|
},
|
|
- "suppress_welcome_messages": True
|
|
|
|
}
|
|
}
|
|
headers = {
|
|
headers = {
|
|
"accept": "application/json",
|
|
"accept": "application/json",
|
|
@@ -106,41 +118,37 @@ class YotpoCoreObject:
|
|
"X-Yotpo-Token": self.get_token()
|
|
"X-Yotpo-Token": self.get_token()
|
|
}
|
|
}
|
|
response = requests.post(url, json=payload, headers=headers)
|
|
response = requests.post(url, json=payload, headers=headers)
|
|
- assert response.status_code == 200
|
|
|
|
- return response.text
|
|
|
|
|
|
+ if response.status_code != 200:
|
|
|
|
+ LOGGER.info(f'yotpo创建订阅失败, customer_params:{customer_params}')
|
|
|
|
+ return False, ""
|
|
|
|
+ return True, response.text
|
|
|
|
|
|
- def close_subscribers(self, email, list_id):
|
|
|
|
|
|
+ def close_subscribers(self, customer, list_id):
|
|
"""
|
|
"""
|
|
关闭订阅
|
|
关闭订阅
|
|
"""
|
|
"""
|
|
url = f"https://api.yotpo.com/messaging/v3/stores/{STORE_ID}/subscribers"
|
|
url = f"https://api.yotpo.com/messaging/v3/stores/{STORE_ID}/subscribers"
|
|
-
|
|
|
|
payload = {
|
|
payload = {
|
|
- "customer": {
|
|
|
|
- "email": email,
|
|
|
|
- },
|
|
|
|
- # "channels": {
|
|
|
|
- # "email": {"marketing": {"consent": None}},
|
|
|
|
- # "suppress_welcome_messages": True
|
|
|
|
- # },
|
|
|
|
|
|
+ "customer": customer,
|
|
"channels": {
|
|
"channels": {
|
|
"email": {"marketing": {
|
|
"email": {"marketing": {
|
|
"consent": "unsubscribed",
|
|
"consent": "unsubscribed",
|
|
"list_id": list_id,
|
|
"list_id": list_id,
|
|
"source": "post_purchase_popup"
|
|
"source": "post_purchase_popup"
|
|
}}
|
|
}}
|
|
- },
|
|
|
|
- "list_id": list_id
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
headers = {
|
|
headers = {
|
|
"accept": "application/json",
|
|
"accept": "application/json",
|
|
"content-type": "application/json",
|
|
"content-type": "application/json",
|
|
"X-Yotpo-Token": self.get_token()
|
|
"X-Yotpo-Token": self.get_token()
|
|
}
|
|
}
|
|
-
|
|
|
|
response = requests.post(url, json=payload, headers=headers)
|
|
response = requests.post(url, json=payload, headers=headers)
|
|
-
|
|
|
|
- print(response.text)
|
|
|
|
|
|
+ if response.status_code != 200:
|
|
|
|
+ LOGGER.info(f'yotpo关闭订阅失败, customer:{customer}')
|
|
|
|
+ return False
|
|
|
|
+ LOGGER.info(f'yotpo关闭订阅成功, customer:{customer}')
|
|
|
|
+ return True
|
|
|
|
|
|
def get_all_list_ids(self):
|
|
def get_all_list_ids(self):
|
|
"""
|
|
"""
|
|
@@ -198,39 +206,3 @@ class YotpoCoreObject:
|
|
# }
|
|
# }
|
|
# self.creat_and_update_customers(customer)
|
|
# self.creat_and_update_customers(customer)
|
|
print('success')
|
|
print('success')
|
|
-
|
|
|
|
-
|
|
|
|
-if __name__ == "__main__":
|
|
|
|
- pass
|
|
|
|
- # YotpoCoreObject().check_api()
|
|
|
|
- # 获取 token ok
|
|
|
|
- # token = get_token()
|
|
|
|
- # print(token)
|
|
|
|
-
|
|
|
|
- # # # 创建用户 ok
|
|
|
|
- # response = creat_and_update_customers()
|
|
|
|
- # print(response)
|
|
|
|
- #
|
|
|
|
- # 检索用户 ok
|
|
|
|
- # response = get_customers_list()
|
|
|
|
- # print(response.text)
|
|
|
|
-
|
|
|
|
- # 检索订阅 ok
|
|
|
|
- # response = get_webhooks_subscriptions()
|
|
|
|
- # print(response.text)
|
|
|
|
-
|
|
|
|
- # 检索接口
|
|
|
|
- # response = get_webhooks("webhooks/targets")
|
|
|
|
- # print(response)
|
|
|
|
-
|
|
|
|
- # 创建筛选器
|
|
|
|
- # create_webhook_filters()
|
|
|
|
- #
|
|
|
|
- # 创建订阅
|
|
|
|
- # create_subscribers()
|
|
|
|
-
|
|
|
|
- # 关闭订阅
|
|
|
|
- # YotpoCoreObject().close_subscribers('1920098158@qq.com', 8589406)
|
|
|
|
-
|
|
|
|
- # 获取list
|
|
|
|
- # get_all_list_ids()
|
|
|