Commit 75131536 authored by Matija Obreza's avatar Matija Obreza
Browse files

Check for OAuth headers before making API calls

parent 650972db
...@@ -76,6 +76,13 @@ authorization <- function(authorization) { ...@@ -76,6 +76,13 @@ authorization <- function(authorization) {
message(paste('Genesys Authorization:', authorization)) message(paste('Genesys Authorization:', authorization))
} }
#' Ensure that environment has OAuth token
check_auth <- function() {
if (is.null(.genesysEnv$Authorization)) {
stop("You must first authorize with Genesys with user_login or client_login.");
}
}
#' Login to Genesys as a user #' Login to Genesys as a user
#' #'
#' The authorization URL will open in a browser, ask the user to grant #' The authorization URL will open in a browser, ask the user to grant
...@@ -149,6 +156,7 @@ client_login <- function() { ...@@ -149,6 +156,7 @@ client_login <- function() {
api_call <- function(path, method = "get") { api_call <- function(path, method = "get") {
check_auth()
resp <- httr::GET(api_url(path), httr::add_headers( resp <- httr::GET(api_url(path), httr::add_headers(
Authorization = .genesysEnv$Authorization Authorization = .genesysEnv$Authorization
) )
...@@ -177,6 +185,7 @@ api_url <- function(path) { ...@@ -177,6 +185,7 @@ api_url <- function(path) {
} }
get <- function(path, query = NULL) { get <- function(path, query = NULL) {
check_auth()
resp <- httr::GET(api_url(path), query = query, httr::add_headers( resp <- httr::GET(api_url(path), query = query, httr::add_headers(
Authorization = .genesysEnv$Authorization Authorization = .genesysEnv$Authorization
)) ))
...@@ -195,6 +204,7 @@ get <- function(path, query = NULL) { ...@@ -195,6 +204,7 @@ get <- function(path, query = NULL) {
#' #'
#' @return httr response #' @return httr response
post <- function(path, query = NULL, body = NULL, content.type = "application/json") { post <- function(path, query = NULL, body = NULL, content.type = "application/json") {
check_auth()
content <- jsonlite::toJSON(body) content <- jsonlite::toJSON(body)
if (! is.null(body) && length(body) == 0) { if (! is.null(body) && length(body) == 0) {
# If body is provided, but has length of 0 # If body is provided, but has length of 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment