Skip to content

Commit d25bcb8

Browse files
Enhance README examples
1 parent 708f61c commit d25bcb8

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ py -m pip install urlpattern
2929

3030
## Usage
3131

32-
Check [urlpattern.pyi](https://github.com/urlpattern/python-urlpattern/blob/main/urlpattern.pyi).
32+
Check [urlpattern.pyi](https://github.com/urlpattern/python-urlpattern/blob/main/urlpattern.pyi) or the examples below.
3333

34-
## Examples
34+
For various usage examples, you can also check [Chrome for Developers](https://developer.chrome.com/docs/web-platform/urlpattern) or [MDN](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API) (you need to convert JavaScript into Python).
3535

36-
For various usage examples, refer to [Chrome for Developers](https://developer.chrome.com/docs/web-platform/urlpattern) or [MDN](https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API) (you may need to convert JavaScript into Python).
36+
## Examples
3737

3838
### `test`
3939

@@ -72,20 +72,33 @@ print(pattern.test("https://example.com/TeST")) # output: True
7272
### Simple WSGI app
7373

7474
```py
75-
from urlpattern import URLPattern
7675
from wsgiref.simple_server import make_server
7776

77+
from urlpattern import URLPattern
7878

7979
user_id_pattern = URLPattern({"pathname": "/users/:id"})
8080

8181

82+
def get_user_id(environ, start_response):
83+
user_id = environ["result"]["pathname"]["groups"]["id"]
84+
status = "200 OK"
85+
response_headers = [("Content-type", "text/plain; charset=utf-8")]
86+
start_response(status, response_headers)
87+
return [f"{user_id=}".encode()]
88+
89+
8290
def app(environ, start_response):
8391
path = environ["PATH_INFO"]
92+
method = environ["REQUEST_METHOD"]
8493

8594
if result := user_id_pattern.exec({"pathname": path}):
86-
user_id = result["pathname"]["groups"]["id"]
87-
start_response("200 OK", [("Content-Type", "text/plain; charset=utf-8")])
88-
return [f"{user_id=}".encode()]
95+
if method == "GET":
96+
return get_user_id(environ | {"result": result}, start_response)
97+
98+
status = "404 Not Found"
99+
response_headers = [("Content-type", "text/plain; charset=utf-8")]
100+
start_response(status, response_headers)
101+
return [b"Not Found"]
89102

90103

91104
with make_server("", 8000, app) as httpd:
@@ -96,4 +109,4 @@ with make_server("", 8000, app) as httpd:
96109

97110
Due to limitations in the dependency [denoland/rust-urlpattern](https://github.com/denoland/rust-urlpattern), it may not support all features specified in [the standard](https://urlpattern.spec.whatwg.org/).
98111

99-
Check the `pytest.skip` in [`tests/test_lib.py`](https://github.com/urlpattern/python-urlpattern/blob/main/tests/test_lib.py).
112+
Check `pytest.skip` in [`tests/test_lib.py`](https://github.com/urlpattern/python-urlpattern/blob/main/tests/test_lib.py).

0 commit comments

Comments
 (0)