Skip to content

Commit

Permalink
Merge pull request #1 from asiripanich/0.2.0
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
asiripanich authored Nov 19, 2020
2 parents 1e0a55a + 92e6e7e commit 4942019
Show file tree
Hide file tree
Showing 11 changed files with 732 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ jobs:
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
- name: Install ProtoBuf libraries and compiler
if: runner.os == 'Linux'
run: |
sudo apt-get install -y protobuf-compiler libprotobuf-dev libprotoc-dev
- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
Expand Down
18 changes: 10 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: tfnswapi
Type: Package
Title: Access TfNSW Open Data from R
Version: 0.1.0
Version: 0.2.0
Author: Amarin Siripanich
Maintainer: Amarin Siripanich <[email protected]>
Description: Access TfNSW Open Data from R.
Expand All @@ -15,12 +15,14 @@ Suggests:
testthat
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Depends:
R (>= 3.5.0)
Imports:
sf (>= 0.9.0),
magrittr (>= 2.0.0),
httr,
checkmate,
jsonlite,
stringr,
cli,
glue
httr (>= 1.4.0),
checkmate (>= 2.0.0),
jsonlite (>= 1.7.0),
stringr (>= 1.4.0),
cli (>= 2.0.0),
glue (>= 1.4.0),
RProtoBuf (>= 0.4.17)
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(tfnswapi_browse)
export(tfnswapi_get)
export(tfnswapi_get_api_key)
export(tfnswapi_get_response)
export(tfnswapi_has_api_key)
export(tfnswapi_register)
importFrom(magrittr,"%>%")
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# tfnswapi 0.2.0

* You now can quickly browse TfNSW API with `tfnswapi_browse()`.
* `tfnswapi_get()` is now able to parse 'application/x-google-protobuf', the protobuf format of GTFS realtime.

# tfnswapi 0.1.0

* Added a `NEWS.md` file to track changes to the package.
6 changes: 6 additions & 0 deletions R/browse.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#' Open API Explorer on your default browser.
#'
#' @export
tfnswapi_browse = function() {
utils::browseURL("https://opendata.transport.nsw.gov.au/developers/api-explorer")
}
56 changes: 36 additions & 20 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tfnsw_base_api_url = "https://api.transport.nsw.gov.au/"
#' @param key Character (optional). An API key can be provided, else if emptied
#' it will look for any previously registered API key.
#'
#' @return a response list
#' @return a response with parsed content.
#' @export
#'
#' @examples
Expand All @@ -25,27 +25,21 @@ tfnsw_base_api_url = "https://api.transport.nsw.gov.au/"
tfnswapi_get = function(api, params = NULL, key = tfnswapi_get_api_key()) {

checkmate::assert_string(api)
checkmate::assert_list(params, names = "unique", any.missing = FALSE, null.ok = TRUE)

path = c("v1", api)
url = httr::modify_url(tfnsw_base_api_url, path = path, query = params)

headers = httr::add_headers("Authorization" = paste0("apikey ", key),
"Accept" = "application/json")
path = glue::glue("v1/{api}")

resp <- httr::GET(url, ua, headers)

if (httr::http_type(resp) != "application/json") {
stop("API did not return json", call. = FALSE)
}
response = tfnswapi_get_response(path, params, key)

parsed <- jsonlite::fromJSON(httr::content(resp, "text"), simplifyVector = FALSE)
response$content = switch(httr::http_type(response),
"application/json" = parse_json_response(response),
"application/x-google-protobuf" = parse_ggprotobuf_response(response),
stop("Don't know how to parse ", httr::http_type(response)))

if (httr::status_code(resp) != 200) {
if (httr::status_code(response) != 200) {
stop(
sprintf(
"TfNSW API request failed [%s]\n%s\n<%s>\n<%s>",
httr::status_code(resp),
httr::status_code(response),
parsed$ErrorDetails$Message,
parsed$ErrorDetails$RequestMethod,
parsed$ErrorDetails$RequestedUrl
Expand All @@ -55,12 +49,34 @@ tfnswapi_get = function(api, params = NULL, key = tfnswapi_get_api_key()) {
}

structure(
list(
content = parsed,
path = path,
response = resp
),
response,
class = "tfnswapi"
)
}

#' @param path Character.
#' @rdname tfnswapi_get
#' @export
tfnswapi_get_response = function(path, params = NULL, key = tfnswapi_get_api_key()) {

checkmate::assert_string(path)
checkmate::assert_list(params, names = "unique", any.missing = FALSE, null.ok = TRUE)

url = httr::modify_url(tfnsw_base_api_url, path = path, query = params)

headers = httr::add_headers("Authorization" = paste0("apikey ", key))

# return response
httr::GET(url, ua, headers)
}

parse_json_response = function(response) {
jsonlite::fromJSON(httr::content(response, "text"), simplifyVector = FALSE)
}

parse_ggprotobuf_response = function(response) {
FeedMessage = RProtoBuf::read(descriptor = transit_realtime.FeedMessage,
input = response$content)
json = RProtoBuf::toJSON(FeedMessage)
lst = jsonlite::fromJSON(json)
}
7 changes: 7 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.onLoad <- function(libname, pkgname) {

proto.dir <- system.file("proto", package = "tfnswapi", mustWork = T)
proto.file <- file.path(proto.dir, "gtfs-realtime.proto")
RProtoBuf::readProtoFiles(proto.file)

}
Loading

0 comments on commit 4942019

Please sign in to comment.