Abrir Acceso
Métodos para compartir bases de datos online, procedimientos para la configuración de APIS.
¿Qué es una RESTAPI?
RESTAPI, or RESTful API, is a type of web service that follows the architectural principles of Representational State Transfer (REST). REST is an architectural style that defines a set of constraints and guidelines for building web services.
A RESTful API is a type of API that is built using the REST architectural style. It provides a standardized interface for communication between different software systems, and allows for the creation of interoperable web services that can be accessed by a wide range of clients, including web browsers, mobile devices, and other systems.
RESTful APIs typically use HTTP as the communication protocol, and support a set of common operations, such as GET, POST, PUT, and DELETE, for retrieving, creating, updating, and deleting resources. They also use standard HTTP response codes to indicate the success or failure of an operation, and use HTTP headers to provide additional information and metadata about the request and response.
Overall, a RESTful API is a useful and widely-used tool for building web services that can be accessed and used by a wide range of clients and applications. It provides a standardized and interoperable interface for communication between different systems, and allows for the creation of flexible and scalable web services.
JSON:
Testing request: contain methods (get, post), url, body and headers. https://code.visualstudio.com/api/extension-guides/testing
requirements.txt:
pip install -r requirements.txt
,
Docker
Definition: [Docker] is a containers hardware use linux kernel container inside docker, lighter. A [docker_image]is a snapshot of a source code, libraries, dependencies.
Write Dockerfile:
Build docker:
docker build -t rest-apis-flask-python .
Run docker:
docker run -dp -p 5000 rest-apis-flask-python
run from image:
docker run -dp 5000:5000 flask-smorest-api
run with volume: update to changes
docker run -dp 5000:5000 -w /app -v "/c/Documents/yourproject:/app" flask-smorest-api
gunicorn: deploy with best performance
run locally:
docker run -dp 5000:5000 -w /app -v "$(pwd):/app" teclado-site-flask sh -c "flask run --host 0.0.0.0"
File structure
app:
/.venv:
python -m venv .venv
flask:
pip install flask
andflask run
app config:
blueprint and registers
swagger ui: http://127.0.0.1:5000/swagger-ui
jwt: secret key signature (access token) and error handling
/blocklist: log an user out we must revoke their JWT.
alembic: Alembic also tracks each of these migrations over time, so that you can easily go to a past version of the database. This is useful if bugs are introduced or the feature requirements change. (no longer require sqlalchemy).
flask db init
,flask db migrate
andflask db upgrade
./migration: manually review db migrations (first)
''' import libraries, resources, models, db create app: app config initialize database jwt authorization (secret key) api register blueprints '''
db:
/resources: flask_smorest, sqlalchemy, jwt, MethodView
Create store:
post
is usually used to receive data from clients and create resources with it.Create items: dynamic endpoint
@app.route("/store/<string:name>/item")
Get a particular store:
Retrieve all stores and their items:
Get only items in a store:
Create blueprints: group related request.
MethodView
each method maps to one endpoint.Using marsmallow for validation: it will inject the validated data into out method and add Swagger UI documentation. '''python @blp.arguments(ItemSchema) def post(self, item_data): '''
Decorating responses: update your documentation (show data and status returned by the endpoint). Pass any data your endpoint returns through the marshmallow schema, casting data types and removing data that isn't in the schema.
@blp.response(200, ItemSchema)
User: register and logout blueprints
require login: jwt authorization
@jwt_required()
''' dynamic endpoint blueprint(group related) validation for protect the endpoint, require login decorating response, verify marsmallow schema (types) and swagger documentation function (get, put, post, delete, update): query database; admin level to perform request (jwt) try add database (sqlalchemy) raise exceptions '''
schemas: marshmallow
Similar a la definición de tipos de input/output en hops antes de crear funciones; o a la definición previa de los schemas en las bases de datos postgres. Different requirement, different squemas.
import: marshmallow library define type data field. Then pass incoming data thought the validator.
Validation: Check for validation (type, requirement). ''' class Schema: field.type for validation (dump_only or/and required) '''
PlainSchema: avoid infinite nesting (not use nesting)
ItemSchema: use nesting
UserSchema:
/models: sqlalchemy
Models and schemas are modified at the same time.
simple model (without relationships): define fields properties, exactly the same as postgres database. (similar to schemas).
one to many:
UserModel:
/instances:
Última actualización
¿Te fue útil?