async def fetch_content(url, session): async with aiofiles.open("input.docx", mode='rb') as file: data = FormData() data.add_field('file', file, filename='_.docx') async with session.post(url, data=data) as response: data = await response.read() await write_image(data)
def function398(function1105, function604): var3042 = FormData(quote_fields=False, charset='ascii') var3042.add_field('emails[]', '*****@*****.**', content_type='multipart/form-data') var4723 = var3042() yield from var4723.function2007(function604) assert (b'name="emails[]"' in function1105)
def test_invalid_formdata_content_transfer_encoding() -> None: form = FormData() invalid_vals = [0, 0.1, {}, [], b'foo'] for invalid_val in invalid_vals: with pytest.raises(TypeError): form.add_field('foo', 'bar', content_transfer_encoding=invalid_val)
def function1429(function1105, function604): var4060 = FormData(charset='ascii') var4060.add_field('emails[]', '*****@*****.**', content_type='multipart/form-data') var3464 = var4060() yield from var3464.function2007(function604) assert (b'name="emails%5B%5D"' in function1105)
async def process_partition( loop: asyncio.BaseEventLoop, results_queue: asyncio.Queue, server_address: URL, http: aiohttp.ClientSession, partition: PointsPartition, mission_template: Template, mission_loader: str, mission_name: str, width: int, scale: int, ) -> Awaitable[None]: LOG.debug( f"query range [{partition.start}:{partition.end}] on server " f"{server_address}" ) file_name = f"{mission_name}_{partition.start}_{partition.end}.mis" missions_url = server_address / "missions" mission_dir_url = missions_url / "heightmap" mission_url = mission_dir_url / file_name points = ( index_to_point(i, width, scale) for i in range(partition.start, partition.end + 1) ) mission = mission_template.render( loader=mission_loader, points=points, ) data = FormData() data.add_field( 'mission', mission.encode(), filename=file_name, content_type='plain/text', ) await http.post(mission_dir_url, data=data) await http.post(mission_url / "load") await http.post(missions_url / "current" / "begin") async with http.get(server_address / "radar" / "stationary-objects") as response: data = await response.json() data = [ pack(HEIGHT_PACK_FORMAT, int(point['pos']['z'])) for point in data ] data = b''.join(data) await http.post(missions_url / "current" / "unload") await http.delete(mission_url) await results_queue.put((partition, data))
async def request(self, method, url, data=None, headers=None, files=None, timeout=5, timeout_retry=3, decode_json=True, return_binary=False, **kwargs): for _ in range(timeout_retry): try: post_data = data if files is None: if isinstance(data, dict) or isinstance(data, list): post_data = json.dumps(data, ensure_ascii=False) else: post_data = FormData() if data: for k, v in data.items(): post_data.add_field(k, v) file_args, fileobs = convert_file_args(files) for name, (filename, fileobj, mimetype) in file_args.items(): post_data.add_field(name, fileobj, content_type=mimetype, filename=filename) func = getattr(self.session, method) async with func(url, data=post_data, headers=headers, timeout=ClientTimeout(timeout), **kwargs) as resp: if resp.status != 200: self.logger.error( "[%s] url[%s], data[%s] headers[%s] kwargs[%s] failed,"\ " code[%d], resp[%s]", method, url, post_data, headers, kwargs, resp.status, resp) return None if return_binary: result = await resp.content.read() else: result = await resp.text(encoding='UTF-8') if decode_json: result = json.loads(result) return result except asyncio.TimeoutError: self.logger.warning( "[%s] url[%s], data[%s] headers[%s] kwargs[%s] timeout", method, url, data, headers, kwargs) else: self.logger.error("[%s] url[%s], timeout after retry [%d] times", method, url, timeout_retry)
async def http(session, domain, *args, **kwargs): """ TODO 补充一下参数和返回值格式说明 http请求处理器 :param session: ClientSession对象 :param domain: 服务地址 :param args: :param kwargs: :return: """ method, api = args arguments = kwargs.get('data') or kwargs.get('params') or kwargs.get( 'json') or {} # kwargs中加入token # TODO 实现getheader 通用方法 # 已经实现了 # kwargs.setdefault('headers', {}).update({'token': bxmat.token}) contentType = kwargs.setdefault('headers', {}).setdefault('Content-Type', '') #TODO 补充文件上传的处理 if contentType.startswith('multipart/'): formData = FormData() data = kwargs.get('data') or {} if isinstance(data, dict): for k, v in data.items(): if os.path.isfile(v): formData.add_field(k, await aiofiles.open(v, 'rb'), filename=os.path.basename(v)) # content_type=contentType) else: formData.add_field(k, str(v)) # print('111') pass kwargs['data'] = formData # 拼接服务地址和api url = ''.join([domain, api]) # print(kwargs['data'].__dict__) # print() # async with ClientSession() as session: # async with session.request(method, url, **kwargs) as response: # res = await response_handler(response) # return { # 'response': res, # 'url': url, # 'arguments': arguments # } # async with ClientSession() as session: async with session.request(method, url, **kwargs) as response: res = await response_handler(response) return {'response': res, 'url': url, 'arguments': arguments}
async def post(): async with aiohttp.ClientSession() as session: # 表单模式(转码) payload = {'key1': 'value1', 'key2': 'value2'} # 表单模式(不转码) payload = json.dumps(payload) # 上传小文件 # files = {'file': open('test.py', 'rb')} # 设置文件名称 data = FormData() data.add_field('file', open('test.py', 'rb'), filename='test.py', content_type='application/vnd.ms-excel') async with session.post('http://httpbin.org/post', data=data) as resp: print(resp.status) print(await resp.text()) print(resp.url)
async def upload_file(path, existing_id=""): if not path.exists(): return with load_data_file(path.parent) as parent_data_file: parent_id = parent_data_file["id"] if parent_id is None: return with path.open("rb") as file: data = FormData(fields=[("existing_id", existing_id), ("product_id", str(parent_id)), ("token", utils.get_login_token()), ("user_id", str(utils.get_user_id()))], quote_fields=False) data.add_field("file", file, filename=path.name) resp = await (await utils.upload_file( constants.SERVER_HOST + "/_back/product-sync-upload", data)).json() with load_data_file(path) as data: data["id"] = resp.get("id", data.get("id", None)) data["type"] = resp.get("type", data.get("type", None)) data["name"] = path.name
def test_invalid_formdata_filename(): form = FormData() invalid_vals = [0, 0.1, {}, [], b'foo'] for invalid_val in invalid_vals: with pytest.raises(TypeError): form.add_field('foo', 'bar', filename=invalid_val)
def test_invalid_formdata_payload(): form = FormData() form.add_field('test', object(), filename='test.txt') with pytest.raises(TypeError): form()
async def test_aiohttp(self): for method, path, data in ( ("GET", "/request/", []), ("GET", "/request/bar/bar?a=2", []), ("POST", "/request/some/random/path", { "a": "2" }), ): parsed = urlparse(path) resp = await self.client.request(method, path, data=data or None) self.assertEqual(resp.status, 200) self.assertEqual( await resp.json(), [method, "http", "127.0.0.1", parsed.path, parsed.query, data], ) for cached in (True, False): root = cached and "/cached" or "/app" resp1 = await self.client.request("GET", root + "/random.wl") resp2 = await self.client.request("GET", root + "/random.wl") self.assertIsInstance(float(await resp1.text()), float) (cached and self.assertEqual or self.assertNotEqual)(await resp1.text(), await resp2.text()) for loc, content in ( ("", '"Hello from / in a folder!"'), ("/", '"Hello from / in a folder!"'), ("/index.wl", '"Hello from / in a folder!"'), ("/foo", '"Hello from foo"'), ("/foo/", '"Hello from foo"'), ("/foo/index.wl", '"Hello from foo"'), ("/foo/bar", '"Hello from foo/bar"'), ("/foo/bar", '"Hello from foo/bar"'), ("/foo/bar/index.wl", '"Hello from foo/bar"'), ("/foo/bar/something.wl", '"Hello from foo/bar/something"'), ): resp = await self.client.request("GET", root + loc) self.assertEqual(resp.status, 200) self.assertEqual(await resp.text(), content) for loc in ("/some-random-url", "/404", "/some-/nonsense"): resp = await self.client.request("GET", root + loc) self.assertEqual(resp.status, 404) for fmt in ("wxf", "mx", "m", "wl", "json"): resp = await self.client.request("GET", root + "some." + fmt) self.assertEqual(resp.status, 200) self.assertEqual(len(await resp.json()), 4) self.assertEqual((await resp.json())[0:3], ["hello", "from", fmt.upper()]) self.assertIsInstance((await resp.json())[-1], int) self.assertEqual(resp.headers["Content-Type"], "application/json") resp = await self.client.request("GET", "/") self.assertEqual(resp.status, 200) self.assertEqual(await resp.text(), "Hello from aiohttp") for root in ("", "/app"): resp = await self.client.request("GET", root + "/api") self.assertEqual(resp.status, 400) self.assertEqual((await resp.json())["Success"], False) self.assertEqual(resp.headers["Content-Type"], "application/json") resp = await self.client.request("GET", root + "/api?x=a") self.assertEqual(resp.status, 200) self.assertEqual((await resp.json())["x"], "a") self.assertEqual(resp.headers["Content-Type"], "application/json") resp = await self.client.request("GET", root + "/form") self.assertEqual(resp.status, 200) self.assertEqual(first(resp.headers["Content-Type"].split(";")), "text/html") resp = await self.client.request("POST", root + "/form", data={"x": "foobar"}) self.assertEqual(resp.status, 200) self.assertEqual((await resp.json())["x"], "foobar") self.assertEqual(resp.headers["Content-Type"], "application/json") data = FormData() data.add_field("x", b"foobar", filename="somefile.txt") resp = await self.client.request("POST", root + "form", data=data) self.assertEqual(resp.status, 200) self.assertEqual((await resp.json())["x"], "foobar") self.assertEqual(resp.headers["Content-Type"], "application/json")
def test_invalid_formdata_filename() -> None: form = FormData() invalid_vals = [0, 0.1, {}, [], b'foo'] for invalid_val in invalid_vals: with pytest.raises(TypeError): form.add_field('foo', 'bar', filename=invalid_val)
def test_formdata_field_name_is_quoted(buf, writer): form = FormData(charset="ascii") form.add_field("emails[]", "*****@*****.**", content_type="multipart/form-data") payload = form() yield from payload.write(writer) assert b'name="emails%5B%5D"' in buf
def test_formdata_multipart(buf, writer) -> None: form = FormData() assert not form.is_multipart form.add_field('test', b'test', filename='test.txt') assert form.is_multipart
def test_invalid_formdata_payload() -> None: form = FormData() form.add_field('test', object(), filename='test.txt') with pytest.raises(TypeError): form()
def test_invalid_formdata_content_type(): form = FormData() invalid_vals = [0, 0.1, {}, [], b'foo'] for invalid_val in invalid_vals: with pytest.raises(TypeError): form.add_field('foo', 'bar', content_type=invalid_val)
def function1541(): var670 = FormData() var676 = [0, 0.1, {}, [], b'foo'] for var103 in var676: with pytest.raises(TypeError): var670.add_field('foo', 'bar', content_transfer_encoding=var103)
def function2237(): var938 = FormData() var3721 = [0, 0.1, {}, [], b'foo'] for var1022 in var3721: with pytest.raises(TypeError): var938.add_field('foo', 'bar', filename=var1022)
def function615(): var1644 = FormData() var1564 = [0, 0.1, {}, [], b'foo'] for var155 in var1564: with pytest.raises(TypeError): var1644.add_field('foo', 'bar', content_type=var155)
def function617(): var3234 = FormData() var3234.add_field('test', object(), filename='test.txt') with pytest.raises(TypeError): var3234()
def function2016(function1105, function604): var3393 = FormData() assert (not var3393.is_multipart) var3393.add_field('test', b'test', filename='test.txt') assert var3393.is_multipart
def test_invalid_formdata_content_transfer_encoding(): form = FormData() invalid_vals = [0, 0.1, {}, [], b'foo'] for invalid_val in invalid_vals: with pytest.raises(TypeError): form.add_field('foo', 'bar', content_transfer_encoding=invalid_val)
def test_formdata_field_name_is_not_quoted(buf, writer): form = FormData(quote_fields=False, charset="ascii") form.add_field("emails[]", "*****@*****.**", content_type="multipart/form-data") payload = form() yield from payload.write(writer) assert b'name="emails[]"' in buf
def test_formdata_multipart(buf, writer) -> None: form = FormData() assert not form.is_multipart form.add_field('test', b'test', filename='test.txt') assert form.is_multipart
def test_invalid_formdata_content_type() -> None: form = FormData() invalid_vals = [0, 0.1, {}, [], b'foo'] for invalid_val in invalid_vals: with pytest.raises(TypeError): form.add_field('foo', 'bar', content_type=invalid_val)
async def test_formdata_field_name_is_quoted(buf, writer) -> None: form = FormData(charset="ascii") form.add_field("emails[]", "*****@*****.**", content_type="multipart/form-data") payload = form() await payload.write(writer) assert b'name="emails%5B%5D"' in buf
def request( self, request_params: MutableMapping[str, Any], operation: Optional[Operation] = None, request_config: Optional[RequestConfig] = None, ) -> HttpFuture: """Sets up the request params for aiohttp and executes the request in the background. :param request_params: request parameters for the http request. :param operation: operation that this http request is for. Defaults to None - in which case, we're obviously just retrieving a Swagger Spec. :param request_config:RequestConfig request_config: Per-request config that is passed to :class:`bravado.http_future.HttpFuture`. :rtype: :class: `bravado_core.http_future.HttpFuture` """ orig_data = request_params.get("data", {}) if isinstance(orig_data, Mapping): data = FormData() for name, value in orig_data.items(): str_value = ( str(value) if not is_list_like(value) else [str(v) for v in value] ) data.add_field(name, str_value) else: data = orig_data if isinstance(data, FormData): for name, file_tuple in request_params.get("files", {}): stream_obj = file_tuple[1] data.add_field(name, stream_obj, filename=file_tuple[0]) params = self.prepare_params(request_params.get("params")) connect_timeout = request_params.get("connect_timeout") # type: Optional[float] request_timeout = request_params.get("timeout") # type: Optional[float] # mypy thinks the type of total and connect is float, even though it is Optional[float]. Let's ignore the error. timeout = ( aiohttp.ClientTimeout(total=request_timeout, connect=connect_timeout) if (connect_timeout or request_timeout) else None ) follow_redirects = request_params.get("follow_redirects", False) # aiohttp always adds a Content-Type header, and this breaks some servers that don't # expect it for non-POST/PUT requests: https://github.com/aio-libs/aiohttp/issues/457 skip_auto_headers = ( ["Content-Type"] if request_params.get("method") not in ["POST", "PUT"] else None ) coroutine = self.client_session.request( method=request_params.get("method") or "GET", url=cast(str, request_params.get("url", "")), params=params, data=data, headers={ # Convert not string headers to string k: from_bytes(v) if isinstance(v, bytes) else str(v) for k, v in request_params.get("headers", {}).items() }, allow_redirects=follow_redirects, skip_auto_headers=skip_auto_headers, timeout=timeout, **self._get_ssl_params() ) future = self.run_coroutine_func(coroutine, loop=self.loop) return self.bravado_future_class( self.future_adapter(future), self.response_adapter(loop=self.loop), operation, request_config=request_config, )
async def test_formdata_field_name_is_not_quoted(buf, writer) -> None: form = FormData(quote_fields=False, charset="ascii") form.add_field("emails[]", "*****@*****.**", content_type="multipart/form-data") payload = form() await payload.write(writer) assert b'name="emails[]"' in buf