Skip to content
Open
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
13 changes: 13 additions & 0 deletions PackageR.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
3 changes: 3 additions & 0 deletions spotify/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
^spotify\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
1 change: 1 addition & 0 deletions spotify/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.Rproj.user
14 changes: 14 additions & 0 deletions spotify/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Package: spotify
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
595 changes: 595 additions & 0 deletions spotify/LICENSE.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions spotify/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(check_spotify_credentials)
export(spotify_search_artist)
export(spotify_token)
import(httr)
19 changes: 19 additions & 0 deletions spotify/R/Q1_spotify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Yetkileri kontrol eder
#'
#' @return SPOTIFY_ID ve SPOTIFY_SECRET'ı kontrol ederek true değeri ya da hata mesajı döndürür
#' @export
check_spotify_credentials <- function() {
api_id <- Sys.getenv("SPOTIFY_ID")

if(api_id == "") {
stop("Spotify API ID boş. Lütfen .Renviron dosyasını doğru şekilde düzenleyiniz.")
}

api_secret <- Sys.getenv("SPOTIFY_SECRET")

if(api_secret == "") {
stop("Spotify API Secret boş. Lütfen .Renviron dosyasını doğru şekilde düzenleyiniz.")
}

return(TRUE)
}
80 changes: 80 additions & 0 deletions spotify/R/Q2_spotify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#' Token Döndürür
#'
#' @return Spotify ID ve SECRET bilgilerini doğrulayarak token döndürür
#' @export
#' @import httr
spotify_token <- function() {
body <- list(
grant_type = "client_credentials",
client_id = (Sys.getenv("SPOTIFY_ID")),
client_secret = (Sys.getenv("SPOTIFY_SECRET"))
)

endpoint_uri <- "https://accounts.spotify.com/api/token"

check_spotify_credentials()

response <- POST(
url = endpoint_uri,
body = body,
encode = "form",
add_headers("Content-Type" = "application/x-www-form-urlencoded")
)

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)
}



#' Artist ID döndürür
#'
#' @param artist_name Artist adı girilir
#' @return Artist adına göre yakın isimdeki ID'leri döndürür
#' @export
spotify_search_artist <- function(artist_name) {
if(!is.character(artist_name)) stop("Artist name must be character type.");

search_url <- paste0(
"https://api.spotify.com/v1/search?q=", URLencode(artist_name),
"&type=artist&limit=",5)
token <- spotify_token()

check_spotify_credentials()

response <- GET(
url = search_url,
add_headers("Authorization" = token$token)
)

status_code <- status_code(response)

if (status_code == 200) {
search_result <- content(response, type = "application/json")
artists <- search_result$artists$items[seq_len(5)]

search_results <- data.frame(
artist = sapply(artists, function(x) x$name),
id = sapply(artists, function(x) x$id)
)

result <- list(
status_code = status_code,
search_results = search_results
)

return(result)
}
else {
error <- content(response)
return(error)
}
}
14 changes: 14 additions & 0 deletions spotify/man/check_spotify_credentials.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions spotify/man/spotify_search_artist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions spotify/man/spotify_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions spotify/spotify.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
12 changes: 12 additions & 0 deletions spotify/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(spotify)

test_check("spotify")
16 changes: 16 additions & 0 deletions spotify/tests/testthat/test-Q1_spotify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
check_result <- check_spotify_credentials()

#Test 1.1
test_that("Global Workspace'de check_spotify_credentials bulunmalı", {
expect_true(exists("check_spotify_credentials"))
})

#Test 1.2
test_that("check_spotify_credentials adlı değişkenin tipi “function” olmalı.", {
testthat::expect_true(is.function(check_spotify_credentials))
})

#Test 1.3
test_that("check_spotify_credentials() çağrıldığında döndürdüğü çıktı true olmalı", {
testthat::expect_true(isTRUE(check_result))
})
50 changes: 50 additions & 0 deletions spotify/tests/testthat/test-Q2_spotify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#----------------------------SPOTIFY_TOKEN TESTLERİ----------------------------

token_result <- spotify_token()

#Test 1.1
test_that("Global Workspace’de spotify_token adlı bir değişken olmalı.",{
testthat::expect_true(exists("spotify_token"))
})

#Test 1.2
test_that("spotify_token adlı değişkenin tipi “function” olmalı.", {
testthat::expect_true(is.function(spotify_token))
})

#Test 1.3
test_that("spotify_token() çağrıldığında döndürdüğü çıktı bir liste olmalı", {
testthat::expect_true(is.list(token_result))
})

#Test 1.4
test_that("spotify_token() çağrıldığında döndürdüğü listenin iki elementi olmalı", {
expect_length(token_result, 2)
})

#Test 1.5
test_that("spotify_token() çağrıldığında döndürdüğü listenin ilk elementinin ismi status_code olmalı", {
expect_identical(names(token_result)[1], "status_code")
})




#------------------------SPOTIFY_SEARCH_ARTISTS TESTLERİ------------------------

spotify_search_artist_result <- spotify_search_artist("The Doors")

#Test 1.1
test_that("Global Workspace’de spotify_search_artist adlı bir değişken olmalı.",{
expect_true(exists("spotify_search_artist"))
})

#Test 1.2
test_that("spotify_search_artist adlı değişkenin tipi “function” olmalı.",{
expect_true(is.function(spotify_search_artist))
})

#Test 1.3
test_that("spotify_search_artist() herhangi bir artist ismi ile çağrıldığında döndürdüğü çıktı bir liste olmalı.",{
expect_true(is.list(spotify_search_artist_result))
})