Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does endpoint mean in flask-restful

I defined some resource called WorkerAPI using flask-restful and the plan is to process POST request from /api/workers/new and GET request from /api/workers/. When using the following code

api.add_resource(WorkerAPI, '/api/workers/new')
api.add_resource(WorkerAPI, '/api/workers/')

I get errors:

AssertionError: View function mapping is overwriting an existing endpoint function: workerapi

Then I tried to use the following, which seems to work, although I don't know why it works.

api.add_resource(WorkerAPI, '/api/workers/new', endpoint='/workers/new')
api.add_resource(WorkerAPI, '/api/workers/', endpoint='/workers/')

It looks like redundant information to me though. It seems the site works as long as the two endpoints are defined as different strings. What does endpoint mean here?

like image 922
nos Avatar asked Jul 14 '17 04:07

nos


People also ask

What is an endpoint in API?

An API endpoint is a point at which an API -- the code that allows two software programs to communicate with each other -- connects with the software program. APIs work by sending requests for information from a web application or web server and receiving a response.

What is endpoint in Python?

Endpoints Frameworks for Python is integrated with the App Engine standard Python 2.7 runtime environment. Endpoints Frameworks consists of tools, libraries, and capabilities that let you generate APIs and client libraries from an App Engine application.

How do I create an endpoint in Python API?

To create an API in Python with Flask, we have to indicate: the endpoint, the method and the function that should be executed on that endpoint. Let's see an example with an API that simply returns the text “Hello world!”. from flask import Flask, jsonify, request,send_file app = Flask() @app.


2 Answers

The thing is that the add_resource function registers the routes with the framework using the given endpoint. If an endpoint isn't given then Flask-RESTful generates one for you from the class name.

Your case is WorkerAPI, the endpoint will beworkerapi for these two methods, better make endpoint explicit and avoid to have conflicting endpoint names registered.

For what's endpoint, you can refer to this answer for more details.

like image 87
Tiny.D Avatar answered Oct 22 '22 18:10

Tiny.D


'endpoint' is actually an identifier that is used in determining what logical unit of your code should handle a specific request.

So, an URL path is mapped to an endpoint and then an endpoint is mapped to a view function in your flask app.

In your case, when you are explicitly defining your endpoints for your resources, it helps the flask app internally to map it efficiently to a list of routes, while when your are not explicitly defining it, flask takes some default value, as explained by @Tiny.D, in the above answer.

like image 43
hansrajswapnil Avatar answered Oct 22 '22 19:10

hansrajswapnil