Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removendo /v1 #555

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ eb local run

## Testando servidor

Caso a execução tenha sido realizada com sucesso, você pode utilizar o seu cliente de api REST para acessar o servidor local, que está localizado em http://{HOST}:{PORT}/v1/orgaos
Caso a execução tenha sido realizada com sucesso, você pode utilizar o seu cliente de api REST para acessar o servidor local, que está localizado em http://{HOST}:{PORT}/v2/orgaos

## Documentando as rotas da API utilizando o swagger

Expand All @@ -136,7 +136,7 @@ $ go install github.com/swaggo/swag/cmd/swag@latest

Com o binário instalado, podemos iniciar a criação da documentação.

Como exemplo iremos documentar a rota da api `/v1/orgao/:orgao`. Os passos são:
Como exemplo iremos documentar a rota da api `/v2/orgao/:orgao`. Os passos são:

### 1 - Ir até o método chamado pela rota(nesse caso será o `GetAgencyById`)

Expand All @@ -155,7 +155,7 @@ func (h handler) GetAgencyById(c echo.Context) error {
// @Param orgao path string true "ID do órgão. Exemplos: tjal, tjba, mppb."
// @Success 200 {object} agency "Requisição bem sucedida."
// @Failure 404 {string} string "Órgão não encontrado."
// @Router /v1/orgao/{orgao} [get]
// @Router /v2/orgao/{orgao} [get]
func (h handler) GetAgencyById(c echo.Context) error {
...
}
Expand Down
2 changes: 1 addition & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/uiapi/v1/orgao/resumo/{orgao}": {
"/uiapi/v2/orgao/resumo/{orgao}": {
"get": {
"description": "Retorna os dados anuais de um orgão",
"produces": [
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"version": "1.0"
},
"paths": {
"/uiapi/v1/orgao/resumo/{orgao}": {
"/uiapi/v2/orgao/resumo/{orgao}": {
"get": {
"description": "Retorna os dados anuais de um orgão",
"produces": [
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ info:
title: API do dadosjusbr.org
version: "1.0"
paths:
/uiapi/v1/orgao/resumo/{orgao}:
/uiapi/v2/orgao/resumo/{orgao}:
get:
description: Retorna os dados anuais de um orgão
operationId: GetAnnualSummary
Expand Down
25 changes: 4 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,49 +168,32 @@ func main() {
log.Fatalf("Error creating uiapi handler: %q", err)
}
// Return a summary of an agency. This information will be used in the head of the agency page.
uiAPIGroup.GET("/v1/orgao/resumo/:orgao/:ano/:mes", uiApiHandler.GetSummaryOfAgency)
uiAPIGroup.GET("/v2/orgao/resumo/:orgao/:ano/:mes", uiApiHandler.V2GetSummaryOfAgency)
// TODO: Apagar essa rota (v1) quando o site migrar para a nova rota.
uiAPIGroup.GET("/v1/orgao/resumo/:orgao", uiApiHandler.GetAnnualSummary)
// Return a annual summary of an agency.
uiAPIGroup.GET("/v2/orgao/resumo/:orgao", uiApiHandler.GetAnnualSummary)
// Return all the salary of a month and year. This will be used in the point chart at the entity page.
uiAPIGroup.GET("/v1/orgao/salario/:orgao/:ano/:mes", uiApiHandler.GetSalaryOfAgencyMonthYear)
uiAPIGroup.GET("/v2/orgao/salario/:orgao/:ano/:mes", uiApiHandler.V2GetSalaryOfAgencyMonthYear)
// Return the total of salary of every month of a year of a agency. The salary is divided in Wage, Perks and Others. This will be used to plot the bars chart at the state page.
uiAPIGroup.GET("/v1/orgao/totais/:orgao/:ano", uiApiHandler.GetTotalsOfAgencyYear)
uiAPIGroup.GET("/v2/orgao/totais/:orgao/:ano", uiApiHandler.V2GetTotalsOfAgencyYear)
// Return basic information of a type or state
uiAPIGroup.GET("/v1/orgao/:grupo", uiApiHandler.GetBasicInfoOfType)
uiAPIGroup.GET("/v2/orgao/:grupo", uiApiHandler.V2GetBasicInfoOfType)
uiAPIGroup.GET("/v1/geral/remuneracao/:ano", uiApiHandler.GetGeneralRemunerationFromYear)
uiAPIGroup.GET("/v2/geral/remuneracao/:ano", uiApiHandler.V2GetGeneralRemunerationFromYear)
uiAPIGroup.GET("/v1/geral/resumo", uiApiHandler.GeneralSummaryHandler)
uiAPIGroup.GET("/v2/geral/resumo", uiApiHandler.GetGeneralSummary)
// Retorna um conjunto de dados a partir de filtros informados por query params
uiAPIGroup.GET("/v2/pesquisar", uiApiHandler.SearchByUrl)
// Baixa um conjunto de dados a partir de filtros informados por query params
uiAPIGroup.GET("/v2/download", uiApiHandler.DownloadByUrl)

apiHandler := papi.NewHandler(pgS3Client, conf.DadosJusURL, conf.PackageRepoURL)
// Public API configuration
apiGroup := e.Group("/v1", middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderContentLength},
}))
// Return agency
apiGroup.GET("/orgao/:orgao", apiHandler.V1GetAgencyById)
// Return all agencies
apiGroup.GET("/orgaos", apiHandler.V1GetAllAgencies)
// Return MIs by year
apiGroup.GET("/dados/:orgao/:ano", apiHandler.GetMonthlyInfo)
// Return MIs by month
apiGroup.GET("/dados/:orgao/:ano/:mes", apiHandler.GetMonthlyInfo)

// V2 public api, to be used by the new returned data
apiGroupV2 := e.Group("/v2", middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderContentLength},
}))
// Return agency
apiGroupV2.GET("/orgao/:orgao", apiHandler.V2GetAgencyById)
// Return all agencies
apiGroupV2.GET("/orgaos", apiHandler.V2GetAllAgencies)
// Return MIs by year
apiGroupV2.GET("/dados/:orgao/:ano", apiHandler.GetMonthlyInfosByYear)
Expand Down
137 changes: 0 additions & 137 deletions papi/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ func NewHandler(client *storage.Client, dadosJusURL, packageRepoURL string) *han
}
}

