liveBook · Manning

Endpoint

As explained in Python Concurrency with asyncio

An endpoint is a network-accessible address or interface through which a service exposes a resource or operation and handles requests and responses.

An endpoint is a network-accessible address where a service receives requests and returns responses. In REST, endpoints commonly represent resources such as customer collections, individual customers, or sub-resources. section 9.1.1

A REST API can expose related endpoints at different levels:

  • customers returns a customer collection, potentially paginated. section 9.1.1
  • customers/1 identifies one customer by its integer ID. section 9.1.1
  • customers/1/favorites addresses a customer's favorite-products sub-resource. section 9.1.1

A time endpoint

With aiohttp, @routes.get('/time') connects an HTTP GET request to a coroutine. The coroutine builds month, day, and time data, then returns a JSON response with status 200 and an application/json content type. Running the application makes the endpoint available at localhost:8080/time for a browser or cURL. section 9.1.2

Listing 9.1 The current time endpoint
from aiohttp import web
from datetime import datetime
from aiohttp.web_request import Request
from aiohttp.web_response import Response
 
routes = web.RouteTableDef()
 
 
@routes.get('/time')                    #1
async def time(request: Request) -> Response:
    today = datetime.today()
 
    result = {
        'month': today.month,
        'day': today.day,
        'time': str(today.time())
    }
 
    return web.json_response(result)    #2
 
 
app = web.Application()                 #3
app.add_routes(routes)
web.run_app(app)

Use the runnable listing to see route declaration, request handling, JSON serialization, application registration, and server startup together. section 9.1.2

How Python Concurrency with asyncio covers it

How deep it goes
Explains resource paths and implements one GET route, while deferring the full details of REST.
Examples
Uses customer collection, singleton, and nested favorites paths, plus an aiohttp time endpoint tested with cURL.
What it assumes
Assumes basic HTTP and API familiarity while introducing routes, resource paths, JSON responses, and application startup.
What you can do after
Build it from scratch with aiohttp by declaring a route, handling its request, returning JSON, and starting the server.
How it connects
Connects endpoints with REST, resources, HTTP methods, routes, JSON responses, and web applications.

Also explained in

Compare how 26 books explain endpoint →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.