def __init__(self, env='QA'): self.log = Logger("debug") self.opera = OperationIni(fileName='config.ini', pathName='config') path = '/website/saas/account/api2/user/login' self.key = env.lower() + '_token' d = get_env_authorization(env=env) self.url = d[0] + path self.cookie = d[1] self.userName = d[2] self.passWord = d[3] # if env == 'QA': # self.url = self.opera.read_ini(section='Authorization', key='qa_url') + path # self.cookie = self.opera.read_ini(section='Authorization', key='qa_cookie') # self.userName = self.opera.read_ini(section='Authorization', key='qa_username') # self.passWord = self.opera.read_ini(section='Authorization', key='qa_password') # if env == 'DEV': # self.url = self.opera.read_ini(section='Authorization', key='dev_url') + path # self.cookie = self.opera.read_ini(section='Authorization', key='dev_cookie') # self.userName = self.opera.read_ini(section='Authorization', key='dev_username') # self.passWord = self.opera.read_ini(section='Authorization', key='dev_password') self.headers = { 'Cookie': self.cookie, 'Content-Type': 'application/x-www-form-urlencoded' }
class GetAuth: def __init__(self, env='QA'): self.log = Logger("debug") self.opera = OperationIni(fileName='config.ini', pathName='config') path = '/website/saas/account/api2/user/login' self.key = env.lower() + '_token' d = get_env_authorization(env=env) self.url = d[0] + path self.cookie = d[1] self.userName = d[2] self.passWord = d[3] # if env == 'QA': # self.url = self.opera.read_ini(section='Authorization', key='qa_url') + path # self.cookie = self.opera.read_ini(section='Authorization', key='qa_cookie') # self.userName = self.opera.read_ini(section='Authorization', key='qa_username') # self.passWord = self.opera.read_ini(section='Authorization', key='qa_password') # if env == 'DEV': # self.url = self.opera.read_ini(section='Authorization', key='dev_url') + path # self.cookie = self.opera.read_ini(section='Authorization', key='dev_cookie') # self.userName = self.opera.read_ini(section='Authorization', key='dev_username') # self.passWord = self.opera.read_ini(section='Authorization', key='dev_password') self.headers = { 'Cookie': self.cookie, 'Content-Type': 'application/x-www-form-urlencoded' } def get_auth(self): ''' 获取token :return: rsp, 登录后的token ''' data = { 'zone': '0086', 'phone': self.userName, 'password': self.passWord, 'remember': False, 'passwordType': 'new' } self.log.info('开始:调用获取B端后台token接口,请求地址为:{0},入参为:{1},请求头为:{2}'.format( self.url, data, self.headers)) r = requests.post(url=self.url, data=data, headers=self.headers) try: token = r.json()['data']['token'] self.log.info('结束:调用获取B端后台token接口,获取到token为:{0}'.format(token)) return token, r.json() except Exception as f: self.log.error('获取B端后台token失败,错误日志为:{0}'.format(f)) print(f) def set_auth(self): ''' 存储最新的后端Authorization :return: ''' token = self.get_auth()[0] self.opera.write_ini(section='Authorization', data=token, key=self.key)
def get_env_access_token(env, pid): ''' 通过不同的env来获取不同的access_token :param env: 环境 :param pid: 商家id :return: 返回不同环境的access_token ''' if pid == None: if env == 'QA': pid = 1 if env == 'DEV': pid = 17 # TODO 预留prod环境 if env == 'PROD': pid = 17 access_token = None if env == 'QA': opera = OperationIni(fileName='config.ini', pathName='config') access_token = opera.read_ini(section='access_token', key='qa_{0}_access_token'.format(pid)) if env == 'DEV': # 大坑:一定要在每个if中重新调用读取ini文件的方法,要不然读取的内容不是最新的 opera = OperationIni(fileName='config.ini', pathName='config') access_token = opera.read_ini(section='access_token', key='dev_{0}_access_token'.format(pid)) if env == 'PROD': opera = OperationIni(fileName='config.ini', pathName='config') access_token = opera.read_ini(section='access_token', key='prod_{0}_access_token'.format(pid)) return access_token
def __init__(self, env='QA'): self.log = Logger("debug") opera = OperationIni(fileName='config.ini', pathName='config') self.env = env self.get_access_token = GetAccessToken(env=env) # env字符串转小写 env = env.lower() key = env + '_url' self.url = opera.read_ini(section='goods', key=key) self.path = opera.read_ini(section='goods', key='queryGoodsDetail') self.access_token = self.get_access_token.get_ini_access_token()
def __init__(self, pid, env='QA'): self.log = Logger("debug") opera = OperationIni(fileName='config.ini', pathName='config') self.get_skuId = GetGoodsDetail(env=env, pid=pid) self.get_access_token = GetAccessToken(env=env, pid=pid) # env字符串转小写 env = env.lower() key = env + '_url' self.base_url = opera.read_ini(section='goods', key=key) self.path = opera.read_ini(section='goods', key='wholeUpdateStock') self.access_token = self.get_access_token.get_ini_access_token()
def __init__(self, env='QA'): self.log = Logger("debug") self.opera = OperationIni(fileName='config.ini', pathName='config') # 获取鉴权参数 self.clientId = translate_env_access_token(env=env)[0] self.clientSecret = translate_env_access_token(env=env)[1] self.env = env # env字符串转小写 self.l_env = env.lower() self.key = self.l_env + '_url' # 获取ini文件中的base_url base_url = self.opera.read_ini(section='access_token', key=self.key) self.url = base_url.format(self.clientId, self.clientSecret)
def __init__(self, env, serviceName): self.log = Logger("debug") opera = OperationIni() chrome_driver = findPath.data_dir(fileName='chromedriver.exe', pathName='driver') base_url = opera.read_ini(section='CONFIG', key='base_url') url = base_url + opera.read_ini(section=env, key='url') self.userName = opera.read_ini(section='CONFIG', key='userName') self.passWord = opera.read_ini(section='CONFIG', key='passWord') self.ServiceName = opera.read_ini(section='CONFIG', key=serviceName) chrome_options = Options() # 设置chrome浏览器无界面模式 chrome_options.add_argument('--headless') self.log.info("开始调用webdriver,当前模式为Chrome无界面模式") self.d = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options) self.d.maximize_window() self.log.info('成功打开谷歌浏览器') self.d.get(url) self.d.implicitly_wait(30) print('成功打开网址:{0}'.format(url)) self.log.info('成功打开网址:{0}'.format(url))
def get_env_access_token(env): ''' 通过不同的env来获取不同的access_token :param env: 环境 :return: 返回不同环境的access_token ''' access_token = None if env == 'QA': opera = OperationIni(fileName='config.ini', pathName='config') access_token = opera.read_ini(section='access_token', key='qa_access_token') if env == 'DEV': # 大坑:一定要在每个if中重新调用读取ini文件的方法,要不然读取的内容不是最新的 opera = OperationIni(fileName='config.ini', pathName='config') access_token = opera.read_ini(section='access_token', key='dev_access_token') return access_token
class updateGoodsPrice: def __init__(self, env='QA'): self.log = Logger("debug") self.opera = OperationIni(fileName='config.ini', pathName='config') self.get_skuId = GetGoodsDetail(env=env) self.get_access_token = GetAccessToken(env=env) # env字符串转小写 env = env.lower() key = env + '_url' self.base_url = self.opera.read_ini(section='goods', key=key) self.path = self.opera.read_ini(section='goods', key='updatePrice') self.access_token = self.get_access_token.get_ini_access_token() def update_goods_price(self, storeId, goodsId, originalPrice, salePrice): ''' 修改商品价格 :param storeId: 门店id :param goodsId: 商品id :param originalPrice: 市场价 :param salePrice: 商家统一价 :return: rsq ''' url = self.base_url.format(self.path, self.access_token) # 获取skuId try: self.skuId = self.get_skuId.get_goods_detail(goodsId, storeId)[1] json_data = { 'goodsId': goodsId, 'skuList': [{ 'skuId': self.skuId, 'originalPrice': originalPrice, 'salePrice': salePrice, }], 'storeId': storeId } self.log.info('开始:调用update_goods_price方法,请求地址为:{0},入参为:{1}'.format( url, json_data)) r = requests.post(url=url, json=json_data) # 如果access_token无效 if r.json()['data'] == 'invalid accesstoken': self.log.warning('提示:ini文件中的accesstoken失效,开始获取新的accesstoken') # 获取最新的token并存入ini文件 self.get_access_token.set_access_token() # 注意:这里一定要重新获取一次ini文件中的access_token new_access_token = self.get_access_token.get_ini_access_token() url = self.base_url.format(self.path, new_access_token) self.log.warning( '开始:调用update_goods_price方法,请求地址为:{0},入参为:{1}'.format( url, json_data)) res = requests.post(url=url, json=json_data) self.log.warning('结束:调用update_goods_price方法,返回数据为:{0}'.format( res.json())) return res.json() else: self.log.info('结束:调用update_goods_price方法,返回数据为:{0}'.format( r.json())) return r.json() except Exception as f: self.log.error('修改商品失败,错误日志为:{0}'.format(f)) return {'msg': '底层接口请求失败,请检查所传字段的数据是否正确'}
def get_delivery_type(pid=None, env='QA', storeId=None, deliveryType=1, goodsId=""): opera = OperationIni(fileName='config.ini', pathName='config') get_access_token = GetAccessToken(env=env, pid=pid) print(pid) # env字符串转小写 x_env = env.lower() key = x_env + '_url' base_url = opera.read_ini(section='goods', key=key) path = opera.read_ini(section='goods', key='deliveryType') access_token = get_access_token.get_ini_access_token() url = base_url.format(path, access_token) json_data = {"pid": pid, "storeId": storeId, "goodsId": goodsId} log.info('开始:调用get_delivery_type方法,请求地址为:{0},入参为:{1}'.format( url, json_data)) requests.packages.urllib3.disable_warnings() r = requests.post(url=url, json=json_data, verify=False) # 如果access_token无效 if r.json()['data'] == 'invalid accesstoken': # 获取最新的token并存入ini文件 log.warning('提示:ini文件中的accesstoken失效,开始获取新的accesstoken') get_access_token.set_access_token() # 注意:这里一定要重新获取一次ini文件中的access_token new_access_token = get_access_token.get_ini_access_token() url = base_url.format(path, new_access_token) log.warning('开始:调用get_delivery_type方法,请求地址为:{0},入参为:{1}'.format( url, json_data)) requests.packages.urllib3.disable_warnings() res = requests.post(url=url, json=json_data, verify=False) try: deliveryTypeId = delivery_type(result=res.json(), deliveryType=deliveryType) log.warning( '结束:调用get_delivery_type方法,返回数据为:{0},返回deliveryTypeId为:{1}'. format(res.json(), deliveryTypeId)) return res.json(), deliveryTypeId except Exception as f: log.error('调用查询配送方式接口失败,错误日志为:{0}'.format(f)) return r.json() elif r.json()['code']['errmsg'] == '根据Pid查询storeId失败,此商家不存在此门店': # 获取最新的token并存入ini文件 log.warning('提示:根据Pid查询storeId失败,此商家不存在此门店,尝试开始获取新的accesstoken') get_access_token.set_access_token() # 注意:这里一定要重新获取一次ini文件中的access_token new_access_token = get_access_token.get_ini_access_token() url = base_url.format(path, new_access_token) log.warning('开始:调用get_delivery_type方法,请求地址为:{0},入参为:{1}'.format( url, json_data)) requests.packages.urllib3.disable_warnings() res = requests.post(url=url, json=json_data, verify=False) try: deliveryTypeId = delivery_type(result=res.json(), deliveryType=deliveryType) log.warning( '结束:调用get_delivery_type方法,返回数据为:{0},返回deliveryTypeId为:{1}'. format(res.json(), deliveryTypeId)) return res.json(), deliveryTypeId except Exception as f: log.error('调用查询配送方式接口失败,错误日志为:{0}'.format(f)) return res.json() else: deliveryTypeId = delivery_type(result=r.json(), deliveryType=deliveryType) log.info( '结束:调用get_delivery_type方法,返回数据为:{0},返回deliveryTypeId为:{1}'.format( r.json(), deliveryTypeId)) # print('ddddd:',deliveryTypeId) return r.json(), deliveryTypeId
def __init__(self, pid, env): self.log = Logger("debug") self.pid = pid self.env = env self.opera = OperationIni() self.get_order_detail = GetOrderDetail(pid=pid, env=env)
class MockOrderThrow: def __init__(self, pid, env): self.log = Logger("debug") self.pid = pid self.env = env self.opera = OperationIni() self.get_order_detail = GetOrderDetail(pid=pid, env=env) def mock_order_throw(self, orderNo): url = self.opera.read_ini(self.env, key='mock_order_throw_ip') mock_url = 'http://' + url + ':8080/service' headers = {'Content-Type': 'application/x-www-form-urlencoded'} order_detail = self.get_order_detail.get_order_item_id_skuNum( orderNo=orderNo) pickingPackageList = order_detail[0] storeId = order_detail[1] wid = order_detail[2] # 组装paramterInput参数 paramterInput = [{ "markNo": "111", "orderNo": orderNo, "pickingPackageList": pickingPackageList, "pid": self.pid, "storeId": storeId, "wid": wid }] # 组装参数 data = { 'serviceName': 'orderCenterUpdateExportService', 'methodName': 'pickingAndDelivery', 'paramterInput': '{0}'.format(paramterInput) } # 连接VPN start_vpn() code = None try: self.log.info("开始:调用订单抛出服务接口,请求地址为:{0},入参为:{1},请求头为:{2}".format( mock_url, data, headers)) r = requests.post(url=mock_url, data=data, headers=headers, timeout=3) code = r.status_code result = r.json() print('我要看:{0}'.format(result)) self.log.info("结束:调用订单抛出服务接口,返回数据打印:{0}".format(result)) # 关闭VPN stop_vpn() return result except Exception as f: print(f) status = False # print(status) if status == False or code != 200: self.log.warning('IP已失效,重新获取IP') url = GetSoaServerIp( env=self.env, serviceName='mock_order_throw_servicename').get_soa_url() self.log.warning("获取的新IP为:{0}".format(url)) self.opera.write_ini(section=self.env, data=url, key='mock_order_throw_ip') mock_url = 'http://' + url + ':8080/service' self.log.warning("请求url为:{0},请求data为:{1},请求头为:{2}".format( mock_url, data, headers)) try: self.log.warning( "开始:调用订单抛出服务接口,请求地址为:{0},入参为:{1},请求头为:{2}".format( mock_url, data, headers)) r = requests.post(url=mock_url, data=data, headers=headers) result = r.json() self.log.warning("结束:调用订单抛出服务接口,返回数据打印:{0}".format(result)) # 关闭VPN stop_vpn() return result except Exception as f: msg = {'msg': '发生未知错误,请联系管理员,错误日志为:{0}'.format(f)} self.log.error('发生未知错误,请联系管理员,错误日志为:{0}'.format(f)) # 关闭VPN stop_vpn() return msg # g = MockOrderThrow(pid=1,env='QA') # g.mock_order_throw(orderNo='10094010113')
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/8/8 18:09 # @Author : Weiqiang.long # @Site : # @File : get_disparate_env_data.py # @Software: PyCharm from util.readTxt import OperationIni opera = OperationIni(fileName='config.ini', pathName='config') def get_env_access_token(env, pid): ''' 通过不同的env来获取不同的access_token :param env: 环境 :param pid: 商家id :return: 返回不同环境的access_token ''' if pid == None: if env == 'QA': pid = 1 if env == 'DEV': pid = 17 # TODO 预留prod环境 if env == 'PROD': pid = 17 access_token = None
class GetAccessToken: def __init__(self, env='QA'): self.log = Logger("debug") self.opera = OperationIni(fileName='config.ini', pathName='config') # 获取鉴权参数 self.clientId = translate_env_access_token(env=env)[0] self.clientSecret = translate_env_access_token(env=env)[1] self.env = env # env字符串转小写 self.l_env = env.lower() self.key = self.l_env + '_url' # 获取ini文件中的base_url base_url = self.opera.read_ini(section='access_token', key=self.key) self.url = base_url.format(self.clientId, self.clientSecret) # if env == 'DEV': # self.clientId = self.opera.read_ini(section='access_token', key='dev_clientId') # self.clientSecret = self.opera.read_ini(section='access_token', key='dev_clientSecret') # self.access_token = self.opera.read_ini(section='access_token', key='dev_access_token') # if env == 'QA': # self.clientId = self.opera.read_ini(section='access_token', key='qa_clientId') # self.clientSecret = self.opera.read_ini(section='access_token', key='qa_clientSecret') # self.access_token = self.opera.read_ini(section='access_token', key='qa_access_token') def get_access_token(self): ''' 通过clientId、clientSecret参数请求鉴权接口,获取最新的access_token :return: access_token ''' self.log.info('开始:调用access_token接口,请求地址为:{0}'.format(self.url)) r = requests.post(url=self.url) access_token = r.json()['access_token'] self.log.info( '结束:调用access_token接口,获取的access_token为:{0}'.format(access_token)) return access_token def set_access_token(self): ''' 把最新的access_token存入ini文件,自动覆盖旧的access_token :return: ''' key = self.l_env + '_access_token' access_token = self.get_access_token() # 保存最新获取的access_token,存入ini文件 self.opera.write_ini(section='access_token', data=access_token, key=key) self.log.info( '成功写入最新的access_token到ini文件中,子节点为:access_token,key为:{0},写入的access_token为:{1}' .format(access_token, key)) def get_ini_access_token(self): ''' 获取ini文件中已有的access_token :return: ''' # 获取ini文件中已有的access_token access_token = get_env_access_token(env=self.env) self.log.info('获取到ini文件中已有的access_token为:{0}'.format(access_token)) return access_token