示例#1
0
def test_100_continue_custom_response(loop, test_client):

    @asyncio.coroutine
    def handler(request):
        data = yield from request.post()
        assert b'123', data['name']
        return web.Response()

    @asyncio.coroutine
    def expect_handler(request):
        if request.version == HttpVersion11:
            if auth_err:
                return web.HTTPForbidden()

            request.transport.write(b"HTTP/1.1 100 Continue\r\n\r\n")

    form = FormData()
    form.add_field('name', b'123',
                   content_transfer_encoding='base64')

    app = web.Application(loop=loop)
    app.router.add_post('/', handler, expect_handler=expect_handler)
    client = yield from test_client(app)

    auth_err = False
    resp = yield from client.post('/', data=form, expect100=True)
    assert 200 == resp.status

    auth_err = True
    resp = yield from client.post('/', data=form, expect100=True)
    assert 403 == resp.status
示例#2
0
        def go():
            nonlocal auth_err

            app, _, url = yield from self.create_server('POST', '/')
            app.router.add_route(
                'POST', '/', handler, expect_handler=expect_handler)

            form = FormData()
            form.add_field('name', b'123',
                           content_transfer_encoding='base64')

            resp = yield from request(
                'post', url, data=form,
                expect100=True,  # wait until server returns 100 continue
                loop=self.loop)

            self.assertEqual(200, resp.status)
            resp.close(force=True)

            auth_err = True
            resp = yield from request(
                'post', url, data=form,
                expect100=True,  # wait until server returns 100 continue
                loop=self.loop)
            self.assertEqual(403, resp.status)
            resp.close(force=True)
示例#3
0
def test_files_upload_with_same_key(loop, test_client):
    @asyncio.coroutine
    def handler(request):
        data = yield from request.post()
        files = data.getall('file')
        file_names = set()
        for _file in files:
            assert not _file.file.closed
            if _file.filename == 'test1.jpeg':
                assert _file.file.read() == b'binary data 1'
            if _file.filename == 'test2.jpeg':
                assert _file.file.read() == b'binary data 2'
            file_names.add(_file.filename)
        assert len(files) == 2
        assert file_names == {'test1.jpeg', 'test2.jpeg'}
        resp = web.Response(body=b'OK')
        return resp

    app = web.Application(loop=loop)
    app.router.add_post('/', handler)
    client = yield from test_client(app)

    data = FormData()
    data.add_field('file', b'binary data 1',
                   content_type='image/jpeg',
                   filename='test1.jpeg')
    data.add_field('file', b'binary data 2',
                   content_type='image/jpeg',
                   filename='test2.jpeg')
    resp = yield from client.post('/', data=data)
    assert 200 == resp.status
示例#4
0
async def test_100_continue_custom(aiohttp_client):

    expect_received = False

    async def handler(request):
        data = await request.post()
        assert b'123' == data['name']
        return web.Response()

    async def expect_handler(request):
        nonlocal expect_received
        expect_received = True
        if request.version == HttpVersion11:
            request.transport.write(b"HTTP/1.1 100 Continue\r\n\r\n")

    form = FormData()
    form.add_field('name', b'123',
                   content_transfer_encoding='base64')

    app = web.Application()
    app.router.add_post('/', handler, expect_handler=expect_handler)
    client = await aiohttp_client(app)

    resp = await client.post('/', data=form, expect100=True)
    assert 200 == resp.status
    assert expect_received
示例#5
0
async def call(method, file=None, **kwargs):
    """
    Perform an API call to Slack.

    :param file: File pointer
    :type file: file

    :Keyword Arguments:
        All the arguments required by the Slack API.

    :return: JSON response.
    :rtype: dict
    """
    # JSON encode any sub-structure...
    for k, w in kwargs.items():
        # list, tuple or dict but not a str.
        if isinstance(w, (Mapping, list, tuple)):
            kwargs[k] = json.dumps(w)

    form = FormData(kwargs)

    # Handle file upload
    if file:
        form.add_field('file', file)

    with ClientSession() as session:
        async with session.post('https://slack.com/api/{0}'.format(method),
                                data=form) as response:
            assert 200 == response.status, response
            return await response.json()
示例#6
0
 def go():
     _, _, url = yield from self.create_server("POST", "/", handler)
     _data = FormData()
     _data.add_field("file", b"binary data 1", content_type="image/jpeg", filename="test1.jpeg")
     _data.add_field("file", b"binary data 2", content_type="image/jpeg", filename="test2.jpeg")
     resp = yield from request("POST", url, data=_data, loop=self.loop)
     self.assertEqual(200, resp.status)
     resp.close()
