Skip to content

Commit

Permalink
Merge pull request #57 from makinosp/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
makinosp authored Jul 14, 2024
2 parents 4a0d7f4 + c94c376 commit 8bf0c09
Show file tree
Hide file tree
Showing 21 changed files with 440 additions and 208 deletions.
16 changes: 16 additions & 0 deletions Sources/VRCKit/APIServices/APIService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// APIService.swift
//
//
// Created by makinosp on 2024/07/14.
//

import Foundation

public class APIService {
let client: APIClient

public init(client: APIClient) {
self.client = client
}
}
23 changes: 1 addition & 22 deletions Sources/VRCKit/APIServices/AuthenticationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,10 @@

import Foundation

//
// MARK: Authentication API
//

public protocol UserOrRequires {}
extension User: UserOrRequires {}
extension VerifyType: UserOrRequires {}

public protocol AuthenticationServiceProtocol {
func isExists(userId: String) async throws -> Bool
func loginUserInfo() async throws -> UserOrRequires
func verify2FA(verifyType: VerifyType, code: String) async throws -> Bool
func verifyAuthToken() async throws -> Bool
func logout() async throws
}

@available(macOS 12.0, *)
@available(iOS 15.0, *)
public class AuthenticationService: AuthenticationServiceProtocol {
public class AuthenticationService: APIService, AuthenticationServiceProtocol {
private let authPath = "auth"
private let client: APIClient

public init(client: APIClient) {
self.client = client
}

/// Check User Exists
public func isExists(userId: String) async throws -> Bool {
Expand Down
36 changes: 11 additions & 25 deletions Sources/VRCKit/APIServices/FavoriteService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,21 @@

import Foundation

//
// MARK: Favorite API
//

@available(macOS 12.0, *)
@available(iOS 15.0, *)
public struct FavoriteService {
private static let path = "favorites"
public class FavoriteService: APIService, FavoriteServiceProtocol {
private let path = "favorites"

/// Asynchronously retrieves a list of favorite groups from the server.
/// - Parameter client: The API client used to make the network request.
/// - Returns: An array of `FavoriteGroup` objects.
/// - Throws: An error if the network request or decoding of the response fails.
public static func listFavoriteGroups(
_ client: APIClient
) async throws -> [FavoriteGroup] {
public func listFavoriteGroups() async throws -> [FavoriteGroup] {
let path = "favorite/groups"
let response = try await client.request(path: path, method: .get)
return try Serializer.shared.decode(response.data)
}

public static func listFavorites(
_ client: APIClient,
public func listFavorites(
n: Int = 60,
type: FavoriteType,
tag: String? = nil
Expand All @@ -46,22 +38,18 @@ public struct FavoriteService {
}

/// Fetch a list of favorite IDs for each favorite group
public static func fetchFavoriteGroupDetails(
_ client: APIClient,
public func fetchFavoriteGroupDetails(
favoriteGroups: [FavoriteGroup]
) async throws -> [FavoriteDetail] {
var results: [FavoriteDetail] = []
try await withThrowingTaskGroup(of: FavoriteDetail.self) { taskGroup in
for favoriteGroup in favoriteGroups.filter({ $0.type == .friend }) {
taskGroup.addTask {
try await FavoriteDetail(
id: favoriteGroup.id,
favorites: FavoriteService.listFavorites(
client,
type: .friend,
tag: favoriteGroup.name
)
let favorites = try await self.listFavorites(
type: .friend,
tag: favoriteGroup.name
)
return FavoriteDetail(id: favoriteGroup.id, favorites: favorites)
}
}
for try await favoriteGroupDetail in taskGroup {
Expand All @@ -71,8 +59,7 @@ public struct FavoriteService {
return results
}

public static func addFavorite(
_ client: APIClient,
public func addFavorite(
type: FavoriteType,
favoriteId: String,
tag: String
Expand All @@ -84,8 +71,7 @@ public struct FavoriteService {
return try Serializer.shared.decode(response.data)
}

public static func removeFavorite(
_ client: APIClient,
public func removeFavorite(
favoriteId: String
) async throws -> SuccessResponse {
let response = try await client.request(path: path, method: .delete)
Expand Down
18 changes: 1 addition & 17 deletions Sources/VRCKit/APIServices/FriendService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,10 @@

import Foundation

//
// MARK: Friends API
//

public protocol FriendServiceProtocol {
func fetchFriends(offset: Int, n: Int, offline: Bool) async throws -> [Friend]
func fetchFriends(count: Int, offline: Bool) async throws -> [Friend]
func unfriend(id: String) async throws
func friendsGroupedByLocation(_ friends: [Friend]) -> [FriendsLocation]
}

@available(macOS 12.0, *)
@available(iOS 15.0, *)
public class FriendService: FriendServiceProtocol {
public class FriendService: APIService, FriendServiceProtocol {
private let path = "auth/user/friends"
private let client: APIClient

public init(client: APIClient) {
self.client = client
}

/// List information about friends.
public func fetchFriends(offset: Int, n: Int = 60, offline: Bool) async throws -> [Friend] {
Expand Down
16 changes: 1 addition & 15 deletions Sources/VRCKit/APIServices/InstanceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@

import Foundation

//
// MARK: Instance API
//

public protocol InstanceServiceProtocol {
func fetchInstance(worldId: String, instanceId: String) async throws -> Instance
func fetchInstance(location: String) async throws -> Instance
}

@available(macOS 12.0, *)
@available(iOS 15.0, *)
public class InstanceService {
public class InstanceService: APIService, InstanceServiceProtocol {
let path = "instances"
var client: APIClient

public init(client: APIClient) {
self.client = client
}

/// Fetches an instance of a world using the specified world ID and instance ID.
/// - Parameters:
Expand Down
7 changes: 3 additions & 4 deletions Sources/VRCKit/APIServices/UserNoteService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import Foundation

@available(macOS 12.0, *)
@available(iOS 15.0, *)
public struct UserNoteService {
static let path = "userNotes"
public class UserNoteService: APIService, UserNoteServiceProtocol {
let path = "userNotes"

/// Update user's note
public static func updateUserNote(
_ client: APIClient,
public func updateUserNote(
targetUserId: String,
note: String
) async throws -> UserNoteResponse {
Expand Down
18 changes: 4 additions & 14 deletions Sources/VRCKit/APIServices/UserService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,19 @@

import Foundation

//
// MARK: User API
//

@available(macOS 12.0, *)
@available(iOS 15.0, *)
public struct UserService {
static let path = "users"
public class UserService: APIService, UserServiceProtocol {
let path = "users"

/// Fetch a user
public static func fetchUser(
_ client: APIClient,
userId: String
) async throws -> UserDetail {
public func fetchUser(userId: String) async throws -> UserDetail {
let response = try await client.request(path: "\(path)/\(userId)", method: .get)
return try Serializer.shared.decode(response.data)
}

/// Update user
public static func updateUser(
_ client: APIClient,
id: String
) async throws -> User {
public func updateUser(id: String) async throws -> User {
let response = try await client.request(path: "\(path)/\(id)", method: .put)
return try Serializer.shared.decode(response.data)
}
Expand Down
10 changes: 3 additions & 7 deletions Sources/VRCKit/APIServices/WorldService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@

import Foundation

//
// MARK: World API
//

@available(macOS 12.0, *)
@available(iOS 15.0, *)
public struct WorldService {
static let path = "worlds"
public class WorldService: APIService {
let path = "worlds"

public static func fetchWorld(_ client: APIClient, worldId: String) async throws -> World {
public func fetchWorld(worldId: String) async throws -> World {
let response = try await client.request(path: "\(path)/\(worldId)", method: .get)
return try Serializer.shared.decode(response.data)
}
Expand Down
35 changes: 2 additions & 33 deletions Sources/VRCKit/PreviewServices/AuthenticationPreviewService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,9 @@

import Foundation

public class AuthenticationPreviewService: AuthenticationService {
public final class AuthenticationPreviewService: AuthenticationService {
override public func loginUserInfo() async throws -> UserOrRequires {
User(
activeFriends: [],
allowAvatarCopying: false,
bio: "This is the demo user.",
bioLinks: ["https://example.com"],
currentAvatar: "",
currentAvatarAssetUrl: "",
currentAvatarImageUrl: "",
currentAvatarThumbnailImageUrl: "",
dateJoined: "2024/07/01",
displayName: "Demo user",
friendKey: "",
friends: [],
homeLocation: "",
id: "usr_\(UUID().uuidString)",
isFriend: false,
lastActivity: Date(),
lastLogin: Date(),
lastPlatform: "standalonewindows",
offlineFriends: [],
onlineFriends: [],
pastDisplayNames: [],
profilePicOverride: nil,
state: .active,
status: .active,
statusDescription: "status",
tags: [],
twoFactorAuthEnabled: true,
userIcon: nil,
userLanguage: nil,
userLanguageCode: nil
)
PreviewDataProvider.shared.previewUser
}

/// Logout
Expand Down
52 changes: 52 additions & 0 deletions Sources/VRCKit/PreviewServices/FavoritePreviewService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// FavoritePreviewService.swift
//
//
// Created by makinosp on 2024/07/13.
//

import Foundation

public final class FavoritePreviewService: FavoriteService {
override public func listFavoriteGroups() async throws -> [FavoriteGroup] {
[
FavoriteGroup(
id: "fvgrp_\(UUID().uuidString)",
displayName: "DemoGroup",
name: "group_1",
tags: [],
type: .friend,
visibility: "private"
)
]
}

override public func listFavorites(
n: Int = 60,
type: FavoriteType,
tag: String? = nil
) async throws -> [Favorite] {
switch type {
case .friend:
PreviewDataProvider.shared.onlineFriends.prefix(5).map { friend in
Favorite(id: UUID().uuidString, favoriteId: friend.id, tags: ["group_1"], type: .friend)
}
default:
[]
}
}

override public func addFavorite(
type: FavoriteType,
favoriteId: String,
tag: String
) async throws -> Favorite {
Favorite(id: UUID().uuidString, favoriteId: favoriteId, tags: [tag], type: type)
}

override public func removeFavorite(
favoriteId: String
) async throws -> SuccessResponse {
SuccessResponse(success: ResponseMessage(message: "OK", statusCode: 200))
}
}
22 changes: 2 additions & 20 deletions Sources/VRCKit/PreviewServices/FriendPreviewService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,8 @@

import Foundation

public class FriendPreviewService: FriendService {
public final class FriendPreviewService: FriendService {
override public func fetchFriends(offset: Int, n: Int, offline: Bool) async throws -> [Friend] {
let friend = Friend(
bio: nil,
bioLinks: nil,
currentAvatarImageUrl: nil,
currentAvatarThumbnailImageUrl: nil,
displayName: "Dummy friend",
id: "usr_\(UUID().uuidString)",
isFriend: true,
lastLogin: Date(),
lastPlatform: "standalonewindows",
profilePicOverride: nil,
status: .active,
statusDescription: "",
tags: [],
userIcon: nil,
location: "private",
friendKey: ""
)
return [friend]
offline ? PreviewDataProvider.shared.offlineFriends : PreviewDataProvider.shared.onlineFriends
}
}
Loading

0 comments on commit 8bf0c09

Please sign in to comment.