Skip to content

Commit

Permalink
updated helper functions and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
emraher committed Jul 12, 2024
1 parent c7fa722 commit aa03c94
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
2 changes: 1 addition & 1 deletion R/cbRt_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ cbrt_get <- function(series,
as = c("tibble", "tsibble", "data.frame", "data.table")) {


token <- Sys.getenv("EVDS_TOKEN") # Get token from .Renviron
if (is.null(token)) token <- Sys.getenv("EVDS_TOKEN") # Get token from .Renviron

# EVDS combines series if multiple series are requested.
# If one yearly and one monthly requested, API converts monthly to yearly.
Expand Down
50 changes: 27 additions & 23 deletions R/cbRt_helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
# -------------------------------------------------------------------------- ###
get_categories_info <- function(token = NULL) {
if (is.null(token)) token <- Sys.getenv("EVDS_TOKEN")
urlroot <- "https://evds2.tcmb.gov.tr/service/evds/categories/key="
url <- paste0(urlroot, token, "&type=json")
doc <- jsonlite::fromJSON(url)
urlroot <- "https://evds2.tcmb.gov.tr/service/evds/categories/"
url <- paste0(urlroot, "type=json")
doc <- cbrt_geturl(url, token = token)
doc <- jsonlite::fromJSON(httr::content(doc, as = "text", encoding = "UTF-8"))
return(doc)
}

Expand All @@ -19,24 +20,26 @@ get_categories_info <- function(token = NULL) {
# -------------------------------------------------------------------------- ###
get_groups_info <- function(token = NULL, category_id = NULL) {
if (is.null(token)) token <- Sys.getenv("EVDS_TOKEN")
urlroot <- "https://evds2.tcmb.gov.tr/service/evds/datagroups/key="
urlroot <- "https://evds2.tcmb.gov.tr/service/evds/datagroups/"

if (!is.null(category_id)) {
url <- paste0(urlroot, token, "&mode=2&code=", category_id, "&type=json")
doc <- jsonlite::fromJSON(url)
doc <- doc %>% dplyr::select(.data$CATEGORY_ID, .data$DATAGROUP_CODE,
.data$DATAGROUP_NAME_ENG, .data$FREQUENCY,
.data$START_DATE, .data$END_DATE,
url <- paste0(urlroot, "mode=2&code=", category_id, "&type=json")
doc <- cbrt_geturl(url, token = token)
doc <- jsonlite::fromJSON(httr::content(doc, as = "text", encoding = "UTF-8"))
doc <- doc %>% dplyr::select(CATEGORY_ID, DATAGROUP_CODE,
DATAGROUP_NAME_ENG, FREQUENCY,
START_DATE, END_DATE,
tidyselect::everything())
} else {
url <- paste0(urlroot, token, "&mode=0&type=json")
doc <- jsonlite::fromJSON(url)
doc <- doc %>% dplyr::select(.data$CATEGORY_ID, .data$DATAGROUP_CODE,
.data$DATAGROUP_NAME_ENG, .data$FREQUENCY,
.data$START_DATE, .data$END_DATE,
url <- paste0(urlroot, "mode=0&type=json")
doc <- cbrt_geturl(url, token = token)
doc <- jsonlite::fromJSON(httr::content(doc, as = "text", encoding = "UTF-8"))
doc <- doc %>% dplyr::select(CATEGORY_ID, DATAGROUP_CODE,
DATAGROUP_NAME_ENG, FREQUENCY,
START_DATE, END_DATE,
tidyselect::everything())
doc <- dplyr::filter(doc, .data$DATAGROUP_CODE != "bie_bosluk1")
doc <- dplyr::filter(doc, !is.na(.data$START_DATE))
doc <- dplyr::filter(doc, DATAGROUP_CODE != "bie_bosluk1")
doc <- dplyr::filter(doc, !is.na(START_DATE))
}

return(doc)
Expand All @@ -49,12 +52,13 @@ get_groups_info <- function(token = NULL, category_id = NULL) {
# code is getGroups$DATAGROUP_CODE
get_series_info <- function(token = NULL, code) {
if (is.null(token)) token <- Sys.getenv("EVDS_TOKEN")
urlroot <- "https://evds2.tcmb.gov.tr/service/evds/serieList/key="
url <- paste0(urlroot, token, "&type=json&code=", code)
doc <- jsonlite::fromJSON(url)
doc <- doc %>% dplyr::select(.data$DATAGROUP_CODE, .data$SERIE_CODE,
.data$SERIE_NAME_ENG, .data$FREQUENCY_STR,
.data$START_DATE, .data$END_DATE,
urlroot <- "https://evds2.tcmb.gov.tr/service/evds/serieList/"
url <- paste0(urlroot, "type=json&code=", code)
doc <- cbrt_geturl(url, token = token)
doc <- jsonlite::fromJSON(httr::content(doc, as = "text", encoding = "UTF-8"))
doc <- doc %>% dplyr::select(DATAGROUP_CODE, SERIE_CODE,
SERIE_NAME_ENG, FREQUENCY_STR,
START_DATE, END_DATE,
tidyselect::everything())

return(doc)
Expand Down Expand Up @@ -206,7 +210,7 @@ json_read_res <- function(res) {
names(df)[names(df) == "UNIXTIME"] <- "Date"

# Reorder columns
df <- df %>% dplyr::select(.data$Date, tidyselect::everything())
df <- df %>% dplyr::select(Date, tidyselect::everything())

return(df)
}
10 changes: 6 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,13 @@ token <- Sys.getenv("EVDS_TOKEN") # Get token from .Renviron
# 4 TP.KB.O06.TRL YILLIK
# EDDS API returns following
url <- paste0("https://evds2.tcmb.gov.tr/service/evds/series=TP.AB.B1-TP.AB.C2-TP.BKEA.S001-TP.KB.O06.TRL&startDate=01-01-2017&endDate=01-01-2020&type=json&key=", token, "&formulas=2-3-7-8")
url <- paste0("https://evds2.tcmb.gov.tr/service/evds/series=TP.AB.B1-TP.AB.C2-TP.BKEA.S001-TP.KB.O06.TRL&startDate=01-01-2017&endDate=01-01-2020&type=json", "&formulas=2-3-7-8")
(edds_dat <- jsonlite::fromJSON(url) %>%
.[["items"]] %>%
tibble::as_tibble())
(edds_dat <- cbRt:::cbrt_geturl(url, token = token) %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON() %>%
.[["items"]] %>%
tibble::as_tibble())
```


Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,13 @@ token <- Sys.getenv("EVDS_TOKEN") # Get token from .Renviron
# 4 TP.KB.O06.TRL YILLIK

# EDDS API returns following
url <- paste0("https://evds2.tcmb.gov.tr/service/evds/series=TP.AB.B1-TP.AB.C2-TP.BKEA.S001-TP.KB.O06.TRL&startDate=01-01-2017&endDate=01-01-2020&type=json&key=", token, "&formulas=2-3-7-8")
url <- paste0("https://evds2.tcmb.gov.tr/service/evds/series=TP.AB.B1-TP.AB.C2-TP.BKEA.S001-TP.KB.O06.TRL&startDate=01-01-2017&endDate=01-01-2020&type=json", "&formulas=2-3-7-8")

(edds_dat <- jsonlite::fromJSON(url) %>%
.[["items"]] %>%
tibble::as_tibble())
(edds_dat <- cbRt:::cbrt_geturl(url, token = token) %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON() %>%
.[["items"]] %>%
tibble::as_tibble())
#> # A tibble: 4 × 10
#> Tarih `TP_AB_B1-2` `TP_AB_C2-3` `TP_BKEA_S001-7` `TP_KB_O06_TRL-8`
#> <chr> <chr> <chr> <chr> <chr>
Expand Down

0 comments on commit aa03c94

Please sign in to comment.