示例#7
0
        def go():
            _, _, url = yield from self.create_server("POST", "/", handler)

            form = FormData()
            form.add_field("name", b"123", content_transfer_encoding="base64")

            resp = yield from request("post", url, data=form, loop=self.loop)

            self.assertEqual(200, resp.status)
            resp.close()
示例#8
0
        def go():
            _, _, url = yield from self.create_server('POST', '/', handler)

            form = FormData()
            form.add_field('name', b'123',
                           content_transfer_encoding='base64')

            resp = yield from request(
                'post', url, data=form,
                loop=self.loop)

            self.assertEqual(200, resp.status)
示例#9
0
        def go():
            app, _, url = yield from self.create_server("POST", "/")
            app.router.add_route("POST", "/", handler)

            form = FormData()
            form.add_field("name", b"123", content_transfer_encoding="base64")

            resp = yield from request(
                "GET", url, data=form, expect100=True, loop=self.loop  # wait until server returns 100 continue
            )

            self.assertEqual(405, resp.status)
            resp.close()
示例#10
0
 def go():
     _, _, url = yield from self.create_server('POST', '/', handler)
     _data = FormData()
     _data.add_field('file', b'binary data 1',
                     content_type='image/jpeg',
                     filename='test1.jpeg')
     _data.add_field('file', b'binary data 2',
                     content_type='image/jpeg',
                     filename='test2.jpeg')
     resp = yield from request('POST', url, data=_data,
                               loop=self.loop)
     self.assertEqual(200, resp.status)
     resp.close()
示例#11
0
        def go():
            _, _, url = yield from self.create_server('POST', '/', handler)

            form = FormData()
            form.add_field('name', b'123',
                           content_transfer_encoding='base64')

            resp = yield from request(
                'post', url, data=form,
                expect100=True,  # wait until server returns 100 continue
                loop=self.loop)

            self.assertEqual(200, resp.status)
            resp.close()
示例#12
0
def test_100_continue(loop, test_client):
    @asyncio.coroutine
    def handler(request):
        data = yield from request.post()
        assert b'123' == data['name']
        return web.Response()

    form = FormData()
    form.add_field('name', b'123',
                   content_transfer_encoding='base64')

    app = web.Application(loop=loop)
    app.router.add_post('/', handler)
    client = yield from test_client(app)

    resp = yield from client.post('/', data=form, expect100=True)
    assert 200 == resp.status
示例#13
0
async def test_100_continue(aiohttp_client):

    async def handler(request):
        data = await request.post()
        assert b'123' == data['name']
        return web.Response()

    form = FormData()
    form.add_field('name', b'123',
                   content_transfer_encoding='base64')

    app = web.Application()
    app.router.add_post('/', handler)
    client = await aiohttp_client(app)

    resp = await client.post('/', data=form, expect100=True)
    assert 200 == resp.status
示例#14
0
async def test_POST_DATA_with_content_transfer_encoding(loop, test_client):

    async def handler(request):
        data = await request.post()
        assert b'123' == data['name']
        return web.Response()

    app = web.Application()
    app.router.add_post('/', handler)
    client = await test_client(app)

    form = FormData()
    form.add_field('name', b'123',
                   content_transfer_encoding='base64')

    resp = await client.post('/', data=form)
    assert 200 == resp.status
    async def test_request_with_file(self):
        debug_app = self.make_debug_app()
        async with TestServer(debug_app) as server:
            url = str(server.make_url("/"))
            app = self.make_app_with_response(target=url)
            payload = FormData()
            payload.add_field("field1", "value1")
            payload.add_field("field2", b"binary", content_type="image/jpeg", filename="image.jpg")

            async with run(app) as client:
                response = await client.request(POST, "/", data=payload)
                body = await response.json()
                self.assertEqual(body["method"], POST)
                self.assertEqual(body["form"], {
                    "field1": ["value1"],
                    "field2": [{
                        "name": "image.jpg",
                        "size": str(len(b"binary")),
                        "type": "image/jpeg",
                    }],
                })
示例#16
0
        def go():
            nonlocal expect_received

            app, _, url = yield from self.create_server('POST', '/')
            app.router.add_route('POST',
                                 '/',
                                 handler,
                                 expect_handler=expect_handler)

            form = FormData()
            form.add_field('name', b'123', content_transfer_encoding='base64')

            resp = yield from request(
                'post',
                url,
                data=form,
                expect100=True,  # wait until server returns 100 continue
                loop=self.loop)

            self.assertEqual(200, resp.status)
            self.assertTrue(expect_received)
示例#17
0
def transmit(temp_path,store_key):

    if os.path.exists(temp_path):
        print('ok it saved')

    print(temp_path)
    print(store_key)

    upload_token = generate_token(store_key)
    
    data = FormData()
    data.add_field('key', store_key)
    data.add_field('token', upload_token)
    data.add_field('file',
                   open(temp_path, 'rb'),
                   filename = os.path.split(temp_path)[-1],
                   content_type = 'application/octet-stream')

    session = ClientSession()
    response = yield from session.post('http://up-z2.qiniu.com',data = data)
    text = yield from response.text()
    yield from session.close()
    print(text)
    os.remove(temp_path)
    return
