From 6220d337f9c19a37d3794c2f2278c2e80043655b Mon Sep 17 00:00:00 2001 From: Aleksander Eilertsen Date: Thu, 28 Oct 2021 08:35:15 -0400 Subject: [PATCH 1/3] Update API spec --- R/start_api.R | 2 - inst/plumber/v1/endpoints.R | 7 --- inst/plumber/v1/openapi.yaml | 85 ++++++++++++++++++++++++++---------- inst/plumber/v1/plumber.R | 8 +++- 4 files changed, 67 insertions(+), 35 deletions(-) diff --git a/R/start_api.R b/R/start_api.R index 476e4c18..d57ce7b4 100644 --- a/R/start_api.R +++ b/R/start_api.R @@ -18,6 +18,4 @@ start_api <- function(api_version = "v1", api <- source(api_path) plumber::pr_run(api$value, host = host, port = port) - # plumber::plumb_api('pipapi', name = api_version) %>% - # plumber::pr_run(host = host, port = port) } diff --git a/inst/plumber/v1/endpoints.R b/inst/plumber/v1/endpoints.R index 9e45d7c8..41110128 100644 --- a/inst/plumber/v1/endpoints.R +++ b/inst/plumber/v1/endpoints.R @@ -345,10 +345,3 @@ function(req) { params$version <- NULL do.call(pipapi::ui_svy_meta, params) } - -# # Update UI -# #* @plumber -# function(pr) { -# pr %>% -# plumber::pr_set_api_spec(yaml::read_yaml("openapi.yaml")) -# } diff --git a/inst/plumber/v1/openapi.yaml b/inst/plumber/v1/openapi.yaml index a1b4c1bb..f149035f 100644 --- a/inst/plumber/v1/openapi.yaml +++ b/inst/plumber/v1/openapi.yaml @@ -1,6 +1,6 @@ openapi: 3.0.0 info: - description: This API computes all poverty and statistics available at pip.worldbank.org + description: This API computes all poverty and inequality statistics available at pip.worldbank.org title: Poverty and Inequality Platform API version: 0.0.1 paths: @@ -11,24 +11,65 @@ paths: default: description: Default response. parameters: [] - /api/v1/info: + /api/v1/versions: get: - summary: ' Returns information about the API and the available data versions' + summary: ' Return available data versions' responses: default: description: Default response. parameters: [] - /api/v1/get-cpi: + /api/v1/pip-info: get: - summary: ' Download CPI table as .csv' + summary: ' Return information about the API' responses: default: description: Default response. parameters: [] + /api/v1/valid-params: + get: + summary: ' Return valid query parameters' + responses: + default: + description: Default response. + parameters: + - in: query + name: version + schema: + type: string + description: Data version. Defaults to most recent version. See api/v1/versions. + - in: query + name: format + schema: + type: string + enum: [json, csv, rds] + description: Response format. Defaults to json. + /api/v1/aux: + get: + summary: ' Return auxiliary data tables' + responses: + default: + description: Default response. + parameters: + - in: query + name: table + schema: + type: string + description: Auxiliary data table to be returned. + - in: query + name: version + schema: + type: string + description: Data version. Defaults to most recent version. See api/v1/versions. + - in: query + name: format + schema: + type: string + enum: [json, csv, rds] + description: Response format. Defaults to json. /api/v1/pip: get: summary: ' Return main poverty and inequality statistics' - description: Main function to compute poverty and inequality statistics + description: Main function to compute poverty and inequality statistics. parameters: - in: query name: country @@ -76,13 +117,13 @@ paths: schema: type: boolean example: FALSE - description: Whether to aggregate results (Population weighted average) + description: Whether to aggregate results (Population weighted average). - in: query name: group_by schema: type: string enum: [wb, inc] - description: Aggregate results by pre-defined sub-groups + description: Aggregate results by pre-defined sub-groups. - in: query name: welfare_type schema: @@ -94,22 +135,18 @@ paths: schema: type: string enum: [all, national, rural, urban] - description: Survey coverage + description: Reporting coverage. + - in: query + name: version + schema: + type: string + description: Data version. Defaults to most recent version. See api/v1/versions. + - in: query + name: format + schema: + type: string + enum: [json, csv, rds] + description: Response format. Defaults to json. responses: '200': description: successful message - /api/v1/hp-stacked: - get: - summary: ' Return data for PIP homepage main chart' - description: Return data for PIP homepage main chart. Stacked line chart. - parameters: - - in: query - name: povline - schema: - type: number - minimum: 0 - maximum: 100 - example: 1.90 - description: Poverty line to be used to compute poverty mesures. - - diff --git a/inst/plumber/v1/plumber.R b/inst/plumber/v1/plumber.R index 388bad20..a08f6871 100644 --- a/inst/plumber/v1/plumber.R +++ b/inst/plumber/v1/plumber.R @@ -3,7 +3,8 @@ library(logger) library(glue) endpoints_path <- system.file("plumber/v1/endpoints.R", package = "pipapi") -convert_empty <- pipapi:::convert_empty +api_spec_path <- system.file("plumber/v1/openapi.yaml", package = "pipapi") +# convert_empty <- pipapi:::convert_empty plumber::pr(endpoints_path) %>% # pre-route log @@ -29,7 +30,10 @@ plumber::pr(endpoints_path) %>% plumber::pr_hook("exit", function() { # log_info('Bye bye: {proc.time()[["elapsed"]]}') }) %>% + # Set API spec plumber::pr_set_api_spec(api = function(spec) { spec$info$version <- utils::packageVersion("pipapi") %>% as.character() spec - }) + }) %>% + plumber::pr_set_api_spec( + yaml::read_yaml(api_spec_path)) From 9852681c65d194857efbdf8969886f34fb5b5c6d Mon Sep 17 00:00:00 2001 From: Aleksander Eilertsen Date: Thu, 28 Oct 2021 08:39:27 -0400 Subject: [PATCH 2/3] Update API doc styling --- inst/plumber/v1/plumber.R | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/inst/plumber/v1/plumber.R b/inst/plumber/v1/plumber.R index a08f6871..e6908021 100644 --- a/inst/plumber/v1/plumber.R +++ b/inst/plumber/v1/plumber.R @@ -36,4 +36,18 @@ plumber::pr(endpoints_path) %>% spec }) %>% plumber::pr_set_api_spec( - yaml::read_yaml(api_spec_path)) + yaml::read_yaml(api_spec_path)) %>% + # Style doc page + plumber::pr_set_docs( + "rapidoc", + show_info = TRUE, + show_header = FALSE, + bg_color = "#FFFFF", + text_color = "#343A40", + primary_color = "#007BFF", + regular_font = "sans-serif", + mono_font = "monospace", + font_size = "default", + # render_style = "view", + # sort_endpoints_by = "none" + ) From fd7dc950b712e09ab63fd4dbb5ecd015c81b99c3 Mon Sep 17 00:00:00 2001 From: Aleksander Eilertsen Date: Thu, 28 Oct 2021 08:42:49 -0400 Subject: [PATCH 3/3] Add rapidoc to Imports --- DESCRIPTION | 3 ++- inst/plumber/v1/plumber.R | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0022500d..51fde637 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -45,7 +45,8 @@ Imports: wbpip, readr, glue, - logger + logger, + rapidoc Remotes: PIP-Technical-Team/wbpip@master Depends: diff --git a/inst/plumber/v1/plumber.R b/inst/plumber/v1/plumber.R index e6908021..d7cc957a 100644 --- a/inst/plumber/v1/plumber.R +++ b/inst/plumber/v1/plumber.R @@ -1,4 +1,5 @@ library(plumber) +library(rapidoc) library(logger) library(glue)