Skip to content

Commit

Permalink
RC from V8 to V9 (#41)
Browse files Browse the repository at this point in the history
API V8 is deprecated, we now use API V9
  • Loading branch information
VincentGuyader authored Feb 13, 2024
1 parent db54e35 commit 043c6c8
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 135 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: togglr
Title: 'Toggl.com' Api for 'Rstudio'
Version: 0.1.99.9001
Version: 0.1.99.9002
Authors@R: c(
person("Vincent", "Guyader", , "[email protected]", role = c("aut", "cre")),
person("ThinkR", role = c("cph", "fnd"))
Expand Down Expand Up @@ -37,4 +37,4 @@ VignetteBuilder:
ByteCompile: true
Encoding: UTF-8
LazyData: TRUE
RoxygenNote: 7.2.0
RoxygenNote: 7.2.3
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# togglr 0.1.99.9002

* from api V8 to V9

# togglr 0.1.34.9000

* `get_time_entries()` can now handle multiple tags, stored in a list column (#21, @pat-s)
Expand Down
39 changes: 25 additions & 14 deletions R/Get_time_entries.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
get_time_entries <- function(api_token = get_toggl_api_token(),
since = Sys.time() - lubridate::weeks(1),
until = Sys.time()) {
url <- glue::glue("https://api.track.toggl.com/api/v8/time_entries?start_date={format_iso_8601(since)}&end_date={format_iso_8601(until)}")
url <- glue::glue("https://api.track.toggl.com/api/v9/me/time_entries?start_date={format_iso_8601(since)}&end_date={format_iso_8601(until)}")
res <- content(GET(url,
# verbose(),
authenticate(api_token, "api_token"),
Expand All @@ -31,20 +31,26 @@ get_time_entries <- function(api_token = get_toggl_api_token(),
return(data.frame())
}





# extract list of tags
tags <- map(res, ~ .x$tags)

res %>%
# remove tags column, otherwise bind_rows() adds additional rows if
# length(tags) > 1
map(~ {
.x$tags <- NULL
return(.x)
}) %>%
bind_rows() %>%
# add list column
mutate(tags = tags) %>%
woot <- get_project_id_and_name() |>
mutate(id=as.character(id))

res |> map(unlist) |> map(~ {
avirer <- which(grepl(names(.x), pattern = "tag"))
if (length(avirer) > 0){
.x <- .x[-avirer]
}
return(.x)
}) |> bind_rows() %>%
mutate(tags = tags) %>%
mutate(
duration = as.numeric(duration),
start = parse_iso_8601(start),
duration = case_when(
duration < 0 ~ as.integer(difftime(
Expand All @@ -60,7 +66,7 @@ get_time_entries <- function(api_token = get_toggl_api_token(),
),
stop = parse_iso_8601(stop)
) %>%
left_join(get_project_id_and_name(), by = c("pid" = "id")) %>%
left_join(woot, by = c("project_id" = "id")) %>%
select(
start,
stop,
Expand All @@ -71,7 +77,12 @@ get_time_entries <- function(api_token = get_toggl_api_token(),
pid,
wid,
everything()
) %>%
slice(nrow(.):1)
) %>% arrange(desc(start))

}






20 changes: 11 additions & 9 deletions R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ create_client <- function(name = "wihtout client",
workspace_id = get_workspace_id(api_token)
){

# POST https://api.track.toggl.com/api/v8/clients
message(glue("we create the client : {name}"))
POST("https://api.track.toggl.com/api/v8/clients",
POST(glue::glue("https://api.track.toggl.com/api/v9/workspaces/{workspace_id}/clients"),
verbose(),
authenticate(api_token,"api_token"),
encode="json",
body=toJSON(
list(client = list(
list(
# client = list(
name = name,
wid = workspace_id
)
# )
),
auto_unbox = TRUE)
)
Expand All @@ -40,6 +40,7 @@ POST("https://api.track.toggl.com/api/v8/clients",
#' get_all_client_info
#'
#' @param api_token the toggl api token
#' @param workspace_id workspace_id
#'
#' @return a data.frame
#' @export
Expand All @@ -48,8 +49,8 @@ POST("https://api.track.toggl.com/api/v8/clients",
#' get_all_client_info()
#' }
#' @export
get_all_client_info <- function(api_token=get_toggl_api_token()){
GET("https://api.track.toggl.com/api/v8/clients",authenticate(api_token,"api_token")) %>% content() %>% bind_rows()
get_all_client_info <- function(api_token=get_toggl_api_token(),workspace_id = get_workspace_id(api_token) ){
GET(glue::glue("https://api.track.toggl.com/api/v9/workspaces/{workspace_id}/clients"),authenticate(api_token,"api_token")) %>% content() %>% bind_rows()
}


Expand Down Expand Up @@ -92,14 +93,15 @@ client_id_to_name <- function(id, api_token = get_toggl_api_token()) {
#'
#' @param id client id
#' @param api_token the toggl api token
#' @param workspace_id workspace_id
#' @import assertthat
#' @importFrom dplyr filter pull
#' @return the client name
#' @export
#'
get_client_project <- function(id,api_token=get_toggl_api_token()){

GET(glue("https://api.track.toggl.com/api/v8/clients/{id}/projects?active=both"),authenticate(api_token,"api_token")) %>% content() %>% bind_rows()
get_client_project <- function(id,api_token=get_toggl_api_token(),workspace_id = get_workspace_id(api_token)){

get_all_projects() %>%
dplyr::filter(client_id == !! id)

}
9 changes: 7 additions & 2 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ get_current <- function(api_token=get_toggl_api_token()){
stop("you have to set your api token using set_toggl_api_token('XXXXXXXX')")

}
content(GET("https://api.track.toggl.com/api/v8/time_entries/current",
content (

GET("https://api.track.toggl.com/api/v9/me/time_entries/current",
# verbose(),
authenticate(api_token,"api_token"),
encode="json"))$data
encode="json")


)

}

Expand Down
70 changes: 48 additions & 22 deletions R/get_project.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ get_project_id <- function(project_name = get_context_project(),

}

project_tbl <- content(GET(
paste0(
"https://api.track.toggl.com/api/v8/workspaces/",
workspace_id,
"/projects?active=both"
),
# verbose(),
authenticate(api_token, "api_token"),
encode = "json"
)) %>% bind_rows()
# TODO la V9 permet de ne pas faire comme ceci et de faire la requete directement.



project_tbl <- get_all_projects()


if (ncol(project_tbl) == 0) {
id <- NULL
} else {
Expand Down Expand Up @@ -85,18 +81,48 @@ get_project_id_and_name <- function(

# active: possible values true/false/both. By default true. If false, only archived projects are returned.

content(GET(
paste0(
"https://api.track.toggl.com/api/v8/workspaces/",
workspace_id,
"/projects?active=both"
),
# verbose(),
authenticate(api_token, "api_token"),
encode = "json"
)) %>%
bind_rows() %>%
get_all_projects() %>%
select(id,name,'active') %>%
rename(project_name = name)

}
}


get_all_projects <- function(api_token = get_toggl_api_token(),
workspace_id = get_workspace_id(api_token)){
nb_page <- 1
all_projects <- list()


while (TRUE) {

# message("page = ",nb_page)
project_tbl_ <- content(GET(
paste0(
"https://api.track.toggl.com/api/v9/workspaces/",
workspace_id,
"/projects"
),
# verbose(),
authenticate(api_token, "api_token"),
encode = "json",query = list(page = nb_page, per_page = 200)

)) %>% bind_rows()

all_projects[[nb_page]] <- project_tbl_


if (nrow(project_tbl_) < 200) {
break #
} else {
nb_page <- nb_page + 1 # Passer à la page suivante
}




}

all_projects %>% bind_rows

}
9 changes: 4 additions & 5 deletions R/get_workspace.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ get_workspace_id <- function(
stop("you have to set your api token using set_toggl_api_token('XXXXXXXX')")

}
content(GET("https://api.track.toggl.com/api/v8/workspaces",
ppp <- content(GET("https://api.track.toggl.com/api/v9/workspaces",
# verbose(),
authenticate(api_token,"api_token"),
encode="json")) %>%
as.data.frame() %>%
.$id -> id
id <- id[1] # si plusieurs workspace , il faudra adapter
encode="json"))

ppp[[1]]$id -> id # si plusieurs workspace , il faudra adapter
if (length(id) == 0){
stop(paste("cant find workspace id - is your api token ok ?"))
id <- NULL
Expand Down
9 changes: 1 addition & 8 deletions R/get_workspace_users.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@ get_workspace_users <- function(api_token = get_toggl_api_token(),
workspace_id = get_workspace_id(api_token)){


url <- glue::glue("https://api.track.toggl.com/api/v8/workspaces/{workspace_id}/users")
url <- glue::glue("https://api.track.toggl.com/api/v9/workspaces/{workspace_id}/users")


wp <- content(GET(url,
# verbose(),
authenticate(api_token, "api_token"),
encode = "json"))
wp %>%
# map(~discard(.x,str_detect(names(.x),"blog"))) %>%
map(~keep(.x,names(.x) %in% c("id","default_wid","email","fullname","image_url"))) %>%
# map(~discard(.x,names(.x) %in% c("send_timer_notifications",
# "send_product_emails",
# "send_weekly_report",
# "openid_enabled",
# "openid_email"))) %>%
bind_rows()
}
2 changes: 1 addition & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ globalVariables(
"time",
"case_when",
"full_join" ,
"tribble",
"tribble","client_id",
"id","start","duration","pretty_duration",'project_name','description','pid','wid',
# get_summary_report
"total_currencies", "title", "id", "fullname", "time", "items"
Expand Down
Loading

0 comments on commit 043c6c8

Please sign in to comment.