示例#18
0
async def trigger_publish(conn, data):
    media_type = data.get('media_type')
    service_config = await get_service_config_by_action(conn=conn,
                                                        action='publish',
                                                        media_type=media_type)
    logging.debug(msg='service_config: {}'.format(str(service_config)))
    service_name = service_config.get('name')
    did = data.get('id')
    form = data.get('form')
    metadata = extract_data_from_form(form)
    data = {}
    data.update({'metadata': metadata})
    fd = FormData()
    fd.add_field('data', json.dumps(data), content_type='application/json')
    service_config = await db.get_service_config(conn=conn, name=service_name)
    if service_config:
        endpoint = service_config.get('endpoint')
        config = service_config.get('config')
        fd.add_field('config',
                     json.dumps(config),
                     content_type='application/json')
        with open(TMP_FILE_LOCATION.format(did), 'rb') as f:
            fd.add_field('file',
                         f,
                         filename=did,
                         content_type='application/octet-stream')
            async with new_request(method='POST', url=endpoint,
                                   data=fd) as resp:
                resp_json = await resp.json()
                location = resp_json.get('uri')
                return location
 async def test_file_with_eq_hash_and_name(self, aiohttp_client, event_loop,
                                           create_user, test_conf):
     app = await create_app(config=test_conf)
     client = await aiohttp_client(app)
     data = FormData()
     data.add_field("configuration",
                    io.StringIO("test content"),
                    filename="test_file")
     await client.post(
         "api/v1/configurations/",
         data=data,
         headers={"Authorization": f"Token {create_user['api_key']}"},
     )
     data = FormData()
     data.add_field("configuration",
                    io.StringIO("test content"),
                    filename="test_file")
     r = await client.post(
         "api/v1/configurations/",
         data=data,
         headers={"Authorization": f"Token {create_user['api_key']}"},
     )
     assert r.status == 409
     data = FormData()
     data.add_field("configuration",
                    io.StringIO("test content"),
                    filename="test_file_new")
     r = await client.post(
         "api/v1/configurations/",
         data=data,
         headers={"Authorization": f"Token {create_user['api_key']}"},
     )
     assert r.status == 201
示例#20
0
async def flash_firmware(session, url, auth, exp_id, firm_path, nodes=None):
    """ Flash firmware """
    form = FormData()
    form.add_field('file',
                   open(firm_path, 'rb'),
                   filename=basename(firm_path),
                   content_type="application/octet-stream")
    if splitext(firm_path)[1] == '.bin' and nodes:
        nodes_binary = {}
        nodes_binary['nodes'] = nodes
        nodes_binary['offset'] = 0
        form.add_field("nodes",
                       json.dumps(nodes_binary),
                       content_type="application/json")
    elif nodes:
        form.add_field("nodes",
                       json.dumps(nodes),
                       content_type="application/json")
    url_flash = 'experiments/{}/nodes/flash'.format(exp_id)
    if splitext(firm_path)[1] == '.bin':
        url_flash += '/binary'
    async with session.post(
            urljoin(url, url_flash),
            auth=auth,
            data=form,
    ) as resp:
        res = await resp.json()
        if not "0" in res and not "1" in res:
            assert False
        assert resp.status == 200
示例#21
0
文件: views.py 项目: elgloor/3deposit
async def store_buckets(request):
    PATH = '/bucket'
    async with request.app['db'].acquire() as conn:
        service_config = await get_service_config_by_action(conn=conn, action='store', media_type='default')
    config = service_config.get('config')
    endpoint = service_config.get('endpoint')
    if request.method == 'GET':
        try:
            data = request.query
            config.update({'bucket_name': data.get('bucket_name')})
            fd = FormData()
            fd.add_field('config', json.dumps(config), content_type='application/json')
            async with new_request(method='GET', url=endpoint+PATH, data=fd) as resp:
                try:
                    resp_json = await resp.json()
                except Exception as err:
                    return web.json_response({'err': str(err), 'resp': await resp.text()})
                return web.json_response({'resp': resp_json})
        except Exception as err:
            return web.json_response({'origin': 'gateway', 'err': str(err)})

    if request.method == 'POST':
        try:
            data = await request.json()
            fd = FormData()
            fd.add_field('config', json.dumps(config), content_type='application/json')
            fd.add_field('data', json.dumps(data), content_type='application/json')
            # payload = dict({'config': config, 'data': data})
            async with new_request(method='POST', url=endpoint+PATH, data=fd) as resp:
                resp_json = await resp.json()
                return web.json_response({'resp': resp_json})
        except Exception as err:
            return web.json_response({'origin': 'gateway', 'err': str(err)})
