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

solves internal functions, arranges references in pkgdown and adds go… #131

Merged
merged 1 commit into from
Apr 23, 2024
Merged
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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
CodeOfConduct.md
Contributing.md
docs/
pkgdown
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(get_endpoint)
export(get_event)
export(get_event_stats)
export(get_raster)
Expand Down
3 changes: 2 additions & 1 deletion R/get_endpoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @importFrom httr2 request
#' @importFrom httr2 req_url_path_append
#' @importFrom httr2 req_url_query
#' @export
#' @keywords internal

get_endpoint <- function(dataset_type,
...) {
Expand Down Expand Up @@ -92,6 +92,7 @@ get_endpoint <- function(dataset_type,
#' @param dataset_type Type of identity dataset to get API dataset name for. It can be a vector with any combination of "support_vessel", "carrier_vessel" or "fishing_vessel"
#' @param search_type Type of vessel search to perform. Can be "search" or "id". "advanced" is no longer in use as of gfwr 2.0.0 and basic and advanced options can be accessed with parameters query and where
#' @param ids optional, a vector with vessel ids
#' @keywords internal
#' @param ... Other arguments that would depend on the dataset type.

get_identity_endpoint <- function(dataset_type,
Expand Down
134 changes: 134 additions & 0 deletions R/stash/get_endpoint_events.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#'
#' Function to get API dataset name for given event type
#'
#' @param dataset_type Type of dataset to get API dataset name for. It can be
#' "PORT_VISIT", "FISHING", "ENCOUNTER", "LOITERING", "eez" or "mpa""
#' @param ... Other arguments that would depend on the dataset type.
#' @importFrom httr2 request
#' @importFrom httr2 req_url_path_append
#' @importFrom httr2 req_url_query
#'

get_endpoint <- function(dataset_type,...) {

# API endpoint specific parameters from ...
args <- list(...)
for (i in seq_len(length(args))) {
assign(names(args[i]), args[[i]])
}

# API datasets to pass to param list
api_datasets <- c(
'port_visit' = "public-global-port-visits-c2-events:latest",
'encounter' = "public-global-encounters-events:latest",
'loitering' = "public-global-loitering-events:latest",
'fishing' = "public-global-fishing-events:latest",
'gap' = "public-global-gaps-events:latest",
'raster' = "public-global-fishing-effort:latest"
)

base <- httr2::request("https://gateway.api.globalfishingwatch.org/v3/")

# Get dataset ID for selected API
if (!dataset_type %in% c('EEZ', 'MPA', 'RFMO')) {
dataset <- api_datasets[[dataset_type]]
}

# Modify base URL with query parameters
if (dataset_type %in% c('PORT_VISIT','FISHING','ENCOUNTER','LOITERING')) {

args <- c(datasets = dataset, args)
endpoint <- base %>%
httr2::req_url_path_append('events') %>%
httr2::req_url_query(!!!args)

} else if (dataset_type == 'raster') {

args <- c(`datasets[0]` = dataset, args)
endpoint <- base %>%
httr2::req_url_path_append('4wings/report') %>%
httr2::req_url_query(!!!args)

} else if (dataset_type == "EEZ") {

endpoint <- base %>%
httr2::req_url_path_append("datasets/public-eez-areas/context-layers")

} else if (dataset_type == "MPA") {

endpoint <- base %>%
httr2::req_url_path_append("datasets/public-mpa-all/context-layers")

} else if (dataset_type == "RFMO") {

endpoint <- base %>%
httr2::req_url_path_append("datasets/public-rfmo/context-layers")

} else {
stop("Select valid dataset type")
}

return(endpoint)
}


#' Function to get API endpoint name for identity search
#'
#' @param dataset_type Type of identity dataset to get API dataset name for. It can be a vector with any combination of "support_vessel", "carrier_vessel" or "fishing_vessel"
#' @param search_type Type of vessel search to perform. Can be "search" or "id". "advanced" is no longer in use as of gfwr 2.0.0 and basic and advanced options can be accessed with parameters query and where
#' @param ids optional, a vector with vessel ids
#' @param ... Other arguments that would depend on the dataset type.

get_identity_endpoint <- function(dataset_type,
search_type,
ids,
...) {

#this is unnecessary if the function is called from within get vessel info
if (search_type %in% c("advanced", "basic")) {
# Signal the deprecation to the user
warning("basic or advanced search are no longer in use as of gfwr 2.0.0. Options are 'search' or 'id'. Use `query` for simple queries and `where` for advanced SQL expressions")
search_type <- "search"
}
# API endpoint specific parameters from ...
args <- list(...)
for (i in seq_len(length(args))) {
assign(names(args[i]), args[[i]])
}

if (dataset_type == "all") dataset_type <- c("support_vessel", "carrier_vessel", "fishing_vessel")

base <- httr2::request("https://gateway.api.globalfishingwatch.org/v3/")

# API datasets to pass to param list
api_datasets <- c(
"support_vessel" = "public-global-support-vessels:latest",
"carrier_vessel" = "public-global-carrier-vessels:latest",
"fishing_vessel" = "public-global-fishing-vessels:latest"
)

# Get dataset ID for selected API
dataset <- api_datasets[names(api_datasets) %in% dataset_type]
dataset <- vector_to_array(dataset, type = "dataset")

# swap name is searching by vessel id
if (search_type == "id") {
#names(args)[names(args) == "query"] <- "ids"
args <- vector_to_array(ids, type = "ids")
args <- args[!names(args) %in% c('limit','offset')]
path_append <- "vessels"
} else if (search_type == "search") {
path_append <- "vessels/search"
}
#where <- vector_to_array(query, type = "where")
args <- c(dataset,
#query,
#where,
args)

endpoint <- base %>%
httr2::req_url_path_append(path_append) %>%
httr2::req_url_query(!!!args)

return(endpoint)
}
Loading