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
def _build_form(source, kwargs): source = os.path.abspath(source) sourcedir = os.path.dirname(source) flist = [source] if os.path.isdir(source): recursive = kwargs.get('recursive') flist = glob(os.path.join(source, "**"), recursive=recursive) form = FormData() for path in flist: relpath = os.path.relpath(path, sourcedir) if os.path.isdir(path): form.add_field('files', BytesIO(), filename=relpath, content_type='application/x-directory') else: form.add_field('files', open(path, 'rb'), filename=relpath) return form
async def get_user_info_and_redirect(access_token, next_url, request_session): ''' We use the access_token to get the user info. Upon success, we save the info in request_session and redirect the next_url. On failure (ie an invalid token), we remove the info from the request_session, try to get an explanation, and do _not_ redirect anywhere. ''' # LOG.debug('Token: %s', access_token) async with ClientSession() as session: headers = { 'Accept': 'application/json', 'Authorization': 'Bearer ' + access_token } LOG.debug('Contacting %s', conf.idp_user_info) async with session.get(conf.idp_user_info, headers=headers) as resp: # LOG.debug('Response %s', resp) if resp.status == 200: user = await resp.json() # Save and exit LOG.info('user: %s', user) request_session['user'] = user raise HTTPFound(next_url) else: content = await resp.text() LOG.error('Content: %s', content) # Invalid access token LOG.error('Invalid token') del request_session['access_token'] if 'user' in request_session: del request_session['user'] # Get the explanation async with ClientSession() as session: async with session.post(conf.idp_introspection, auth=BasicAuth( conf.idp_client_id, password=conf.idp_client_secret), data=FormData( { 'token': access_token, 'token_type_hint': 'access_token' }, charset='UTF-8')) as resp: LOG.debug('Response %s', resp) content = await resp.text() LOG.debug('Content: %s', content) LOG.debug('Invalid token, try to get a new one')
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())
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 submit_file( self, content: bytes, filename: str, ) -> str: if len(content) > 32 * 1024 ** 2: endpoint = await self.api_request('files/upload_url') endpoint = endpoint['data'] else: endpoint = 'files' data = FormData() data.add_field( 'file', content, filename = filename, content_type = 'application/octet-stream', ) j = await self.api_request(endpoint, data=data) return j['data']['id']
def vimcn(arg, lines, send): print('vimcn') url = 'https://cfp.vim-cn.com/' code = '\n'.join(lines) or arg['code'] or '' if not code: raise Exception() data = FormData() data.add_field('vimcn', code) text = yield from fetch('POST', url, data=data, content='text') esc = re.compile(r'\x1b[^m]*m') text = esc.sub('', text) line = text.splitlines() send('[\\x0302 {0} \\x0f]'.format(line[0]))
async def scenario_orders_post_csv(session): customer_id = random.randint(1, 1000) lines = [] for i in range(0, random.randint(1, 5)): lines.append(','.join(map(str, [ random.randint(1, 6), random.randint(1, 4) ]))) data = FormData() data.add_field('customer', str(customer_id)) data.add_field( 'file', io.BytesIO('\n'.join(lines).encode('utf8')), filename='data.csv', content_type='text/plain' ) async with session.post(join(SERVER_URL, 'api', 'orders', 'csv'), data=data) as resp: assert resp.status == 200, resp.status
async def save(self, record: str, metadata: str) -> str: data = FormData() data.add_field("file", record, filename="data", content_type="application/json") url = f"{self.settings['api_url']}{API_ENDPOINT}" LOGGER.info( f"""DataVault.save: url: {url} id: {id} settings: {self.settings} """ ) async with ClientSession() as session: response = await session.post(url=url, data=data) response_text = await response.text() return response_text
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
async def post_image(self, asset_id: str, filename: str, src: bytes, **kwargs): url = f"{self.url_base}/assets/{asset_id}/images/{filename}" form_data = FormData() form_data.add_field('file', src, filename=filename, content_type='application/octet-stream') async with aiohttp.ClientSession(headers=self.headers) as session: async with await session.post(url=url, data=form_data, **kwargs) as resp: if resp.status == 200: resp = await resp.json() return resp await raise_exception_from_response(resp, **kwargs)
async def test_upload_no_image(cli, url, factory: Factory, login): await factory.create_company() await factory.create_user() await factory.create_cat() await login() data = FormData() data.add_field('wrong', create_image(), 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()
async def post_file(self, form: FileData, **kwargs): data = FormData() data.add_field('objectName', str(form.objectName)) data.add_field('objectData', form.objectData) data.add_field('objectSize', str(form.objectSize)) data.add_field('contentType', form.content_type) data.add_field('contentEncoding', form.content_encoding) if form.owner: data.add_field('owner', form.owner) async with aiohttp.ClientSession(headers=self.headers) as session: async with await session.post(url=self.upload_file_url, data=data, **kwargs) as resp: if resp.status == 201: return await resp.read() await raise_exception_from_response(resp)
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)
async def trigger_store(conn, did): fd = FormData() deposit_id = dict({'deposit_id': did}) with open(TMP_FILE_LOCATION.format(did), 'rb') as f: fd.add_field('file', f, filename=did, content_type='application/octet-stream') async with ClientSession() as sess: async with sess.request(url='http://gateway:8080/store/objects', method='POST', data=fd, params=deposit_id) as resp: resp_json = await resp.json() etag = resp_json.get('etag') logging.debug( msg=f'trigger_store resp: {str(resp_json)}, {etag}') return etag
async def _async_send_remote_file_message( self, url, targets, message, title, *, username=None, password=None ): """Upload a remote file (with message) to Slack. Note that we bypass the python-slackclient WebClient and use aiohttp directly, as the former would require us to download the entire remote file into memory first before uploading it to Slack. """ if not self._hass.config.is_allowed_external_url(url): _LOGGER.error("URL is not allowed: %s", url) return filename = _async_get_filename_from_url(url) session = aiohttp_client.async_get_clientsession(self.hass) kwargs = {} if username and password is not None: kwargs = {"auth": BasicAuth(username, password=password)} resp = await session.request("get", url, **kwargs) try: resp.raise_for_status() except ClientError as err: _LOGGER.error("Error while retrieving %s: %s", url, err) return data = FormData( { "channels": ",".join(targets), "filename": filename, "initial_comment": message, "title": title or filename, "token": self._client.token, }, charset="utf-8", ) data.add_field("file", resp.content, filename=filename) try: await session.post("https://slack.com/api/files.upload", data=data) except ClientError as err: _LOGGER.error("Error while uploading file message: %s", err)
async def store_objects(request): PATH = '/object' async with request.app['db'].acquire() as conn: service_config = await get_service_config_by_action(conn=conn, action='store', media_type='default') logging.debug(msg='store_objects service_config: {}'.format(str(service_config))) config = dict(service_config.get('config')) endpoint = service_config.get('endpoint') bucket_name = dict({'bucket_name': '3deposit'}) config.update(bucket_name) if request.method == 'GET': try: data = request.query config.update({'bucket_name': data.get('bucket_name')}) payload = dict({'config': config}) async with new_request(method='GET', url=endpoint+PATH, json=payload) 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, 'payload': payload}) except Exception as err: return web.json_response({'origin': 'gateway', 'err': str(err)}) if request.method == 'POST': try: q = dict(request.query) fd = FormData() reader = await request.multipart() while True: part = await reader.next() if part is None: break if part.name == 'file': fd.add_field(name='file', value=await part.read(), filename=q.get('deposit_id'), content_type='application/octet-stream') else: continue fd.add_field('config', json.dumps(config), content_type='application/json') fd.add_field('data', json.dumps(q), content_type='application/json') async with new_request(method='POST', url=endpoint+PATH, data=fd) as resp: resp_json = await resp.json() return web.json_response(resp_json) except Exception as err: return web.json_response({'origin': 'gateway', 'err': str(err)})
async def test_add_image_wrong_id(cli, url, factory: Factory, db_conn, login): await factory.create_company() await factory.create_cat() await factory.create_user() await login() assert None is await db_conn.fetchval('SELECT image FROM donation_options') 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=999), 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 == 404, await r.text()
async def test_sign_in_by_password_good_password(self): param = FormData({ 'login': '******', 'password': '******' }) resp = await self.client.request( "POST", "/api/AuthService/User/sign_in_by_password", data=param) if resp.status != 200: print(await resp.text()) self.assertEqual(200, resp.status, 'response status') set_cookie = resp.headers.get('Set-Cookie') session = urllib.parse.unquote(set_cookie).split(';') self.assertEqual('session', session[0][:7], 'set session') session = session[0][8:] data = await resp.json() self.assertEqual(session, data['session']) resp = await self.client.request( "POST", "/api/AuthService/User/sign_in_by_password", data=param) set_cookie2 = resp.headers.get('Set-Cookie') data2 = await resp.json() if resp.status != 200: print(await resp.text()) self.assertEqual(200, resp.status, 'response status') self.assertIsNone(set_cookie2, 'don\'t new session') self.assertEqual(session, data['session'], 'return first session') self.assertEqual(session, data2['session']) resp = await self.client.request("POST", "/api/AuthService/User/sign_out", data=param) set_cookie3 = resp.headers.get('Set-Cookie') session2 = urllib.parse.unquote(set_cookie3).split(';') self.assertEqual('session', session2[0][:7], 'set session') session2 = session2[0][8:] data3 = await resp.json() if resp.status != 200: print(await resp.text()) self.assertEqual('""', session2, 'new_session') self.assertEqual(200, resp.status, 'response status') self.assertEqual({}, data3)
async def test_upload_logo_convert(cli, url, factory: Factory, db_conn, login): 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(mode='CMYK'), 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 == 200, await r.text() assert None is not await db_conn.fetchval('SELECT logo FROM companies')
async def test_mark_formdata_as_processed(aiohttp_client: Any) -> None: async def handler(request): return web.Response() app = web.Application() app.add_routes([web.post("/", handler)]) client = await aiohttp_client(app) data = FormData() data.add_field("test", "test_value", content_type="application/json") resp = await client.post("/", data=data) assert len(data._writer._parts) == 1 with pytest.raises(RuntimeError): await client.post("/", data=data) resp.release()
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) resp.close()
async def test_add_image(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() assert None is await db_conn.fetchval('SELECT image FROM donation_options') 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 sorted(dummy_server.app['images']) == [ ( RegexStr( r'/aws_endpoint_url/testingbucket.example.org/tests/testing/supper-clubs/\d+/\w+/main.png' ), 672, 480, ), ( RegexStr( r'/aws_endpoint_url/testingbucket.example.org/tests/testing/supper-clubs/\d+/\w+/thumb.png' ), 400, 200, ), ] assert None is not await db_conn.fetchval( 'SELECT image FROM donation_options') assert sum('DELETE aws_endpoint_url/' in e for e in dummy_server.app['log']) == 0
async def create_request( last_name: str = Form(...), first_name: str = Form(...), phone_number: str = Form(...), email: str = Form(...), photo: UploadFile = File(...), ): uuid = await get_user_uuid(email, first_name, last_name) async with aiohttp.ClientSession() as session: async with session.post( f'{itsm_base}/create-m2m/serviceCall$complaint?accessKey={access_key}', json={ 'metaClass': 'serviceCall$complaint', 'client': uuid, 'service': 'slmService$3269602', 'agreement': 'agreement$605301', 'shortDescr': 'Жалоба на незаконный спил', 'userName': f'{last_name} {first_name}', 'phoneNumber': phone_number, 'userMail': email, }, ) as resp: res = await resp.json() if resp.status == HTTPStatus.CREATED: uuid = res["UUID"] photo_bytes = await photo.read() async with aiohttp.ClientSession() as session: data = FormData() data.add_field('photo', photo_bytes, filename="photo.png", content_type="multipart/form-data") async with session.post( f"{add_photo_url}/{uuid}?accessKey={access_key}&attrCode=photo", data=data) as resp: pass print(resp.status) else: print(resp.status) return "Произошла ошибка"
async def async_send_request(url, filename): headers = { "X-Requested-With": "XMLHttpRequest", "Accept": "application/json", "Authorization": "Bearer " + access_token } data = FormData() data.add_field('file', open(filename, 'rb'), filename=filename, 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())
async def forward_form_data(self, forward_url, payload): try: temp = payload['files'] file = temp.file filename = temp.filename content_type = temp.content_type name = temp.name data = FormData() data.add_field(name, file, filename=filename, content_type=content_type) async with aiohttp.ClientSession() as session: async with session.post(forward_url, data=data) as response: return await self.process_response(response) except Exception as error: self.logger.error(error) return return_message(status=HTTP_STATUS_UNPROCESSABLE_ENTITY, message=ERROR_FORWARD_MESSAGE)
async def sendFile(session, url, apiKey, action, fileName, tempFilePath): headers = {'X-Api-Key': apiKey} filenameWithId = addUniqueIdToFile(fileName) data = {} if (action == actions.COMMAND_LOAD): data = FormData() data.add_field('file', open(tempFilePath, 'rb'), filename=filenameWithId, content_type='application/octet-stream') async with session.post(url, headers=headers, data=data) as response: await response.text() data = {'command': 'select'} async with session.post(url + '/' + filenameWithId, headers=headers, json=data) as responseCommand: return await responseCommand.read(), responseCommand.status
async def upload_file(url, username, password, responses): mpw = MultipartWriter() fd = FormData() fd.add_field('username', username) fd.add_field('password', password) async with ClientSession() as session: try: async with session.post(url, data=fd) as response: response = await response.read() response_str = response.decode('utf-8') responses[response_str] = responses.get(response_str, 0) + 1 except ClientConnectionError: responses['CONNECTION_ERR'] = responses.get('CONNECTION_ERR', 0) + 1 except ClientPayloadError: responses['PAYLOAD_ERR'] = responses.get('PAYLOAD_ERR', 0) + 1 except ClientResponseError: responses['RESPONSE_ERR'] = responses.get('RESPONSE_ERR', 0) + 1
async def _post_file_image( self, file_path: str, cid: str, content: str = '', at_user: Optional[str] = None) -> Dict[str, Any]: url_path = Channel.SEND_IMAGE.format(channelId=cid) file_info = FileIO.get_file_info(file_path) data = FormData() data.add_field('files', await FileIO.read_image(file_path), content_type=file_info['content_type'], filename=file_info['name']) payload_json = { 'nonce': No.IDENT, 'content': Ms.at(at_user) + content if at_user else content } data.add_field('payload_json', json.dumps(payload_json)) return await self._post_data(url_path, data=data)
async def test_upload_image(cli, url, factory: Factory, login, dummy_server): await factory.create_company() await factory.create_user() await factory.create_cat() await login() data = FormData() data.add_field('image', create_image(), 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 == 200, await r.text() assert sorted(dummy_server.app['log']) == [ RegexStr(r'PUT aws_endpoint_url/testingbucket.example.org/tests/testing/supper-clubs/option/\w+/main.png'), RegexStr(r'PUT aws_endpoint_url/testingbucket.example.org/tests/testing/supper-clubs/option/\w+/thumb.png'), ]