示例#22
0
 async def from_image(cls, ctx, image_url):
     apikeys = await ctx.bot.get_shared_api_tokens("reverseimagesearch")
     apikey = apikeys.get("tracemoe", "")
     async with ctx.typing():
         try:
             async with ctx.cog.session.get(image_url,
                                            raise_for_status=True) as resp:
                 image = BytesIO(await resp.read())
                 image_file = BytesIO()
                 with Image.open(image) as pil_image:
                     with pil_image.convert("RGB") as converted:
                         converted.thumbnail((2048, 2048))
                         converted.save(image_file, "JPEG")
                         image_file.seek(0)
                         image.close()
         except UnidentifiedImageError:
             raise ValueError(_("Unable to convert image."))
         except ClientResponseError as e:
             raise ValueError(
                 _("Unable to get image: {}").format(e.message))
         try:
             data = FormData()
             data.add_field("image", image_file, filename="image.jpg")
             async with ctx.cog.session.post(
                     f"{BASE_API_URL}/search",
                     headers={"x-trace-key": apikey} if apikey else None,
                     params={"anilistInfo": ""},
                     data=data,
             ) as data:
                 # image file closed by aiohttp
                 resp = await data.json(loads=json.loads)
                 if data.status != 200 or (error := resp.get("error")):
                     raise ValueError(
                         _("An error occurred during search: {}").format(
                             error or f"{data.status} ({data.reason})"))
                 return cls(resp)
         except ClientResponseError as e:
             raise ValueError(
                 _("Unable to search for provided image, trace.moe returned {status} ({message})"
                   ).format(status=e.status, message=e.message))
示例#23
0
async def trigger_store(conn, did):

    PATH = '/object'
    service_config = await get_service_config_by_action(conn=conn,
                                                        action='store',
                                                        media_type='default')
    config = dict(service_config.get('config'))
    endpoint = service_config.get('endpoint')
    bucket_name = dict({'bucket_name': '3deposit'})
    config.update(bucket_name)

    with open(TMP_FILE_LOCATION + did, 'rb') as f:
        try:
            deposit_id = dict({'deposit_id': did})
            fd = FormData()
            fd.add_field('file',
                         f,
                         filename=did,
                         content_type='application/octet-stream')
            fd.add_field('config',
                         json.dumps(config),
                         content_type='application/json')
            fd.add_field('data',
                         json.dumps(deposit_id),
                         content_type='application/json')
            async with new_request(method='POST', url=endpoint + PATH,
                                   data=fd) as resp:
                resp_json = await resp.json()
                etag = resp_json.get('etag')
                return etag

        except Exception as err:
            return web.json_response({'origin': 'gateway', 'err': str(err)})
示例#24
0
    async def uploadImage(self, image_bytes: bytes, method: UploadMethods, return_external: bool = False) -> Union[Image, external.Image]:
        """上传一张图片到远端服务器, 需要提供: 图片的原始数据(bytes), 图片的上传类型; 你可以控制是否返回外部态的 Image 消息元素.

        Args:
            image_bytes (bytes): 图片的原始数据
            method (UploadMethods): 图片的上传类型
            return_external (bool, optional): 是否返回外部态的 Image 消息元素. 默认为 False.

        Returns:
            Image(internal): 内部态的 Image 消息元素
            Image(external): 外部态的 Image 消息元素
        """
        data = FormData()
        data.add_field("sessionKey", self.connect_info.sessionKey)
        data.add_field("type", method.value)
        data.add_field("img", image_bytes)
        async with self.session.post(self.url_gen("uploadImage"), data=data) as response:
            response.raise_for_status()
            resp_json = await response.json()
            raise_for_return_code(resp_json)
            external_component = external.Image.parse_obj(resp_json)
            if return_external:
                return external_component
            else:
                return Image.fromExternal(external_component)
示例#25
0
文件: test_cats.py 项目: bakasa/nosht
async def test_upload_image_too_small(cli, url, factory: Factory, login):
    await factory.create_company()
    await factory.create_user()
    await factory.create_cat()
    await login()
    data = FormData()
    data.add_field('image',
                   create_image(200, 100),
                   filename='testing.png',
                   content_type='application/octet-stream')
    r = await cli.post(url('categories-add-image', cat_id=factory.category_id),
                       data=data,
                       headers={
                           'Referer':
                           f'http://127.0.0.1:{cli.server.port}/foobar/',
                           'Origin': f'http://127.0.0.1:{cli.server.port}',
                       })
    assert r.status == 400, await r.text()
    data = await r.json()
    assert data == {
        'message': 'image too small: 200x100 < 1920x500',
    }
