addin failed auth handling

This commit is contained in:
andy 2022-08-09 17:42:01 +01:00
parent 4f2e0f245c
commit 216ac84627
14 changed files with 191 additions and 106 deletions

View File

@ -2,6 +2,6 @@
<Workspace
version = "1.0">
<FileRef
location = "self:/Users/andy/dev/swift/Music-Tools-iOS/Mixonomer.xcodeproj">
location = "self:">
</FileRef>
</Workspace>

View File

@ -15,7 +15,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
BuildableName = "Music Tools.app"
BuildableName = "Mixonomer.app"
BlueprintName = "Mixonomer"
ReferencedContainer = "container:Mixonomer.xcodeproj">
</BuildableReference>
@ -33,7 +33,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9EA691C23F9A54B0012C3E8"
BuildableName = "Music ToolsTests.xctest"
BuildableName = "MixonomerTests.xctest"
BlueprintName = "MixonomerTests"
ReferencedContainer = "container:Mixonomer.xcodeproj">
</BuildableReference>
@ -43,7 +43,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9EA692723F9A54B0012C3E8"
BuildableName = "Music ToolsUITests.xctest"
BuildableName = "MixonomerUITests.xctest"
BlueprintName = "MixonomerUITests"
ReferencedContainer = "container:Mixonomer.xcodeproj">
</BuildableReference>
@ -65,7 +65,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
BuildableName = "Music Tools.app"
BuildableName = "Mixonomer.app"
BlueprintName = "Mixonomer"
ReferencedContainer = "container:Mixonomer.xcodeproj">
</BuildableReference>
@ -74,7 +74,7 @@
<EnvironmentVariable
key = "MTOOLS_SERVER"
value = "http://127.0.0.1:5000/"
isEnabled = "YES">
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
@ -89,7 +89,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
BuildableName = "Music Tools.app"
BuildableName = "Mixonomer.app"
BlueprintName = "Mixonomer"
ReferencedContainer = "container:Mixonomer.xcodeproj">
</BuildableReference>

View File

@ -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<Any>) {
switch response.response?.statusCode {
case 401:
self.loggedIn = false
case _:
print("error making request")
}
}
func loadUserDefaults() -> LiveUser {
let defaults = UserDefaults.standard
let decoder = JSONDecoder()

View File

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

View File

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

View File

@ -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

View File

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

View File

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

View File

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

View File

@ -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

View File

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

View File

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

View File

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

View File

@ -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