From 216ac8462732ce8b2065ebae19fd9a7a667f547c Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 9 Aug 2022 17:42:01 +0100 Subject: [PATCH] addin failed auth handling --- .../contents.xcworkspacedata | 2 +- .../xcschemes/Music Tools.xcscheme | 12 ++-- Mixonomer/Model/LiveUser.swift | 71 +++++++++++-------- Mixonomer/Model/Playlist.swift | 6 +- Mixonomer/Network/AuthApi.swift | 35 +-------- Mixonomer/Views/LoginScreen.swift | 8 +-- .../Views/Playlist/AddPlaylistSheet.swift | 19 +++-- Mixonomer/Views/Playlist/PlaylistList.swift | 7 ++ Mixonomer/Views/Playlist/PlaylistRow.swift | 10 ++- Mixonomer/Views/Playlist/PlaylistView.swift | 44 +++++++++--- Mixonomer/Views/Tag/AddTagSheet.swift | 19 +++-- Mixonomer/Views/Tag/TagList.swift | 8 +++ Mixonomer/Views/Tag/TagRow.swift | 10 ++- Mixonomer/Views/Tag/TagView.swift | 46 +++++++++--- 14 files changed, 191 insertions(+), 106 deletions(-) diff --git a/Mixonomer.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Mixonomer.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 605273a..919434a 100644 --- a/Mixonomer.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/Mixonomer.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:"> diff --git a/Mixonomer.xcodeproj/xcshareddata/xcschemes/Music Tools.xcscheme b/Mixonomer.xcodeproj/xcshareddata/xcschemes/Music Tools.xcscheme index 3eab776..eb9103a 100644 --- a/Mixonomer.xcodeproj/xcshareddata/xcschemes/Music Tools.xcscheme +++ b/Mixonomer.xcodeproj/xcshareddata/xcschemes/Music Tools.xcscheme @@ -15,7 +15,7 @@ @@ -33,7 +33,7 @@ @@ -43,7 +43,7 @@ @@ -65,7 +65,7 @@ @@ -74,7 +74,7 @@ + isEnabled = "NO"> @@ -89,7 +89,7 @@ diff --git a/Mixonomer/Model/LiveUser.swift b/Mixonomer/Model/LiveUser.swift index d6d7ae2..8409cf0 100644 --- a/Mixonomer/Model/LiveUser.swift +++ b/Mixonomer/Model/LiveUser.swift @@ -45,8 +45,10 @@ class LiveUser: ObservableObject { let api = PlaylistApi.getPlaylists RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in - switch response.result { - case .success: + self.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: guard let data = response.data else { fatalError("error getting playlists") @@ -70,14 +72,8 @@ class LiveUser: ObservableObject { print("error encoding playlists: \(error)") } - case .failure: - - switch response.response?.statusCode { - case 401: - self.loggedIn = false - case _: - print("error") - } + case _: + break } } } @@ -87,31 +83,50 @@ class LiveUser: ObservableObject { let api = TagApi.getTags RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in + + self.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: - guard let data = response.data else { - fatalError("error getting tags") - } + guard let data = response.data else { + fatalError("error getting tags") + } - guard let json = try? JSON(data: data) else { - fatalError("error parsing reponse") - } + guard let json = try? JSON(data: data) else { + fatalError("error parsing reponse") + } + + let tags = json["tags"].arrayValue - let tags = json["tags"].arrayValue - - // update state - self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) - - self.isRefreshingTags = false - - let encoder = JSONEncoder() - do { - UserDefaults.standard.set(String(data: try encoder.encode(tags), encoding: .utf8), forKey: "tags") - } catch { - print("error encoding tags: \(error)") + // update state + self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) + + self.isRefreshingTags = false + + let encoder = JSONEncoder() + do { + UserDefaults.standard.set(String(data: try encoder.encode(tags), encoding: .utf8), forKey: "tags") + } catch { + print("error encoding tags: \(error)") + } + + case _: + break } } } + func checkNetworkResponse(response: AFDataResponse) { + + switch response.response?.statusCode { + case 401: + self.loggedIn = false + case _: + print("error making request") + } + } + func loadUserDefaults() -> LiveUser { let defaults = UserDefaults.standard let decoder = JSONDecoder() diff --git a/Mixonomer/Model/Playlist.swift b/Mixonomer/Model/Playlist.swift index 933bc1b..87f3b07 100644 --- a/Mixonomer/Model/Playlist.swift +++ b/Mixonomer/Model/Playlist.swift @@ -117,10 +117,10 @@ class Playlist: Identifiable, Equatable, Codable, ObservableObject { func updatePlaylist(updates: JSON) { let api = PlaylistApi.updatePlaylist(name: self.name, updates: updates) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in - switch response.result { - case .success: + switch response.response?.statusCode { + case 200, 201: break - case .failure: + case _: debugPrint("error: \(self.name), \(updates)") } } diff --git a/Mixonomer/Network/AuthApi.swift b/Mixonomer/Network/AuthApi.swift index 0cfa57c..f792eb8 100644 --- a/Mixonomer/Network/AuthApi.swift +++ b/Mixonomer/Network/AuthApi.swift @@ -12,7 +12,7 @@ import SwiftyJSON public enum AuthApi { - case token(username: String, password: String) + case token(username: String, password: String, expiry: Int) } extension AuthApi: ApiRequest { @@ -37,8 +37,8 @@ extension AuthApi: ApiRequest { var parameters: JSON? { switch self { - case .token(let username, let password): - return JSON(["username": username, "password": password]) + case .token(let username, let password, let expiry): + return JSON(["username": username, "password": password, "expiry": expiry]) } } @@ -56,33 +56,4 @@ extension AuthApi: ApiRequest { var authMethod: AuthMethod? { return AuthMethod.none } - - static func fromJSON(playlist: Data) -> Playlist? { - - let decoder = JSONDecoder() - do { - let playlist = try decoder.decode(Playlist.self, from: playlist) - return playlist - } catch { - print(error) - } - return nil - } - - static func fromJSON(playlist: JSON) -> Playlist? { - - let _json = playlist.rawString()?.data(using: .utf8) - - if let data = _json { - let decoder = JSONDecoder() - do { - let playlist = try decoder.decode(Playlist.self, from: data) - return playlist - } catch { - print(error) - } - } - print(playlist) - return nil - } } diff --git a/Mixonomer/Views/LoginScreen.swift b/Mixonomer/Views/LoginScreen.swift index 01dd1a5..6abe670 100644 --- a/Mixonomer/Views/LoginScreen.swift +++ b/Mixonomer/Views/LoginScreen.swift @@ -41,13 +41,13 @@ struct LoginScreen: View { keychain["username"] = username // keychain["password"] = password - let api = AuthApi.token(username: username, password: password) + let api = AuthApi.token(username: username, password: password, expiry: 604800) RequestBuilder.buildRequest(apiRequest: api) .validate() .responseJSON { response in - switch response.result { - case .success: + switch response.response?.statusCode { + case 200, 201: guard let data = response.data else { fatalError("error getting token") @@ -62,7 +62,7 @@ struct LoginScreen: View { keychain["jwt"] = token self.liveUser.loggedIn = true - case .failure: + case _: keychain["username"] = nil keychain["password"] = nil diff --git a/Mixonomer/Views/Playlist/AddPlaylistSheet.swift b/Mixonomer/Views/Playlist/AddPlaylistSheet.swift index bebb22f..4298d5b 100644 --- a/Mixonomer/Views/Playlist/AddPlaylistSheet.swift +++ b/Mixonomer/Views/Playlist/AddPlaylistSheet.swift @@ -11,6 +11,7 @@ import SwiftyJSON struct AddPlaylistSheet: View { + @EnvironmentObject var liveUser: LiveUser @State private var selectedType = 0 @State private var name = "" @State private var errorMessage = "" @@ -92,11 +93,21 @@ struct AddPlaylistSheet: View { let api = PlaylistApi.newPlaylist(name: self.name, type: PlaylistType(rawValue: selectedType) ?? .defaultPlaylist) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in - self.playlists.append(playlist) - self.playlists = self.playlists.sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) - self.isLoading = false - self.presentationMode.wrappedValue.dismiss() + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: + + self.playlists.append(playlist) + self.playlists = self.playlists.sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) + + self.isLoading = false + self.presentationMode.wrappedValue.dismiss() + + case _: + break + } } } } diff --git a/Mixonomer/Views/Playlist/PlaylistList.swift b/Mixonomer/Views/Playlist/PlaylistList.swift index 7c6d044..c217ce3 100644 --- a/Mixonomer/Views/Playlist/PlaylistList.swift +++ b/Mixonomer/Views/Playlist/PlaylistList.swift @@ -25,7 +25,14 @@ struct PlaylistList: View { indexSet.forEach { index in let api = PlaylistApi.deletePlaylist(name: self.liveUser.playlists[index].name) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in + self.liveUser.checkNetworkResponse(response: response) + switch response.response?.statusCode { + case 200, 201: + break + case _: + break + } } } diff --git a/Mixonomer/Views/Playlist/PlaylistRow.swift b/Mixonomer/Views/Playlist/PlaylistRow.swift index edbd0bc..39f0657 100644 --- a/Mixonomer/Views/Playlist/PlaylistRow.swift +++ b/Mixonomer/Views/Playlist/PlaylistRow.swift @@ -11,6 +11,7 @@ import SwiftyJSON struct PlaylistRow: View { + @EnvironmentObject var liveUser: LiveUser @Binding var playlist: Playlist @State private var showingNetworkError = false @@ -29,10 +30,13 @@ struct PlaylistRow: View { RequestBuilder.buildRequest(apiRequest: api) .validate() .responseJSON{ response in - switch response.result { - case .success: + + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: break - case .failure: + case _: self.showingNetworkError = true } } diff --git a/Mixonomer/Views/Playlist/PlaylistView.swift b/Mixonomer/Views/Playlist/PlaylistView.swift index 8030fd3..638619c 100644 --- a/Mixonomer/Views/Playlist/PlaylistView.swift +++ b/Mixonomer/Views/Playlist/PlaylistView.swift @@ -12,6 +12,7 @@ import SwiftUICharts struct PlaylistView: View { + @EnvironmentObject var liveUser: LiveUser @Binding var playlist: Playlist @State private var showingSheet = false @@ -214,10 +215,13 @@ struct PlaylistView: View { RequestBuilder.buildRequest(apiRequest: api) .validate() .responseJSON{ response in - switch response.result { - case .success: + + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: break - case .failure: + case _: self.showingNetworkError = true } } @@ -228,6 +232,14 @@ struct PlaylistView: View { let api = PlaylistApi.refreshStats(name: playlist.name) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: + break + case _: + break + } } //TODO: do better error checking } @@ -241,10 +253,13 @@ struct PlaylistView: View { func updatePlaylist(updates: JSON) { let api = PlaylistApi.updatePlaylist(name: playlist.name, updates: updates) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in - switch response.result { - case .success: + + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: debugPrint("success") - case .failure: + case _: debugPrint("error") } } @@ -254,11 +269,22 @@ struct PlaylistView: View { func refreshPlaylist() { let api = PlaylistApi.getPlaylist(name: self.playlist.name) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in - guard let data = response.data else { - fatalError("error getting playlist") + + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: + + guard let data = response.data else { + fatalError("error getting playlist") + } + + self.playlist = PlaylistApi.fromJSON(playlist: data)! + + case _: + break } - self.playlist = PlaylistApi.fromJSON(playlist: data)! self.isRefreshing = false } //TODO: do better error checking diff --git a/Mixonomer/Views/Tag/AddTagSheet.swift b/Mixonomer/Views/Tag/AddTagSheet.swift index 776ae56..b2263ed 100644 --- a/Mixonomer/Views/Tag/AddTagSheet.swift +++ b/Mixonomer/Views/Tag/AddTagSheet.swift @@ -11,6 +11,7 @@ import SwiftyJSON struct AddTagSheet: View { + @EnvironmentObject var liveUser: LiveUser @State private var name = "" @State private var errorMessage = "" @State private var isLoading = false @@ -74,11 +75,21 @@ struct AddTagSheet: View { isLoading = true let api = TagApi.newTag(tag_id: tag_id) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in - self.tags.append(tag) - self.tags = self.tags.sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) - self.isLoading = false - self.presentationMode.wrappedValue.dismiss() + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: + + self.tags.append(tag) + self.tags = self.tags.sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) + + self.isLoading = false + self.presentationMode.wrappedValue.dismiss() + + case _: + break + } } } } diff --git a/Mixonomer/Views/Tag/TagList.swift b/Mixonomer/Views/Tag/TagList.swift index b3c6513..c4ffb3d 100644 --- a/Mixonomer/Views/Tag/TagList.swift +++ b/Mixonomer/Views/Tag/TagList.swift @@ -26,6 +26,14 @@ struct TagList: View { let api = TagApi.deleteTag(tag_id: self.liveUser.tags[index].tag_id) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: + break + case _: + break + } } } diff --git a/Mixonomer/Views/Tag/TagRow.swift b/Mixonomer/Views/Tag/TagRow.swift index fcd586c..5f0b772 100644 --- a/Mixonomer/Views/Tag/TagRow.swift +++ b/Mixonomer/Views/Tag/TagRow.swift @@ -11,6 +11,7 @@ import SwiftyJSON struct TagRow: View { + @EnvironmentObject var liveUser: LiveUser @Binding var tag: Tag @State private var showingNetworkError = false @@ -29,10 +30,13 @@ struct TagRow: View { RequestBuilder.buildRequest(apiRequest: api) .validate() .responseJSON{ response in - switch response.result { - case .success: + + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: break - case .failure: + case _: self.showingNetworkError = true } } diff --git a/Mixonomer/Views/Tag/TagView.swift b/Mixonomer/Views/Tag/TagView.swift index 5a1c330..ff3c780 100644 --- a/Mixonomer/Views/Tag/TagView.swift +++ b/Mixonomer/Views/Tag/TagView.swift @@ -12,6 +12,7 @@ import SwiftUICharts struct TagView: View { + @EnvironmentObject var liveUser: LiveUser @Binding var tag: Tag @State private var isRefreshing = false @@ -119,6 +120,14 @@ struct TagView: View { let api = TagApi.runTag(tag_id: tag.tag_id) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: + break + case _: + break + } } //TODO: do better error checking } @@ -127,6 +136,14 @@ struct TagView: View { let api = TagApi.updateTag(tag_id: tag.tag_id, updates: updates) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: + break + case _: + break + } } //TODO: do better error checking } @@ -134,17 +151,28 @@ struct TagView: View { func refreshTag() { let api = TagApi.getTag(tag_id: self.tag.tag_id) RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in - guard let data = response.data else { - fatalError("error getting tag") + + self.liveUser.checkNetworkResponse(response: response) + + switch response.response?.statusCode { + case 200, 201: + + guard let data = response.data else { + fatalError("error getting tag") + } + + guard let json = try? JSON(data: data) else { + fatalError("error parsing reponse") + } + let _tag = TagApi.fromJSON(tag: json["tag"]) + if let tag = _tag { + self.tag = tag + } + + case _: + break } - guard let json = try? JSON(data: data) else { - fatalError("error parsing reponse") - } - let _tag = TagApi.fromJSON(tag: json["tag"]) - if let tag = _tag { - self.tag = tag - } self.isRefreshing = false } //TODO: do better error checking