示例#26
0
    async def upload_cover(self, group_id, cover_img, width=1590, height=400):
        try:
            params = {
                "group_id": group_id,
                "crop_x": 0,
                "crop_y": 0,
                "crop_x2": width,
                "crop_y2": height
            }
            upload_url = await self.get_response(
                'photos.getOwnerCoverPhotoUploadServer', params)

            data = FormData()
            data.add_field('photo',
                           io.BytesIO(cover_img),
                           filename='photo.jpg',
                           content_type='image/png')

            uploaded_data = await req.post(upload_url["upload_url"],
                                           data=data,
                                           timeout=60)
            uploaded_data = json.loads(uploaded_data.decode('utf-8'))

            params = {
                "hash": uploaded_data["hash"],
                "photo": uploaded_data["photo"],
                'v': '5.103',
                'access_token': self.token
            }

            upload_result = await req.get(self.url +
                                          'photos.saveOwnerCoverPhoto',
                                          params=params)

            return upload_result.decode('utf-8')

        except:
            logs()
            return False
示例#27
0
async def trigger_metadata(data):
    collection_name = 'deposits'
    did = data.get('id')
    form = data.get('form')
    form_data = extract_data_from_form(form)
    deposit_metadata = dict({'deposit_metadata': form_data})
    deposit_id = dict({'deposit_id': did})
    data = {}
    data.update(deposit_id)
    data.update(deposit_metadata)
    config = {}
    collection_name = dict({'collection_name': collection_name})
    config.update(collection_name)
    fd = FormData()
    fd.add_field('data', json.dumps(data), content_type='application/json')
    fd.add_field('config', json.dumps(config), content_type='application/json')
    async with new_request(method='POST',
                           url='http://mongo-service:5000/objects',
                           data=fd) as resp:
        resp_json = await resp.json()
        mongo_id = resp_json.get('post_id')
        return mongo_id
示例#28
0
 def _prepare_data(request: Request) -> Optional[Union[Data, FormData]]:
     if request.data is not None:
         return request.data
     if request.files is not None:
         data = FormData()
         for name, value in request.files.items():
             filename, content_type = None, None
             if isinstance(value, tuple):
                 if len(value) == 2:
                     filename, content = value  # type: ignore
                 else:
                     filename, content, content_type = value  # type: ignore
             else:
                 content = value
             data.add_field(
                 name,
                 content,
                 filename=filename,
                 content_type=content_type,
             )
         return data
     return None
示例#29
0
async def send_request(url):
    headers = {
        "X-Requested-With": "XMLHttpRequest",
        "Accept": "application/json",
        "Authorization": "Bearer " + access_token
    }

    data = FormData()
    data.add_field('file',
                   open('file.xlsx', 'rb'),
                   filename='file.xlsx',
                   content_type='application/vnd.ms-excel')

    async with aiohttp.ClientSession() as session:
        async with session.post(url, data=data, headers=headers) as response:
            data = {
                "message": "failed"
            }
            if response.status == 200:
                data = await response.json()

            print(await response.json())
示例#30
0
    async def _request(
            self,
            http_method: RequestMethod,
            api_method: str,
            **params: ParamType
    ) -> APIResponse:
        data = {
            name: str(value) if isinstance(value, (int,  float)) else value
            for name, value in params.items() if value is not None
        }
        bot_logger.debug('Request %s %s %r', http_method, api_method, data)
        if http_method == RequestMethod.GET:
            if len(data) > 0:
                assert all(isinstance(value, str) for value in data.values())
                request = partial(self.client.get, params=data)
            else:
                request = partial(self.client.get)
        else:
            form_data = FormData()
            for name, value in data.items():
                if isinstance(value, (StreamFile, LocalFile)):
                    form_data.add_field(name, value.content,
                                        content_type=value.content_type,
                                        filename=value.name)
                else:
                    form_data.add_field(name, value)
            request = partial(self.client.post, data=form_data)

        url = TG_API_URL.format(token=self._token, method=api_method)
        async with request(url) as response:
            response_dict = json.loads(await response.read())
        response_logger.debug(response_dict)
        api_response = APIResponse.from_dict(response_dict)

        if api_response.ok:
            return api_response
        else:
            raise Bot._telegram_exception(api_response)