func (h handler) V1GetAgencyById(c echo.Context) error {
agencyName := c.Param("orgao")
agency, err := h.client.Db.GetAgency(agencyName)
if err != nil {
return c.JSON(http.StatusNotFound, "Agency not found")
}
host := c.Request().Host
agency.URL = fmt.Sprintf("%s/v1/orgao/%s", host, agency.ID)
return c.JSON(http.StatusOK, agency)
}

// @ID GetAgencyById
// @Tags public_api
// @Description Busca um órgão específico utilizando seu ID.
Expand Down Expand Up @@ -77,19 +66,6 @@ func (h handler) V2GetAgencyById(c echo.Context) error {
return c.JSON(http.StatusOK, agency)
}

func (h handler) V1GetAllAgencies(c echo.Context) error {
agencies, err := h.client.Db.GetAllAgencies()
if err != nil {
fmt.Println("Error while listing agencies: %w", err)
return c.JSON(http.StatusInternalServerError, "Error while listing agencies")
}
host := c.Request().Host
for i := range agencies {
agencies[i].URL = fmt.Sprintf("%s/v1/orgao/%s", host, agencies[i].ID)
}
return c.JSON(http.StatusOK, agencies)
}

// @ID GetAllAgencies
// @Tags public_api
// @Description Busca todos os órgãos disponíveis.
Expand Down Expand Up @@ -133,119 +109,6 @@ func (h handler) V2GetAllAgencies(c echo.Context) error {
return c.JSON(http.StatusOK, agencies)
}

