diff --git a/coderlog/Labex_Q1.R b/coderlog/Labex_Q1.R new file mode 100644 index 0000000..8f6e578 --- /dev/null +++ b/coderlog/Labex_Q1.R @@ -0,0 +1,30 @@ +library(httr) + +spotify_token <- function() { + endpoint_uri <- "https://accounts.spotify.com/api/token" + + body <- list( + grant_type = "client_credentials", + client_id = (Sys.getenv("SPOTIFY_ID")), + client_secret = (Sys.getenv("SPOTIFY_SECRET")) + ) + + response <- POST( + url = endpoint_uri, + body = body, + add_headers("Content-Type" = "application/x-www-form-urlencoded"), + encode = "form" + ) + + status_code <- status_code(response) + token <- content(response)$access_token + bearer_token <- paste("Bearer", token) + + result <- list( + status_code <- status_code, + token <- bearer_token + ) + + return(result) +} + diff --git a/coderlog/Labex_Q1_tests.R b/coderlog/Labex_Q1_tests.R new file mode 100644 index 0000000..30544d1 --- /dev/null +++ b/coderlog/Labex_Q1_tests.R @@ -0,0 +1,22 @@ +library(testthat) + +rm(list = ls()) +file_path <- "Labex_Q1.R" +source(file_path) + +result <- spotify_token() + +#--------------------------------Test 1.1--------------------------------# +test_that("Global Workspace'de spotify_token adlı bir değişken olmalı", { + expect_true(exists("spotify_token")) +}) + +#--------------------------------Test 1.2--------------------------------# +test_that("spotify_token adlı değişken bir 'function' tipinde olmalı", { + expect_true(is.function(spotify_token)) +}) + +#--------------------------------Test 1.3--------------------------------# +test_that("spotify_token() çağrıldığında döndürdüğü çıktı bir liste olmalı", { + expect_true(is.list(result)) +}) \ No newline at end of file