示例#31
0
async def test_upload_logo_too_small(cli, url, factory: Factory, db_conn,
                                     login, dummy_server):
    await factory.create_company()
    await factory.create_user()
    await login()
    assert None is await db_conn.fetchval('SELECT logo FROM companies')
    data = FormData()
    data.add_field('image',
                   create_image(100, 300),
                   filename='testing.png',
                   content_type='application/octet-stream')
    r = await cli.post(url('company-upload', field='logo'),
                       data=data,
                       headers={
                           'Referer':
                           f'http://127.0.0.1:{cli.server.port}/foobar/',
                           'Origin': f'http://127.0.0.1:{cli.server.port}',
                       })
    assert r.status == 400, await r.text()
    data = await r.json()
    assert data == {'message': 'image too small: 100x300 < 256x256'}
    assert dummy_server.app['images'] == []
    assert None is await db_conn.fetchval('SELECT logo FROM companies')
示例#32
0
    async def upload_file_worker(self, file, ts):
        filename, content = self.files.pop(file)
        async with ClientSession() as session:
            form = FormData()
            form.add_field('file', content, filename=filename)

            response = await session.post('https://slack.com/api/files.upload',
                                          data=form,
                                          params={
                                              'channels': self.slack_channel,
                                              'thread_ts': ts,
                                          },
                                          headers={
                                              'Authorization':
                                              f'Bearer {self.slack_token}',
                                          })

            response = await response.json()
            print(response)
            if not response['ok']:
                return response

            return {'ok': True}
示例#33
0
 async def prove(self):
     await self.get_url()
     if self.base_url:
         async with ClientSession() as session:
             for path in self.url_normpath(self.url, './'):
                 url = path + "kindeditor/php/upload_json.php?dir=file"
                 data = FormData()
                 data.add_field('imgFile',
                                "this is a test for you. ",
                                filename='mytestforyou.html',
                                content_type='text/plain')
                 async with session.post(url=url, data=data) as res:
                     if res != None:
                         text = await res.text()
                         try:
                             res = json.loads(text)
                             if 'url' in res.keys(
                             ) and 'kindeditor' in res['url']:
                                 self.flag = 1
                                 self.req.append({"url": url})
                                 self.res.append({"info": url, "key": url})
                         except:
                             pass
示例#34
0
async def upload_to_telegraph(file_id):
    response = await GetFile(file_id).send()
    API_FILE_URL = 'https://api.telegram.org/file/bot{}/'.format(
        context.bot.token)
    url = API_FILE_URL + response.file_path

    session: ClientSession = context.bot.connector._session

    response = await session.get(url)
    file_data = await response.read()

    form = FormData()
    form.add_field('file', file_data)

    async with session.post('https://telegra.ph/upload',
                            data=form) as response:
        resp = await response.json()
        if 'error' in resp:
            logger.error(resp)
            return resp
        else:
            p = (await response.json())[0]['src']
            attach = f'https://telegra.ph{p}'
            return attach
 async def test_save_file_on_upload(self, test_conf, aiohttp_client,
                                    event_loop, create_user, setup_db):
     app = await create_app(config=test_conf)
     client = await aiohttp_client(app)
     data = FormData()
     data.add_field("configuration",
                    io.StringIO("test content"),
                    filename="test.conf")
     r = await client.get(
         "api/v1/configurations/",
         headers={"Authorization": f"Token {create_user['api_key']}"},
     )
     start_configurations = await r.json()
     await client.post(
         "api/v1/configurations/",
         data=data,
         headers={"Authorization": f"Token {create_user['api_key']}"},
     )
     r = await client.get(
         "api/v1/configurations/",
         headers={"Authorization": f"Token {create_user['api_key']}"},
     )
     after_saved_configuration = await r.json()
     assert len(after_saved_configuration) == len(start_configurations) + 1
示例#36
0
def function663(arg1801, arg549):
    var1478 = False

    @asyncio.coroutine
    def function146(arg675):
        var2387 = yield from arg675.post()
        assert (b'123' == var2387['name'])
        return web.Response()

    @asyncio.coroutine
    def function2702(arg602):
        nonlocal expect_received
        var1478 = True
        if (arg602.version == HttpVersion11):
            arg602.transport.write(b'HTTP/1.1 100 Continue\r\n\r\n')

    var3611 = FormData()
    var3611.add_field('name', b'123', content_transfer_encoding='base64')
    var992 = web.Application()
    var992.router.add_post('/', function146, expect_handler=function2702)
    var776 = yield from arg549(var992)
    var4637 = yield from var776.post('/', data=var3611, expect100=True)
    assert (200 == var4637.status)
    assert expect_received
示例#37
0
 async def test_args(self, server):
     url = f"http://127.0.0.1:{server.port}/component/args_test?" \
         f"query_arg=test_query"
     async with ClientSession(conn_timeout=10,
                              read_timeout=10,
                              cookies={
                                  "session": "111",
                                  "uid": "222"
                              },
                              headers={"referer":
                                       'http://www.baidu.com/'}) as session:
         data = FormData()
         data.add_field("form_arg", "test_form")
         resp = await session.post(url, data=data)
         assert await resp.json() == {
             "query_arg": "test_query",
             "referer": 'http://www.baidu.com/',
             "form_arg": "test_form",
             "session": "111",
             "cookies": {
                 "session": "111",
                 "uid": "222"
             }
         }
