Skip to content

Commit

Permalink
chore: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Jul 6, 2021
1 parent 68da016 commit 8223056
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
35 changes: 18 additions & 17 deletions firstfm/Data/Entities/API/Spotify/SpotifyToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@
import Foundation

struct SpotifyCredentialsResponse: Codable {
var access_token: String
var expires_in: Int
var accessToken: String
var expiresIn: Int
}

func renewSpotifyToken(completion: @escaping (String) -> ()) {
let authURL = "https://accounts.spotify.com/api/token"
let SpotifyAPIToken = Bundle.main.object(forInfoDictionaryKey: "SpotifyAPIToken") as! String

// swiftlint:disable force_cast
let spotifyAPIToken = Bundle.main.object(forInfoDictionaryKey: "SpotifyAPIToken") as! String

if let queryURL = URL(string: authURL) {
var request = URLRequest(url: queryURL)
request.setValue("Basic \(SpotifyAPIToken)",

request.setValue("Basic \(spotifyAPIToken)",
forHTTPHeaderField:"Authorization");

let data : Data = "grant_type=client_credentials".data(using: .utf8)!

request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type");
request.httpBody = data

URLSession.shared.dataTask(with: request , completionHandler : { data, response, error in
do {
if let response = response {
Expand All @@ -46,10 +47,10 @@ func renewSpotifyToken(completion: @escaping (String) -> ()) {
do{
let jsonResponse = try JSONDecoder().decode(SpotifyCredentialsResponse.self, from: data)
let defaults = UserDefaults.standard
defaults.set(jsonResponse.access_token, forKey: "spotify_token")
let expiresAt = Int64(Date().timeIntervalSince1970 * 1000) + Int64(jsonResponse.expires_in)
defaults.set(jsonResponse.accessToken, forKey: "spotify_token")
let expiresAt = Int64(Date().timeIntervalSince1970 * 1000) + Int64(jsonResponse.expiresIn)
defaults.setValue(expiresAt, forKey: "spotify_expires")
completion(jsonResponse.access_token)
completion(jsonResponse.accessToken)
}
}
}
Expand All @@ -66,23 +67,23 @@ func getSpotifyToken(completion: @escaping (String) -> ()) {
let storedToken = defaults.string(forKey: "spotify_token")
let expiresAt = defaults.integer(forKey: "spotify_expires")
let currentTimeSeconds = Int64(Date().timeIntervalSince1970 * 1000)


if storedToken == nil || expiresAt == 0 {
renewSpotifyToken() { renewedToken in
completion(renewedToken)
}

} else {
// We want the refresh token to be valid for at least 30s
if expiresAt != 0 && currentTimeSeconds + 30 > expiresAt {
renewSpotifyToken() { renewedToken in
completion(renewedToken)

}
}
}

if let token = storedToken {
completion(token)
}
Expand Down
36 changes: 18 additions & 18 deletions firstfm/Data/ViewModel/ChartViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class ChartViewModel: ObservableObject {
@Published var artists: [Artist] = []
@Published var tracks: [Track] = []
var isLoading = true


// swiftlint:disable force_cast
let lastFMAPIKey = Bundle.main.object(forInfoDictionaryKey: "LastFMAPIKey") as! String

//
// Artist
//
Expand Down Expand Up @@ -72,19 +73,19 @@ class ChartViewModel: ObservableObject {
}
}.resume()
}

func getImageForArtist(artistName: String, completion: @escaping (String?) -> ()) {
if let encodedArtistName = artistName.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
let queryURLString = "https://api.spotify.com/v1/search?q=\(encodedArtistName)&type=artist&limit=1"
if let queryURL = URL(string: queryURLString) {
var request = URLRequest(url: queryURL)

request.setValue("application/json", forHTTPHeaderField:"Content-Type");

getSpotifyToken() { spotifyToken in
request.setValue("Bearer \(spotifyToken)",
forHTTPHeaderField:"Authorization");

URLSession.shared.dataTask(with: request , completionHandler : { data, response, error in
do {
if let response = response {
Expand All @@ -102,7 +103,7 @@ class ChartViewModel: ObservableObject {
if let data = data {
do{
let jsonResponse = try JSONDecoder().decode(SpotifyArtistSearchResponse.self, from: data)

// TODO: match image sizes
completion(jsonResponse.artists.items[0].images[0].url)
}
Expand All @@ -117,22 +118,22 @@ class ChartViewModel: ObservableObject {
}
}
}


//
// Track
//

func getChartingTracks() {
self.isLoading = true

var request = URLRequest(url: URL(string: "https://ws.audioscrobbler.com/2.0/?format=json")!)
let data : Data = "api_key=\(lastFMAPIKey)&method=chart.getTopTracks&limit=30".data(using: .utf8)!

request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type");
request.httpBody = data

URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
do {
if let response = response {
Expand Down Expand Up @@ -175,13 +176,13 @@ class ChartViewModel: ObservableObject {
}
}.resume()
}

func getImageForTrack(trackName: String, completion: @escaping (String?) -> ()) {
if let encodedTrackName = trackName.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
let queryURLString = "https://api.spotify.com/v1/search?q=\(encodedTrackName)&type=track&limit=1"
if let queryURL = URL(string: queryURLString) {
var request = URLRequest(url: queryURL)

request.setValue("application/json", forHTTPHeaderField:"Content-Type");

getSpotifyToken() { spotifyToken in
Expand All @@ -205,7 +206,7 @@ class ChartViewModel: ObservableObject {
if let data = data {
do{
let jsonResponse = try JSONDecoder().decode(SpotifyTrackSearchResponse.self, from: data)

// TODO: match image sizes
completion(jsonResponse.tracks.items[0].album.images[0].url)
}
Expand All @@ -220,6 +221,5 @@ class ChartViewModel: ObservableObject {
}
}
}

}

}
17 changes: 9 additions & 8 deletions firstfm/Data/ViewModel/UserViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ import Foundation
class ProfileViewModel: ObservableObject {
@Published var user: User?
var isLoading = true


// swiftlint:disable force_cast
let lastFMAPIKey = Bundle.main.object(forInfoDictionaryKey: "LastFMAPIKey") as! String

func getProfile(username: String) {
self.isLoading = true

var request = URLRequest(url: URL(string: "https://ws.audioscrobbler.com/2.0/?format=json")!)

let data : Data = "api_key=\(lastFMAPIKey)&method=user.getInfo&user=\(username)".data(using: .utf8)!

request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type");
request.httpBody = data


URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
do {
if let response = response {
Expand All @@ -41,7 +42,7 @@ class ProfileViewModel: ObservableObject {
if let data = data {
do{
let jsonResponse = try JSONDecoder().decode(UserInfoResponse.self, from: data)

DispatchQueue.main.async {
self.user = jsonResponse.user
self.isLoading = false
Expand Down

0 comments on commit 8223056

Please sign in to comment.