func (h handler) GetMonthlyInfo(c echo.Context) error {
year, err := strconv.Atoi(c.Param("ano"))
if err != nil {
return c.JSON(http.StatusBadRequest, fmt.Sprintf("Parâmetro ano=%d inválido", year))
}
agencyName := strings.ToLower(c.Param("orgao"))
var monthlyInfo map[string][]models.AgencyMonthlyInfo
month := c.Param("mes")
if month != "" {
m, err := strconv.Atoi(month)
if err != nil {
return c.JSON(http.StatusBadRequest, fmt.Sprintf("Parâmetro mes=%d inválido", m))
}
oma, _, err := h.client.Db.GetOMA(m, year, agencyName)
if err != nil {
return c.JSON(http.StatusBadRequest, "Error getting OMA data")
}
monthlyInfo = map[string][]models.AgencyMonthlyInfo{
agencyName: {*oma},
}
} else {
monthlyInfo, err = h.client.Db.GetMonthlyInfo([]models.Agency{{ID: agencyName}}, year)
}
if err != nil {
log.Printf("[totals of agency year] error getting data for first screen(ano:%d, estado:%s):%q", year, agencyName, err)
return c.JSON(http.StatusBadRequest, fmt.Sprintf("Parâmetro ano=%d ou orgao=%s inválidos", year, agencyName))
}

if len(monthlyInfo[agencyName]) == 0 {
return c.NoContent(http.StatusNotFound)
}

var sumMI []summaryzedMI
for i := range monthlyInfo {
for _, mi := range monthlyInfo[i] {
// Fazemos duas checagens no formato do ProcInfo para saber se ele é vazio pois alguns dados diferem, no banco de dados, quando o procinfo é nulo.
if mi.ProcInfo == nil || mi.ProcInfo.String() == "" {
sumMI = append(
sumMI,
summaryzedMI{
AgencyID: mi.AgencyID,
Error: nil,
Month: mi.Month,
Year: mi.Year,
Package: &backup{
URL: h.formatDownloadUrl(mi.Package.URL),
Hash: mi.Package.Hash,
Size: mi.Package.Size,
},
Summary: &summaries{
MemberActive: summary{
Count: mi.Summary.Count,
BaseRemuneration: dataSummary{
Max: mi.Summary.BaseRemuneration.Max,
Min: mi.Summary.BaseRemuneration.Min,
Average: mi.Summary.BaseRemuneration.Average,
Total: mi.Summary.BaseRemuneration.Total,
},
OtherRemunerations: dataSummary{
Max: mi.Summary.OtherRemunerations.Max,
Min: mi.Summary.OtherRemunerations.Min,
Average: mi.Summary.OtherRemunerations.Average,
Total: mi.Summary.OtherRemunerations.Total,
},
},
},
Metadata: &metadata{
OpenFormat: mi.Meta.OpenFormat,
Access: mi.Meta.Access,
Extension: mi.Meta.Extension,
StrictlyTabular: mi.Meta.StrictlyTabular,
ConsistentFormat: mi.Meta.ConsistentFormat,
HasEnrollment: mi.Meta.HaveEnrollment,
HasCapacity: mi.Meta.ThereIsACapacity,
HasPosition: mi.Meta.HasPosition,
BaseRevenue: mi.Meta.BaseRevenue,
OtherRecipes: mi.Meta.OtherRecipes,
Expenditure: mi.Meta.Expenditure,
},
Score: &score{
Score: mi.Score.Score,
CompletenessScore: mi.Score.CompletenessScore,
EasinessScore: mi.Score.EasinessScore,
},
Collect: &collect{
Duration: mi.Duration,
CrawlerRepo: mi.CrawlerRepo,
CrawlerVersion: mi.CrawlerVersion,
ParserRepo: mi.ParserRepo,
ParserVersion: mi.ParserVersion,
}})
// The status 4 is a report from crawlers that data is unavailable or malformed. By removing them from the API results, we make sure they are displayed as if there is no data.
} else if mi.ProcInfo.Status != 4 {
sumMI = append(
sumMI,
summaryzedMI{
AgencyID: mi.AgencyID,
Error: &miError{
ErrorMessage: mi.ProcInfo.Stderr,
Status: mi.ProcInfo.Status,
Cmd: mi.ProcInfo.Cmd,
},
Month: mi.Month,
Year: mi.Year,
Package: nil,
Summary: nil,
Metadata: nil})
}
}
}
return c.JSON(http.StatusOK, sumMI)
}

// @ID GetMonthlyInfo
// @Tags public_api
// @Description Busca um dado mensal de um órgão
Expand Down
Loading