From 70cd5161552165383ce69c213b32fb76214b93b9 Mon Sep 17 00:00:00 2001 From: Jay Marcyes Date: Sun, 8 Dec 2024 10:51:21 -0700 Subject: [PATCH] README edits --- README.md | 54 +++++---------------------------------------- docs/CONTROLLERS.md | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 82e1c52..dd9196b 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,9 @@ # Endpoints -Quickest API builder in the West! - _Endpoints_ is a lightweight REST api framework written in python that supports both WSGI and ASGI. _Endpoints_ has been used in multiple production systems that handle millions of requests daily. -## 5 Minute Getting Started +## Getting Started ### Installation @@ -24,7 +22,7 @@ Create a controller file with the following command: $ touch controllers.py -Add the following code to the `controllers.py` file (These controller classes are just to help you get started and understand how endpoints works). +Add the following code to the `controllers.py` file: ```python from endpoints import Controller @@ -59,7 +57,8 @@ Install [Daphne](https://github.com/django/daphne): And start it: - $ ENDPOINTS_PREFIX=controllers daphne -b localhost -p 8000 -v 3 endpoints.interface.asgi:ApplicationFactory + $ ENDPOINTS_PREFIX=controllers daphne -b localhost -p 8000 -v 3 endpoints.interface.asgi:Application.factory + ### Test it out @@ -72,13 +71,13 @@ Using curl: $ curl http://localhost:8000/ -d "name=Awesome you" "hello Awesome you" -That's it. Easy peasy! +That's it! In the ***first request*** (`/`), the `controllers` module was accessed, then the `Default` class, and then the `GET` method. In the ***second request*** (`/foo`), the `controllers` module was accessed, then the `Foo` class as specified in the path of the url, and then the `GET` method. -Finally, in the ***last request***, the `controllers` module was accessed, then the `Default` class, and finally the `POST` method with the passed in argument as JSON. +Finally, in the ***last request***, the `controllers` module was accessed, then the `Default` class, and finally the `POST` method with the passed in argument. ## How does it work? @@ -98,47 +97,6 @@ Once the module is found, endpoints will then attempt to find the class with the This makes it easy to bundle your controllers into a `controllers` package/module. -Below are some examples of HTTP requests and how they would be interpreted using endpoints. - -**Note:** prefix refers to the name of the base module that you set. - -|HTTP Request | Path Followed | -|---------------------------------------|---------------------------------- | -|GET / | prefix.Default.GET() | -|GET /foo | prefix.foo.Default.GET() | -|POST /foo/bar | prefix.foo.Bar.POST() | -|GET /foo/bar/che | prefix.foo.Bar.GET(che) | -|GET /foo/bar/che?baz=foo | prefix.foo.Bar.GET(che, baz=foo) | -|POST /foo/bar/che with body: baz=foo | prefix.foo.Bar.POST(che, baz=foo) | - - -### One more example - -Let's say your site had the following setup: - - site/controllers/__init__.py - -and the file `controllers/__init__.py` contained: - -```python -from endpoints import Controller - -class Default(Controller): - async def GET(self): - return "called /" - -class Foo(Controller): - async def GET(self): - return "called /foo" -``` - -then your call requests would be translated like this: - -|HTTP Request | Path Followed | -|-------------- | ------------------------- | -|GET / | controllers.Default.GET() | -|GET /foo | controllers.Foo.GET() | - ## Learn more about Endpoints diff --git a/docs/CONTROLLERS.md b/docs/CONTROLLERS.md index aea991e..ee9ccb6 100644 --- a/docs/CONTROLLERS.md +++ b/docs/CONTROLLERS.md @@ -188,3 +188,54 @@ class Default(Controller): ``` Endpoints first checks module paths, then it checks classes in the found module, then defaults to the `Default` class if no other suitable class is found. + + +## Another Example + +Imagine a folder structure like this: + +``` +controllers/ + __init__.py + foo.py +``` + +With `controllers/__init__.py` having content: + +```python +from endpoints import Controller + +class Default(Controller): + async def GET(self, *args, **kwargs): + pass +``` + +And `controllers/foo.py` having content: + +```python +from endpoints import Controller + +class Default(Controller): + async def GET(self, *args, **kwargs): + pass + +class Bar(Controller): + async def GET(self, *args, **kwargs): + pass + + async def POST(self, *args, **kwargs): + pass +``` + + +Below are how the HTTP requests would be interpreted using endpoints using `controllers` as the prefix. + + +|HTTP Request | Path Followed | +|---------------------------------------|--------------------------------------- | +|GET / | controllers.Default.GET() | +|GET /foo | controllers.foo.Default.GET() | +|POST /foo/bar | controllers.foo.Bar.POST() | +|GET /foo/bar/che | controllers.foo.Bar.GET(che) | +|GET /foo/bar/che?baz=foo | controllers.foo.Bar.GET(che, baz=foo) | +|POST /foo/bar/che with body: baz=foo | controllers.foo.Bar.POST(che, baz=foo) |