示例#38
0
async def upload(batch_id, batch_key, worker_id, version, filename):
    async with aiohttp.ClientSession() as session:
        url = 'http://' + server_name + '/submitBatchUnit'
        data = FormData()

        data.add_field('batchID', batch_id)
        data.add_field('batchKey', batch_key)
        data.add_field('workerID', worker_id)
        data.add_field('version', version)
        data.add_field('data',
                       open(filename, 'rb'),
                       filename=filename,
                       content_type='application/x-gzip')

        resp = await session.post(url, data=data)
        print(resp.status)
        print(await resp.text())
示例#39
0
async def test_add_image_delete_old(cli, url, factory: Factory, db_conn, login,
                                    dummy_server):
    await factory.create_company()
    await factory.create_cat()
    await factory.create_user()
    await factory.create_donation_option()
    await login()
    await db_conn.execute("UPDATE donation_options SET image='testing'")
    data = FormData()
    data.add_field('image',
                   create_image(700, 500),
                   filename='testing.png',
                   content_type='application/octet-stream')
    r = await cli.post(url('donation-image-upload',
                           pk=factory.donation_option_id),
                       data=data,
                       headers={
                           'Referer':
                           f'http://127.0.0.1:{cli.server.port}/foobar/',
                           'Origin': f'http://127.0.0.1:{cli.server.port}',
                       })
    assert r.status == 200, await r.text()
    assert sum('DELETE aws_endpoint_url/' in e
               for e in dummy_server.app['log']) == 2
示例#40
0
async def submit_experiment(session):
    """ Submit experiment scenario """
    users = molotov.get_var('users')
    if users.empty():
        print("No users ...")
        assert False
    user = users.get()
    config = molotov.get_var('config')['experiments']
    exp = _Experiment(name=generate_exp_name(),
                      duration=config['duration'])
    alias = AliasNodes(config['nb_nodes'], molotov.get_var('site'), 'm3:at86rf231', False)
    exp.set_alias_nodes(alias)
    form = FormData()
    form.add_field("exp", json_dumps(exp),
                   content_type="application/json")
    async with session.post(
        urljoin(molotov.get_var('url'), 'experiments'),
        # password = Monkey-<login>
        auth=get_auth(user, 'Monkey-{}'.format(user)),
        data=form,
    ) as resp:
        res = await resp.json()
        assert res['id'] is not None
        assert resp.status == 200
示例#41
0
    async def search(cls, url: str) -> str:
        data = await cls._request(url)
        data = data["docs"]

        d = dict()
        for i in range(len(data)):
            if data[i]["title_chinese"] in d.keys():
                d[data[i]["title_chinese"]][0] += data[i]["similarity"]
            else:
                m = data[i]["at"] / 60
                s = data[i]["at"] % 60

                if not data[i]["episode"]:
                    n = 1
                else:
                    n = data[i]["episode"]

                d[Translate(data[i]["title_chinese"]).to_simple()] = [
                    data[i]["similarity"],
                    f"第{n}集",
                    f"{int(m)}分{int(s)}秒处",
                ]

        result = sorted(d.items(), key=lambda x: x[1], reverse=True)
        t = 0
        msg0 = str()
        for i in result:
            t += 1
            s = "%.2f%%" % (i[1][0] * 100)
            msg0 = msg0 + ("\n——————————\n"
                           f"({t}) Similarity: {s}\n"
                           f"Name: {i[0]}\n"
                           f"Time: {i[1][1]} {i[1][2]}")

        if len(result) == 2:
            return msg0
        else:
            data = FormData()
            data.add_field("poster", "ATRI running log")
            data.add_field("syntax", "text")
            data.add_field("expiration", "day")
            data.add_field("content", msg0)

            repo = f"详细请移步此处~\n{await UbuntuPaste(data).paste()}"
            return repo
示例#42
0
    async def track_error(track_id: str) -> str:
        try:
            data = load_error(track_id)
        except ReadFileError:
            return "请检查ID是否正确..."

        prompt = data.get("prompt", "ignore")
        time = data.get("time", "ignore")
        content = data.get("content", "ignore")

        msg0 = TRACK_BACK_FORMAT.format(track_id=track_id,
                                        prompt=prompt,
                                        time=time,
                                        content=content)
        f_data = FormData()
        f_data.add_field("poster", "ATRI running log")
        f_data.add_field("syntax", "text")
        f_data.add_field("expiration", "day")
        f_data.add_field("content", msg0)

        repo = f"详细请移步此处~\n{await UbuntuPaste(f_data).paste()}"
        return repo
