Skip to content

Commit 55a61ad

Browse files
authored
Merge pull request #74 from makinosp/develop
Develop
2 parents 5e2adcc + bf2face commit 55a61ad

File tree

7 files changed

+60
-24
lines changed

7 files changed

+60
-24
lines changed

Sources/VRCKit/APIClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class APIClient {
4747
func encodeAuthorization(_ username: String, _ password: String) throws -> String {
4848
let authString = "\(username):\(password)"
4949
guard let payload = authString.data(using: .utf8) else {
50-
throw VRCKitError.unexpectedError
50+
throw VRCKitError.unexpected
5151
}
5252
return "Basic \(payload.base64EncodedString())"
5353
}
@@ -97,7 +97,7 @@ public final class APIClient {
9797

9898
let (data, response) = try await URLSession.shared.data(for: request)
9999
guard let response = response as? HTTPURLResponse else {
100-
throw VRCKitError.invalidResponseError
100+
throw VRCKitError.invalidResponse
101101
}
102102
return (data, response)
103103
}

Sources/VRCKit/Errors.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99

1010
/// An enumeration that represents various errors that can occur in the VRCKit framework.
1111
/// Conforms to the `Error` and `LocalizedError` protocols for better error handling and localization.
12-
public enum VRCKitError: Error, LocalizedError {
12+
public enum VRCKitError: Error, LocalizedError, Equatable {
1313
private typealias RawValue = String
1414

1515
/// Represents an error from the API with details.
@@ -19,32 +19,30 @@ public enum VRCKitError: Error, LocalizedError {
1919
case clientDeallocated
2020

2121
/// Represents an error indicating an invalid response was received.
22-
case invalidResponseError
22+
case invalidResponse
2323

2424
/// Represents an error indicating an invalid request with additional details.
2525
case invalidRequest(_ details: String)
2626

27+
/// Represents an error indicating an authentication failure.
28+
case unauthorized
29+
2730
/// Represents an unexpected error.
28-
case unexpectedError
31+
case unexpected
2932

3033
/// Represents an url error.
3134
case urlError
3235

3336
/// Provides a localized description of the error.
3437
public var errorDescription: String? {
3538
switch self {
36-
case .apiError:
37-
"API Error"
38-
case .clientDeallocated:
39-
"Client Deallocated"
40-
case .invalidResponseError:
41-
"Invalid Response Error"
42-
case .invalidRequest:
43-
"Invalid Request"
44-
case .unexpectedError:
45-
"Unexpected Error"
46-
case .urlError:
47-
"URL Error"
39+
case .apiError: "API Error"
40+
case .clientDeallocated: "Client Deallocated"
41+
case .invalidResponse: "Invalid Response"
42+
case .invalidRequest: "Invalid Request"
43+
case .unauthorized: "Unauthorized"
44+
case .unexpected: "Unexpected"
45+
case .urlError: "URL Error"
4846
}
4947
}
5048

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// FavoriteWorldModel.swift
3+
// VRCKit
4+
//
5+
// Created by makinosp on 2024/09/08.
6+
//
7+
8+
import Foundation
9+
10+
struct AnyCodable: Codable {}
11+
12+
public struct FavoriteWorldWrapper {
13+
public let worlds: [World]
14+
}
15+
16+
extension FavoriteWorldWrapper: Decodable {
17+
public init(from decoder: any Decoder) throws {
18+
var container = try decoder.unkeyedContainer()
19+
var worlds: [World] = []
20+
while !container.isAtEnd {
21+
if let world = try? container.decode(World.self) {
22+
worlds.append(world)
23+
} else {
24+
try container.decode(AnyCodable.self)
25+
}
26+
}
27+
self.worlds = worlds
28+
}
29+
}

Sources/VRCKit/PreviewServices/PreviewDataProvider.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public final class PreviewDataProvider {
113113
)
114114
}
115115

116-
private static func generateFriend(
116+
public static func generateFriend(
117117
id: UUID,
118118
location: Location,
119119
status: UserStatus
@@ -139,7 +139,7 @@ public final class PreviewDataProvider {
139139
)
140140
}
141141

142-
private static func generateUserDetail(
142+
public static func generateUserDetail(
143143
id: UUID,
144144
location: Location,
145145
state: User.State,
@@ -171,7 +171,7 @@ public final class PreviewDataProvider {
171171
)
172172
}
173173

174-
private static func generateInstance(worldId: UUID, instanceId: Int) -> Instance {
174+
public static func generateInstance(worldId: UUID, instanceId: Int) -> Instance {
175175
Instance(
176176
active: true,
177177
capacity: 32,
@@ -197,7 +197,11 @@ public final class PreviewDataProvider {
197197
)
198198
}
199199

200-
private static func generateWorld(worldId: UUID) -> World {
200+
public static func generateInstance() -> Instance {
201+
generateInstance(worldId: UUID(), instanceId: 0)
202+
}
203+
204+
public static func generateWorld(worldId: UUID) -> World {
201205
World(
202206
id: "wrld_\(worldId.uuidString)",
203207
name: "DummyWorld",

Sources/VRCKit/Services/AuthenticationService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class AuthenticationService: APIService, AuthenticationServiceProtocol {
2929
} catch _ as DecodingError {
3030
let result: RequiresTwoFactorAuthResponse = try Serializer.shared.decode(response.data)
3131
guard let requires = result.requires else {
32-
throw VRCKitError.unexpectedError
32+
throw VRCKitError.unexpected
3333
}
3434
return requires
3535
}

Sources/VRCKit/Services/WorldService.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class WorldService: APIService, WorldServiceProtocol {
1616
public func fetchFavoritedWorlds() async throws -> [World] {
1717
let path = "worlds/favorites"
1818
let response = try await client.request(path: path, method: .get)
19-
return try Serializer.shared.decode(response.data)
19+
let favoriteWorldWrapper: FavoriteWorldWrapper = try Serializer.shared.decode(response.data)
20+
return favoriteWorldWrapper.worlds
2021
}
2122
}

Sources/VRCKit/Utils/Serializer.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ final class Serializer {
3030
} catch let error as DecodingError {
3131
do {
3232
let errorResponse = try decoder.decode(ErrorResponse.self, from: data)
33-
throw VRCKitError.apiError(errorResponse.error.message)
33+
if errorResponse.error.statusCode == 401 {
34+
throw VRCKitError.unauthorized
35+
} else {
36+
throw VRCKitError.apiError(errorResponse.error.message)
37+
}
3438
} catch _ as DecodingError {
3539
// for debug
3640
print(type(of: T.self))

0 commit comments

Comments
 (0)