This ReadMe has two version. One in english, and another in Portuguese.
A Python API REST, that implements CRUD in a MongoDB database.
This program has a Makefile with some receipts to help us developing/deploying.
Open a terminal in project root, and type:
- make setup
In the same terminal, type:
- make up
After a quickly period of time (about 30 seconds), type in the same terminal:
- make web
For each document added by the endpoint, a message is sent to a Kafka topic (with the name SCRAPPING_TOPIC
),
which the worker will read this topic to realize some actions with it.
Open a new terminal, in the project root, and type:
- make start-kafka-scrapping-worker
Curl this endpoint below to check if it's healthy, typing:
curl --request GET 'http://127.0.0.1:3000/health'
It must return "ok"
You could import the json files in folder /postman
(in the root source), to your postman.
It will be easier to use manipulate the endpoints using postman.
To see the documentation about available endpoints, use:
METHOD
:GET
ENDPOINT
: /docs
Generate a new token to use in the ENDPOINT below ( it has a 1-hour duration ):
METHOD
:POST
ENDPOINT
: /tokenBODY
:
{
"user": "Some User"
}
RETURNS
:
{
"status": "ok",
"data": {
"token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiRXZlcnRvbiBUb21hbG9rIiwiZXhwIjoxNjEwOTAxMzgxfQ.kWluJ6MDoCGJBdMcT9DlI75ZabER32g43VWT4EO45LQ"
}
}
METHOD
:POST
ENDPOINT
: /toolsHEADER
:authorization
=Bearer ...
BODY
:
{
"title": "Api",
"link": "https://github.com/python/node-scrapper",
"description": "Webservice.",
"tags":["node", "express", "scrapping", "axios"]
}
RETURNS
:
{
"status": "ok",
"data": {
"title": "Api",
"link": "https://github.com/python/node-scrapper",
"description": "Webservice.",
"tags": [
"node",
"express",
"scrapping",
"axios"
],
"id": "SOME ID"
}
}
This endpoint will search for documents. If you only call it, it'll return all the documents.
You could use parameters, as tag
to filter results by tag, and skip
to set an offset, and limit
to limit the number of returning documents.
METHOD
:GET
ENDPOINT
: /toolsPARAMETROS
: OPTIONAL- tag (set a filter by tag)
- skip (skip documents)
- limit (limit documents)
HEADER
:authorization
=Bearer ...
RETORNO
:
{
"status": "ok",
"data": [
{
"title": "Api",
"link": "https://github.com/python/node-scrapper",
"description": "Webservice.",
"tags": [
"node",
"express",
"scrapping",
"axios"
],
"id": "SOME ID"
},
{
"title": "WebServer",
"link": "https://github.com/python/node-webserver",
"description": "Web",
"tags": [
"node",
"express",
"react",
"mongodb"
],
"id": "SOME ID"
},
...
]
}
Endpoint to edit a document
METHOD
:PUT
ENDPOINT
: /tools/:idHEADER
:authorization
=Bearer ...
BODY
:
{
"title": "New API",
"link": "https://github.com/python/python-new-api",
"description": "NEW Webservice.",
"tags":["python", "requests", "scrapping", "bs4", "xpath"]
}
RETURNS
:
{
"status": "ok",
"data": {
"title": "New API",
"link": "https://github.com/python/python-new-api",
"description": "NEW Webservice.",
"tags":["python", "requests", "scrapping", "bs4", "xpath"]
"id": "SOME ID"
}
}
Endpoint to remove a document by IDs
METHOD
:DELETE
ENDPOINT
: /tools/:idHEADER
:authorization
=Bearer ...
RETORNO
:
204 NO CONTENT
Key | Description |
---|---|
MONGO_URL | Database's URL |
JWT_SECRET | JWT secret generation |
TIME_TO_TOKEN_EXPIRES_IN_MINUTES | Time to expire the JWT |
KAFKA_BROKER | Kafka broker's URL |
API REST, utilizando BACKEND em python, que realiza CRUD de tools em um banco de dados NoSQL (MongoDB).
Este código, possui um arquivo Makefile com diversas receitas, para auxiliar no desenvolvimento/deploy do código.
Abra um terminal, no root do projeto, e digite:
- make setup
No mesmo terminal, digite:
- make up
Após um curto periodo de tempo (cerca de 30 segundos), digite no mesmo terminal:
- make web
Para cada documento adicionado pelo endpoint, uma mensagem é enviada para o tópico kafka de nome SCRAPPING_TOPIC, o qual temos um worker especifico para ler esse tópico e realizar certas ações com a mensagem.
Abra um novo terminal no root do projeto, e digite:
- make start-kafka-scrapping-worker
Utilize o curl para verificar se o sistema esta funcinando corretamente, digitando:
curl --request GET 'http://127.0.0.1:3000/health'
Deve retornar "ok"
Para ver a documentação em swagger:
METHOD
:GET
ENDPOINT
: /docs
Gere um novo token para utilizar nos ENDPOINTS abaixo ( o token tem DURAÇÃO DE 1 HORA ):
METHOD
:POST
ENDPOINT
: /tokenBODY
:
{
"user": "Some User"
}
RETORNO
:
{
"status": "ok",
"data": {
"token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiRXZlcnRvbiBUb21hbG9rIiwiZXhwIjoxNjEwOTAxMzgxfQ.kWluJ6MDoCGJBdMcT9DlI75ZabER32g43VWT4EO45LQ"
}
}
METHOD
:POST
ENDPOINT
: /toolsHEADER
:authorization
=Bearer ...
BODY
:
{
"title": "Api",
"link": "https://github.com/python/node-scrapper",
"description": "Webservice.",
"tags":["node", "express", "scrapping", "axios"]
}
RETORNO
:
{
"status": "ok",
"data": {
"title": "Api",
"link": "https://github.com/python/node-scrapper",
"description": "Webservice.",
"tags": [
"node",
"express",
"scrapping",
"axios"
],
"id": "SOME ID"
}
}
Rota para procurar documentos. Caso apenas chamada, trás todos os documentos ( lógica de negócio ).
Aceita parametros tag
para filtro, e skip
para pular documentos, e limit
para limitar numero de documentos
no retorno.
METHOD
:GET
ENDPOINT
: /toolsPARAMETROS
: OPCIONAIS- tag (filtrar pela tag)
- skip (pula numero de documentos)
- limit (limita o numero de documentos)
HEADER
:authorization
=Bearer ...
RETORNO
:
{
"status": "ok",
"data": [
{
"title": "Api",
"link": "https://github.com/python/node-scrapper",
"description": "Webservice.",
"tags": [
"node",
"express",
"scrapping",
"axios"
],
"id": "SOME ID"
},
{
"title": "WebServer",
"link": "https://github.com/python/node-webserver",
"description": "Web",
"tags": [
"node",
"express",
"react",
"mongodb"
],
"id": "SOME ID"
},
...
]
}
Rota para edição de documento
METHOD
:PUT
ENDPOINT
: /tools/:idHEADER
:authorization
=Bearer ...
BODY
:
{
"title": "New API",
"link": "https://github.com/python/python-new-api",
"description": "NEW Webservice.",
"tags":["python", "requests", "scrapping", "bs4", "xpath"]
}
RETORNO
:
{
"status": "ok",
"data": {
"title": "New API",
"link": "https://github.com/python/python-new-api",
"description": "NEW Webservice.",
"tags":["python", "requests", "scrapping", "bs4", "xpath"]
"id": "SOME ID"
}
}
Rota para remoção de documento por um ids
METHOD
:DELETE
ENDPOINT
: /tools/:idHEADER
:authorization
=Bearer ...
RETORNO
:
204 NO CONTENT
Chave | Descrição |
---|---|
MONGO_URL | URL de acesso ao banco |
JWT_SECRET | Segredo para geração de token |
TIME_TO_TOKEN_EXPIRES_IN_MINUTES | Tempo de expiração em minutos de um token gerado |
KAFKA_BROKER | URL de acesso ao broker do kafka |