Skip to content

Commit 32c01b7

Browse files
committed
version 1.9.2 Improving details of ASGI response
1 parent 0053cc1 commit 32c01b7

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed

README.es.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pip install bicchiere
1414

1515
## [Aplicación de demostración del proyecto](https://bicchiere.sytes.net)
1616

17-
Versión actual: 1.9.1
17+
Versión actual: 1.9.2
1818

1919
<p>
2020
<a href="https://pypi.python.org/pypi/bicchiere" target="_blank" rel="nofollow"><img alt="GitHub tag (latest by date)" src="https://img.shields.io/github/v/tag/sandy98/bicchiere?color=%230cc000&label=bicchiere"></a>

README.it.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pip install bicchiere
1414

1515
## [Applicazione Demo Progetto](https://bicchiere.sytes.net)
1616

17-
Versione corrente: 1.9.1
17+
Versione corrente: 1.9.2
1818

1919
<p>
2020
<a href="https://pypi.python.org/pypi/bicchiere" target="_blank" rel="nofollow"><img alt="GitHub tag (latest by date)" src="https://img.shields.io/github/v/tag/sandy98/bicchiere?color=%230cc000&label=bicchiere"></a>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pip install bicchiere
1515

1616
## [Project Demo App](https://bicchiere.sytes.net)
1717

18-
Current version: 1.9.1
18+
Current version: 1.9.2
1919

2020
<p>
2121
<a href="https://pypi.python.org/pypi/bicchiere" target="_blank" rel="nofollow"><img alt="GitHub tag (latest by date)" src="https://img.shields.io/github/v/tag/sandy98/bicchiere?color=%230cc000&label=bicchiere"></a>

bicchiere.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ def save(self) -> str:
15251525
class BicchiereMiddleware:
15261526
"Base class for everything Bicchiere"
15271527

1528-
__version__ = (1, 9, 1)
1528+
__version__ = (1, 9, 2)
15291529
__author__ = "Domingo E. Savoretti"
15301530
config = default_config
15311531
template_filters = {}
@@ -3280,6 +3280,18 @@ def demo_app(cls):
32803280
menu.addItem(dropdown)
32813281
menu.addItem(MenuItem("About", "/about"))
32823282

3283+
3284+
@app.get("/demo/hello/<name>")
3285+
@app.get("/demo/hello")
3286+
#@app.plain_content()
3287+
async def hello(name="Straniero"):
3288+
return f"""
3289+
<h1>Ciao, {name.title()}!</h1>
3290+
<p>Bicchiere ti saluta.</p>
3291+
<p></p>
3292+
<p>Bicchiere v{app.version} in modo {'ASGI' if app.is_asgi(app) else 'WSGI'}</p>
3293+
"""
3294+
32833295
@app.get("/cgi")
32843296
def cgi():
32853297
retval = """
@@ -4341,31 +4353,26 @@ async def __call__(self, scope, receive, send):
43414353
raise HTTPException(f"Received a message type({self.msgtype}) this app can't handle.")
43424354

43434355
async def diamocidafare(status_msg, response):
4344-
status = int(status_msg.split(" ", 1)[0])
4356+
status = int(Bicchiere.tobytes(status_msg).split(b" ", 1)[0])
43454357
body = await self._send_response(status_msg, response)
43464358
contents = dict(type='http.response.body', body=self.tobytes(body), more_body=False)
43474359
try:
4348-
await self.send(dict(type="http.response.start", status=status, headers=self.headers.items()))
4360+
bheaders = [(Bicchiere.tobytes(k), Bicchiere.tobytes(v)) for k, v in self.headers.items()]
4361+
await self.send(dict(type="http.response.start", status=status, headers=bheaders))
43494362
self.headers_sent = True
4350-
except:
4351-
pass
4363+
except Exception as exc:
4364+
self.logger.error(f"Error at 'diamocidafare': {repr(exc)}")
43524365
try:
43534366
sentcontents = await self.send(contents)
43544367
self.headers_sent = False
4355-
except:
4368+
except Exception as exc:
4369+
self.logger.error(f"Error at 'diamocidafare': {repr(exc)}")
43564370
sentcontents = None
43574371
finally:
43584372
return sentcontents
43594373

43604374
if self.msgtype in ["http", "https"]:
43614375

4362-
if self.wsgi_app and self.is_wsgi(self.wsgi_app):
4363-
try:
4364-
asgi_app = WsgiToAsgi(self.wsgi_app)
4365-
return await asgi_app(self.scope, self.receive, self.send)
4366-
except Exception as exc:
4367-
self.logger.error(f"Exeption while trying to convert WSGI app to ASGI: {repr(exc)}")
4368-
43694376
status_msg, response = self._try_static()
43704377
if status_msg and response:
43714378
self.logger.info(f"Proceeding from _try_static with status: {status_msg}")
@@ -4392,6 +4399,15 @@ async def diamocidafare(status_msg, response):
43924399
self.logger.info(f"Proceeding from _try_mount with status: {status_msg}")
43934400
return await diamocidafare(status_msg, response)
43944401

4402+
if self.wsgi_app and self.is_wsgi(self.wsgi_app):
4403+
try:
4404+
asgi_app = WsgiToAsgi(self.wsgi_app)
4405+
return await asgi_app(self.scope, self.receive, self.send)
4406+
except Exception as exc:
4407+
self.logger.error(f"Exeption while trying to convert WSGI app to ASGI: {repr(exc)}")
4408+
err = b"500 Server Error"
4409+
return await diamocidafare(err, err)
4410+
43954411
status_msg, response = self._abort(404, self.environ.get('PATH_INFO'), " not found AT ALL.")
43964412
self.logger.info(f"Resource {self.full_path} not found.\n")
43974413
status = int(status_msg.split(" ", 1)[0])
@@ -4401,6 +4417,7 @@ async def diamocidafare(status_msg, response):
44014417
await self.send(dict(type="http.response.start", status=status, headers=[('Content-Type', 'text/html; charset=utf-8')]))
44024418
self.headers_sent = True
44034419
return await self.send(contents)
4420+
44044421
elif self.msgtype in ["websocket"]:
44054422
self.logger.debug("Received message type 'websocket'.\nThis has been processed elsewhere and thus, this message should never appear.")
44064423
else:
@@ -4464,12 +4481,15 @@ async def showscope():
44644481
@app.get("/demo/hello/<name>")
44654482
@app.get("/demo/hello")
44664483
#@app.plain_content()
4467-
def hello(name="Straniero"):
4468-
return f"<h1>Ciao, {name.title()}!</h1>\n<p>Bicchiere ti saluta.</p>"
4469-
4470-
# app.mount("/", app.wsgi_app)
4484+
async def hello(name="Straniero"):
4485+
return f"""
4486+
<h1>Ciao, {name.title()}!</h1>
4487+
<p>Bicchiere ti saluta.</p>
4488+
<p></p>
4489+
<p>Bicchiere v{app.version} in modo {'ASGI' if app.is_asgi(app) else 'WSGI'}</p>
4490+
"""
44714491

4472-
@app.get("demo/f42")
4492+
@app.get("/demo/f42")
44734493
async def myf42():
44744494
return await app.redirect("/factorial/42")
44754495

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "bicchiere"
9-
version = "1.9.1"
9+
version = "1.9.2"
1010
description = "Yet another python web (WSGI) micro-framework"
1111
readme = "README.md"
1212
authors = [{ name = "Domingo E Savoretti", email = "esavoretti@gmail.com" }]

0 commit comments

Comments
 (0)