示例#43
0
async def publish_models(request):
    PATH = '/models'
    async with request.app['db'].acquire() as conn:
        service_config = await get_service_config_by_action(conn=conn,
                                                            action='publish',
                                                            media_type='model')
    logging.debug(msg='service_config: {}'.format(str(service_config)))
    service_name = service_config.get('name')

    if request.method == 'POST':
        try:
            q = request.query
            did = q.get('deposit_id')
            metadata = await request.json()
            data = {}
            data.update({'metadata': metadata})
            fd = FormData()
            fd.add_field('data',
                         json.dumps(data),
                         content_type='application/json')
            async with request.app['db'].acquire() as conn:
                service_config = await db.get_service_config(conn=conn,
                                                             name=service_name)
                if service_config:
                    endpoint = service_config.get('endpoint')
                    config = service_config.get('config')
                    fd.add_field('config',
                                 json.dumps(config),
                                 content_type='application/json')
                    with open('./data/{}'.format(did), 'rb') as f:
                        fd.add_field('file',
                                     f,
                                     filename=did,
                                     content_type='application/octet-stream')
                        async with new_request(method='POST',
                                               url=endpoint + PATH,
                                               data=fd) as resp:
                            resp_json = await resp.json()
                            uri = resp_json.get('uri')
                            return web.json_response(uri)
                else:
                    return web.json_response({
                        'err':
                        'could not retrieve config for service: {}'.format(
                            service_name)
                    })
        except Exception as err:
            return web.json_response({'err': str(err)})
示例#44
0
async def trigger_metadata(data):
    did = data.get('id')
    form = data.get('form')
    media_type = data.get('media_type')
    form_data = extract_data_from_form(form)
    deposit_date = data.get('deposit_date')
    form_data.update({'deposit_date': deposit_date})
    deposit_metadata = dict({'deposit_metadata': form_data})

    deposit_id = dict({'deposit_id': did})
    data = {}
    data.update({'deposit_date': deposit_date})
    data.update(deposit_id)
    data.update(deposit_metadata)
    data.update({'media_type': media_type})

    fd = FormData()
    fd.add_field('data', json.dumps(data), content_type='application/json')
    with open(TMP_FILE_LOCATION + did, 'rb') as f:
        fd.add_field('file',
                     f,
                     filename=did,
                     content_type='application/octet-stream')
        async with new_request(method='POST',
                               url='http://metadata-service:5000/',
                               data=fd) as resp:
            if resp.content_type == 'application/json':
                resp_json = await resp.json()
            else:
                resp_json = await resp.text()
            logging.debug(
                f'trigger_metadata, metadata-service returned: {str(resp_json)}'
            )
            technical_metadata = resp_json.get('technical_metadata')

        if technical_metadata:
            logging.debug(f'technical_metadata: {technical_metadata}')
            data.update({'technical_metadata': technical_metadata})

    fd = FormData()
    fd.add_field('data', json.dumps(data), content_type='application/json')
    async with new_request(method='POST',
                           url='http://mongo-service:5000/objects',
                           data=fd) as resp:
        resp_json = await resp.json()
        mongo_id = resp_json.get('mongo_id')
        return mongo_id
示例#45
0
 async def uploadImage(self,
                       image_bytes: bytes,
                       method: UploadMethods,
                       return_external: bool = False) -> Image:
     data = FormData()
     data.add_field("sessionKey", self.connect_info.sessionKey)
     data.add_field("type", method.value)
     data.add_field("img", image_bytes)
     async with self.session.post(self.url_gen("uploadImage"),
                                  data=data) as response:
         response.raise_for_status()
         resp_json = await response.json()
         raise_for_return_code(resp_json)
         external_component = external.Image.parse_obj(resp_json)
         if return_external:
             return external_component
         else:
             return Image.fromExternal(external_component)
示例#46
0
    async def _upload_test(self, test_id):
        csv_file = await self._get_csv_data(test_id)

        form = FormData()
        form.add_field('token', BMSENSE_TOKEN, content_type='text/plain')
        form.add_field('projectKey', 'AIOLT_TEST')
        form.add_field('jtl_file', csv_file, filename='report.gz')

        async with self._client_session.post(url=self.BASE_URL + 'api/file/upload/?format=json',
                                             headers={'User-Agent': 'AIOLT-Uploader'},
                                             data=form) as response:
            if response.status == 200:
                async with self._db.acquire() as conn:
                    async with conn.transaction():
                        await conn.execute("""
                            UPDATE tests
                            SET status = 'uploaded'
                            WHERE id = $1 
                            """, test_id)
                        self._logger.info(f'Test \'{test_id}\' results are successfully uploaded')
            else:
                self._logger.error(f'Unexpected response from BMSense: {response!r}')