|
126 | 126 | {
|
127 | 127 | "data": {
|
128 | 128 | "text/plain": [
|
129 |
| - "datetime.datetime(2024, 8, 15, 14, 0)" |
| 129 | + "datetime.datetime(2024, 8, 19, 14, 0)" |
130 | 130 | ]
|
131 | 131 | },
|
132 | 132 | "execution_count": null,
|
|
528 | 528 | " if arg.lower()=='auth': return req.scope.get('auth', None)\n",
|
529 | 529 | " if arg.lower()=='htmx': return _get_htmx(req.headers)\n",
|
530 | 530 | " if arg.lower()=='app': return req.scope['app']\n",
|
| 531 | + " if arg.lower()=='body': return (await req.body()).decode()\n", |
531 | 532 | " if arg.lower() in ('hdrs','ftrs','bodykw','htmlkw'): return getattr(req, arg.lower())\n",
|
532 | 533 | " warn(f\"`{arg} has no type annotation and is not a recognised special name, so is ignored.\")\n",
|
533 | 534 | " return None\n",
|
|
857 | 858 | " return to_xml(resp, indent)"
|
858 | 859 | ]
|
859 | 860 | },
|
| 861 | + { |
| 862 | + "cell_type": "code", |
| 863 | + "execution_count": null, |
| 864 | + "id": "a407dc0e", |
| 865 | + "metadata": {}, |
| 866 | + "outputs": [], |
| 867 | + "source": [ |
| 868 | + "#| export\n", |
| 869 | + "def flat_tuple(o):\n", |
| 870 | + " \"Flatten lists\"\n", |
| 871 | + " result = []\n", |
| 872 | + " if not isinstance(o,(tuple,list)): o=[o]\n", |
| 873 | + " o = list(o)\n", |
| 874 | + " for item in o:\n", |
| 875 | + " if isinstance(item, (list,tuple)): result.extend(item)\n", |
| 876 | + " else: result.append(item)\n", |
| 877 | + " return tuple(result)" |
| 878 | + ] |
| 879 | + }, |
860 | 880 | {
|
861 | 881 | "cell_type": "code",
|
862 | 882 | "execution_count": null,
|
|
866 | 886 | "source": [
|
867 | 887 | "#| export\n",
|
868 | 888 | "def _xt_resp(req, resp):\n",
|
869 |
| - " if not isinstance(resp, tuple): resp = (resp,)\n", |
| 889 | + " resp = flat_tuple(resp)\n", |
870 | 890 | " resp = resp + tuple(getattr(req, 'injects', ()))\n",
|
871 | 891 | " http_hdrs,resp = partition(resp, risinstance(HttpHeader))\n",
|
872 | 892 | " http_hdrs = {o.k:str(o.v) for o in http_hdrs}\n",
|
|
1015 | 1035 | {
|
1016 | 1036 | "data": {
|
1017 | 1037 | "text/plain": [
|
1018 |
| - "'091b529e-f0be-4107-a22e-5644344a20d3'" |
| 1038 | + "'a604e4a2-08e8-462d-aff9-15468891fe09'" |
1019 | 1039 | ]
|
1020 | 1040 | },
|
1021 | 1041 | "execution_count": null,
|
|
1065 | 1085 | "def _mk_locfunc(f,p):\n",
|
1066 | 1086 | " class _lf:\n",
|
1067 | 1087 | " def __init__(self): update_wrapper(self, f)\n",
|
1068 |
| - " def __call__(self, **kw): return p + (f'?{urlencode(kw)}' if kw else '')\n", |
| 1088 | + " def __call__(self, *args, **kw): return f(*args, **kw)\n", |
| 1089 | + " def rt(self, **kw): return p + (f'?{urlencode(kw)}' if kw else '')\n", |
1069 | 1090 | " def __str__(self): return p\n",
|
1070 | 1091 | " return _lf()"
|
1071 | 1092 | ]
|
|
1384 | 1405 | "text": [
|
1385 | 1406 | "<!doctype html>\n",
|
1386 | 1407 | "\n",
|
1387 |
| - "<html><div hx-post=\"/yoyo\">Text.</div>\n", |
| 1408 | + "<html><div hx-post=\"a yoyo\">Text.</div>\n", |
1388 | 1409 | "</html>\n",
|
1389 | 1410 | "\n"
|
1390 | 1411 | ]
|
|
1417 | 1438 | ],
|
1418 | 1439 | "source": [
|
1419 | 1440 | "@app.get\n",
|
1420 |
| - "def autopost2(): return Html(Body(Div('Text.', cls='px-2', hx_post=show_host(a='b'))))\n", |
| 1441 | + "def autopost2(): return Html(Body(Div('Text.', cls='px-2', hx_post=show_host.rt(a='b'))))\n", |
1421 | 1442 | "print(cli.get('/autopost2').text)"
|
1422 | 1443 | ]
|
1423 | 1444 | },
|
|
1683 | 1704 | "test_eq(r.headers['mykey'], 'myval')"
|
1684 | 1705 | ]
|
1685 | 1706 | },
|
| 1707 | + { |
| 1708 | + "cell_type": "code", |
| 1709 | + "execution_count": null, |
| 1710 | + "id": "8a91b0af", |
| 1711 | + "metadata": {}, |
| 1712 | + "outputs": [], |
| 1713 | + "source": [ |
| 1714 | + "@rt\n", |
| 1715 | + "def meta(): \n", |
| 1716 | + " return ((Title('hi'),H1('hi')),\n", |
| 1717 | + " (Meta(property='image'), Meta(property='site_name'))\n", |
| 1718 | + " )\n", |
| 1719 | + "\n", |
| 1720 | + "t = cli.post('/meta').text\n", |
| 1721 | + "assert re.search('<body>\\s*<h1>hi</h1>\\s*</body>', t)\n", |
| 1722 | + "assert '<meta' in t" |
| 1723 | + ] |
| 1724 | + }, |
1686 | 1725 | {
|
1687 | 1726 | "cell_type": "code",
|
1688 | 1727 | "execution_count": null,
|
|
1775 | 1814 | "assert \"<title>It worked!</title>\" in response and \"<p>15, Lorem</p>\" in response"
|
1776 | 1815 | ]
|
1777 | 1816 | },
|
| 1817 | + { |
| 1818 | + "cell_type": "code", |
| 1819 | + "execution_count": null, |
| 1820 | + "id": "51cd4643", |
| 1821 | + "metadata": {}, |
| 1822 | + "outputs": [], |
| 1823 | + "source": [ |
| 1824 | + "# Testing POST with Content-Type: application/json\n", |
| 1825 | + "@app.post(\"/bodytext\")\n", |
| 1826 | + "def index(body): return body\n", |
| 1827 | + "\n", |
| 1828 | + "response = cli.post('/bodytext', headers={\"Content-Type\": \"application/json\"}, data=s).text\n", |
| 1829 | + "test_eq(response, '{\"b\": \"Lorem\", \"a\": 15}')" |
| 1830 | + ] |
| 1831 | + }, |
1778 | 1832 | {
|
1779 | 1833 | "cell_type": "code",
|
1780 | 1834 | "execution_count": null,
|
|
1807 | 1861 | {
|
1808 | 1862 | "data": {
|
1809 | 1863 | "text/plain": [
|
1810 |
| - "'Cookie was set at time 21:05:42.196533'" |
| 1864 | + "'Cookie was set at time 10:40:02.914012'" |
1811 | 1865 | ]
|
1812 | 1866 | },
|
1813 | 1867 | "execution_count": null,
|
|
1837 | 1891 | "name": "stdout",
|
1838 | 1892 | "output_type": "stream",
|
1839 | 1893 | "text": [
|
1840 |
| - "Set to 2024-08-15 21:05:42.225287\n" |
| 1894 | + "Set to 2024-08-19 10:40:03.311880\n" |
1841 | 1895 | ]
|
1842 | 1896 | },
|
1843 | 1897 | {
|
1844 | 1898 | "data": {
|
1845 | 1899 | "text/plain": [
|
1846 |
| - "'Session time: 2024-08-15 21:05:42.225287'" |
| 1900 | + "'Session time: 2024-08-19 10:40:03.311880'" |
1847 | 1901 | ]
|
1848 | 1902 | },
|
1849 | 1903 | "execution_count": null,
|
|
2032 | 2086 | "#|hide\n",
|
2033 | 2087 | "import nbdev; nbdev.nbdev_export()"
|
2034 | 2088 | ]
|
| 2089 | + }, |
| 2090 | + { |
| 2091 | + "cell_type": "code", |
| 2092 | + "execution_count": null, |
| 2093 | + "id": "2af5e721", |
| 2094 | + "metadata": {}, |
| 2095 | + "outputs": [], |
| 2096 | + "source": [] |
2035 | 2097 | }
|
2036 | 2098 | ],
|
2037 | 2099 | "metadata": {
|
|
0 commit comments