Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
genesysr
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Genesys PGR
genesysr
Commits
e33bb826
Commit
e33bb826
authored
Sep 03, 2019
by
Matija Obreza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Download MCPD and PDCI data in Excel for the specified institute
parent
e2abcf98
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
98 additions
and
1 deletion
+98
-1
DESCRIPTION
DESCRIPTION
+1
-1
NAMESPACE
NAMESPACE
+3
-0
R/api-client.R
R/api-client.R
+19
-0
R/genesys.R
R/genesys.R
+75
-0
No files found.
DESCRIPTION
View file @
e33bb826
Package: genesysr
Version: 0.9.
2
Version: 0.9.
3
Title: Genesys PGR Client
Description: Access data on plant genetic resources from genebanks around the world published on Genesys (<https://www.genesys-pgr.org>).
Your use of data is subject to terms and conditions available at <https://www.genesys-pgr.org/content/legal/terms>.
...
...
NAMESPACE
View file @
e33bb826
# Generated by roxygen2: do not edit by hand
export(api1_url)
export(api_url)
export(authorization)
export(client_login)
export(download_mcpd)
export(download_pdci)
export(fetch_accessions)
export(filter_DOI)
export(filter_ORIGCTY)
...
...
R/api-client.R
View file @
e33bb826
...
...
@@ -18,6 +18,7 @@
#'
#' @param libname Library name
#' @param pkgname Package name
#' @keywords internal
.onLoad
<-
function
(
libname
,
pkgname
)
{
setup_production
()
}
...
...
@@ -80,6 +81,7 @@ authorization <- function(authorization) {
}
#' Ensure that environment has OAuth token
#' @keywords internal
.check_auth
<-
function
()
{
if
(
is.null
(
.genesysEnv
$
Authorization
))
{
warning
(
"You must first authorize with Genesys with user_login() or client_login(...)."
);
...
...
@@ -158,6 +160,7 @@ client_login <- function() {
}
#' @keywords internal
.api_call
<-
function
(
path
,
method
=
"get"
)
{
.check_auth
()
resp
<-
httr
::
GET
(
api_url
(
path
),
httr
::
add_headers
(
...
...
@@ -187,6 +190,21 @@ api_url <- function(path) {
paste0
(
.genesysEnv
$
server
,
"/api/v0"
,
path
)
}
#' Get full Genesys API v1 URL for a specific path
#'
#' @param path relative path of the API v1 endpoint (e.g. \code{/me})
#'
#' @return Absolute URL to an API call
#' @export
#'
#' @examples
#' api1_url("/me")
api1_url
<-
function
(
path
)
{
paste0
(
.genesysEnv
$
server
,
"/api/v1"
,
path
)
}
#' @keywords internal
.get
<-
function
(
path
,
query
=
NULL
)
{
.check_auth
()
resp
<-
httr
::
GET
(
api_url
(
path
),
query
=
query
,
httr
::
add_headers
(
...
...
@@ -206,6 +224,7 @@ api_url <- function(path) {
#' @param content.type Content-Type of the body
#'
#' @return httr response
#' @keywords internal
.post
<-
function
(
path
,
query
=
NULL
,
body
=
NULL
,
content.type
=
"application/json"
)
{
.check_auth
()
content
<-
jsonlite
::
toJSON
(
body
)
...
...
R/genesys.R
View file @
e33bb826
...
...
@@ -13,6 +13,7 @@
# limitations under the License.
#' Max pages to retrieve
#' @keywords internal
.MAX_ALLOWED_PAGES
<-
500
#' Who am i?
...
...
@@ -40,6 +41,7 @@ me <- function() {
#' }
#'
#' @return Paged data structure
#' @keywords internal
.fetch_accessions_page
<-
function
(
filters
=
list
(),
page
=
0
,
size
=
1000
,
selector
=
NULL
)
{
start_time
<-
as.numeric
(
as.numeric
(
Sys.time
())
*
1000
,
digits
=
15
)
resp
<-
.post
(
path
=
"/acn/filter"
,
query
=
list
(
p
=
page
,
l
=
size
),
body
=
filters
)
...
...
@@ -115,3 +117,76 @@ fetch_accessions <- function(filters = list(), page = NULL, size = 1000, selecto
paged
}
#' Download all passport data for one genebank in Excel format and save it to disk
#'
#' @param instituteCode FAO WIEWS institute code
#' @param file Target file name. Defaults to Genesys-provided file name in the current working directory.
#'
#' @examples
#' \dontrun{
#' # Download MCPD passport data for NGA039
#' excelData <- download_mcpd("NGA039")
#' }
#'
#' @export
#' @return HTTP response data
download_mcpd
<-
function
(
instituteCode
,
file
=
NULL
)
{
if
(
is.na
(
instituteCode
))
{
stop
(
"instituteCode parameter is required"
)
}
if
(
is.null
(
file
))
{
file
<-
paste0
(
"genesys-accessions-"
,
instituteCode
,
".xlsx"
)
}
resp
<-
httr
::
POST
(
api1_url
(
paste0
(
"/wiews/"
,
instituteCode
,
"/download"
)),
body
=
list
(
mcpd
=
"mcpd"
),
encode
=
"form"
,
httr
::
write_disk
(
file
),
httr
::
add_headers
(
Authorization
=
.genesysEnv
$
Authorization
))
# , httr::verbose())
if
(
httr
::
status_code
(
resp
)
!=
200
)
{
stop
(
"Genesys responded with HTTP status code "
,
httr
::
status_code
(
resp
),
". Expected 200."
)
}
invisible
(
resp
)
}
#' Download PDCI data for one genebank in Excel format and save it to disk.
#'
#' @param instituteCode FAO WIEWS institute code
#' @param file Target file name. Defaults to Genesys-provided file name in the current working directory.
#'
#' @examples
#' \dontrun{
#' # Download PDCI data for NGA039
#' excelData <- download_pdci("NGA039")
#' }
#'
#' @export
#' @return HTTP response data
download_pdci
<-
function
(
instituteCode
,
file
=
NULL
)
{
if
(
is.na
(
instituteCode
))
{
stop
(
"instituteCode parameter is required"
)
}
if
(
is.null
(
file
))
{
file
<-
paste0
(
"genesys-pdci-"
,
instituteCode
,
".xlsx"
)
}
resp
<-
httr
::
POST
(
api1_url
(
paste0
(
"/wiews/"
,
instituteCode
,
"/download"
)),
body
=
list
(
pdci
=
"pdci"
),
encode
=
"form"
,
httr
::
write_disk
(
file
),
httr
::
add_headers
(
Authorization
=
.genesysEnv
$
Authorization
))
# , httr::verbose())
if
(
httr
::
status_code
(
resp
)
!=
200
)
{
stop
(
"Genesys responded with HTTP status code "
,
httr
::
status_code
(
resp
),
". Expected 200."
)
}
invisible
(
resp
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment