Exemple #1
0
 async def _req(self,
                parse_rsp,
                method,
                url,
                ok_status_codes=None,
                **kwargs):
     if ok_status_codes is None:
         ok_status_codes = self.DEFAULT_OK_STATUS_CODES
     async with sem:
         i = 0
         while True:
             i += 1
             if i >= 10:
                 printer.warn(f'反复请求多次未成功, {url}, {kwargs}')
                 await asyncio.sleep(0.75)
             try:
                 async with self.session.request(method, url,
                                                 **kwargs) as rsp:
                     if rsp.status in ok_status_codes:
                         body = await parse_rsp(rsp)
                         if body:  # 有时候是 None 或空,直接屏蔽。read 或 text 类似,禁止返回空的东西
                             return body
                     elif rsp.status in (412, 403):
                         printer.warn(f'403频繁, {url}, {kwargs}')
                         raise ForbiddenError(msg=url)
             except asyncio.CancelledError:
                 raise
             except ForbiddenError:
                 raise
             except:
                 # print('当前网络不好,正在重试,请反馈开发者!!!!')
                 print(sys.exc_info()[0], sys.exc_info()[1], url)
Exemple #2
0
 async def __request(self, parse_rsp, method, url, **kwargs):
     async with sem:
         i = 0
         while True:
             i += 1
             if i >= 10:
                 printer.warn(f'反复请求多次未成功, {url}, {kwargs}')
             try:
                 async with self.var_session.request(method, url,
                                                     **kwargs) as rsp:
                     if rsp.status == 200:
                         body = await parse_rsp(rsp)
                         if body:  # 有时候是None或空,直接屏蔽。下面的read/text类似,禁止返回空的东西
                             return body
                     elif rsp.status in (412, 403):
                         printer.warn(f'403频繁, {url}, {kwargs}')
                         raise ForbiddenError(msg=url)
             except asyncio.CancelledError:
                 raise
             except ForbiddenError:
                 raise
             except:
                 # print('当前网络不好,正在重试,请反馈开发者!!!!')
                 print(sys.exc_info()[0], sys.exc_info()[1], url)
             await asyncio.sleep(0.02)
Exemple #3
0
    def from_file(cls, promotion_json, user, create_products=False):
        product_ids = set(
            map(lambda prom: prom.get('product_id'), promotion_json))
        products = {
            p.id: p
            for p in Product.query.filter(Product.id.in_(product_ids))
        }

        ret = []

        for promotion in promotion_json:
            product = products.get(promotion['product_id'])
            if not product:
                product_id = promotion['product_id']
                if not create_products:
                    raise NotFoundError(
                        'Product {} not found'.format(product_id),
                        payload={'id': product_id})

                product = Product(id=product_id,
                                  user_id=user.id,
                                  name=promotion['product_name'],
                                  description=promotion['product_description'],
                                  price=promotion['price'])
                db.session.add(product)
                products[product.id] = product

            if product.user_id != user.id:
                raise ForbiddenError(
                    'Forbidden',
                    payload={
                        'error':
                        'User cannot create discounts for these products.'
                    })

            for attr, pattr in [('product_name', 'name'),
                                ('product_description', 'description'),
                                ('price', 'price')]:
                promotion.setdefault(attr, getattr(product, pattr))

            promotion_obj = cls(**promotion)
            ret.append(promotion_obj)

        if ret:
            db.session.add_all(ret)
            db.session.commit()

        return ret