Skip to content

Commit

Permalink
Merge pull request #79 from makinosp/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
makinosp authored Sep 21, 2024
2 parents e6d09f8 + c35f86a commit c59c5e9
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 67 deletions.
14 changes: 0 additions & 14 deletions Package.resolved

This file was deleted.

19 changes: 1 addition & 18 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,22 @@ import PackageDescription

let package = Package(
name: "VRCKit",
defaultLocalization: "en",
platforms: [
.macOS(.v12),
.iOS(.v15),
.tvOS(.v15),
.watchOS(.v6)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "VRCKit",
targets: ["VRCKit"]
)
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/SimplyDanny/SwiftLintPlugins", from: "0.55.1")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "VRCKit",
dependencies: [],
path: "Sources"
),
.testTarget(
Expand All @@ -37,12 +29,3 @@ let package = Package(
)
]
)

package.targets.forEach {
$0.plugins = [
.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLintPlugins")
]
$0.swiftSettings = [
.enableUpcomingFeature("ForwardTrailingClosures") // SE-0286
]
}
34 changes: 33 additions & 1 deletion Sources/VRCKit/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

public final class APIClient {
typealias HTTPResponse = (data: Data, response: HTTPURLResponse)
Expand Down Expand Up @@ -44,7 +47,7 @@ public final class APIClient {
/// - password: The password associated with the username.
/// - Returns: A Basic Authentication token string.
/// - Throws: `VRCKitError.unexpectedError` if the username and password cannot be converted to UTF-8 data.
func encodeAuthorization(_ username: String, _ password: String) throws -> String {
private func encodeAuthorization(_ username: String, _ password: String) throws -> String {
let authString = "\(username):\(password)"
guard let payload = authString.data(using: .utf8) else {
throw VRCKitError.unexpected
Expand Down Expand Up @@ -95,12 +98,41 @@ public final class APIClient {
request.httpBody = body
}

#if canImport(FoundationNetworking)
return try await requestWithFoundationNetworking(request)
#else
return try await requestWithFoundation(request)
#endif
}

#if canImport(FoundationNetworking)
private func requestWithFoundationNetworking(_ request: URLRequest) async throws -> HTTPResponse {
var requestError: Error?
let httpResponse = await withCheckedContinuation { continuation in
URLSession.shared.dataTask(with: request) { data, urlResponse, error in
guard let data = data, let reponse = urlResponse as? HTTPURLResponse else {
requestError = error
return
}
continuation.resume(returning: (data, reponse))
}
.resume()
}
if let requestError = requestError {
throw requestError
}
return httpResponse
}

#else
private func requestWithFoundation(_ request: URLRequest) async throws -> HTTPResponse {
let (data, response) = try await URLSession.shared.data(for: request)
guard let response = response as? HTTPURLResponse else {
throw VRCKitError.invalidResponse
}
return (data, response)
}
#endif
}

extension APIClient.Method: CustomStringConvertible {
Expand Down
4 changes: 4 additions & 0 deletions Sources/VRCKit/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public enum VRCKitError: Error, LocalizedError, Equatable {
/// Represents an error from the API with details.
case apiError(_ details: String)

/// Represents a bad gatewaty error.
case badGateway

/// Represents an error indicating that the client has been deallocated.
case clientDeallocated

Expand All @@ -37,6 +40,7 @@ public enum VRCKitError: Error, LocalizedError, Equatable {
public var errorDescription: String? {
switch self {
case .apiError: "API Error"
case .badGateway: "Bad Gateway"
case .clientDeallocated: "Client Deallocated"
case .invalidResponse: "Invalid Response"
case .invalidRequest: "Invalid Request"
Expand Down
5 changes: 0 additions & 5 deletions Sources/VRCKit/Models/EditableUserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,3 @@ public extension EditableUserInfo {
tags = detail.tags
}
}

public struct UpdatedUser: Codable {
public let bio: String?
public let statusDescription: String?
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// TrustRank.swift
// TrustRankModel.swift
// VRCKit
//
// Created by makinosp on 2024/08/04.
//

public enum TrustRank {
public enum TrustRank: Equatable {
case trusted, known, user, newUser, visitor, unknown
}

Expand Down
30 changes: 12 additions & 18 deletions Sources/VRCKit/Models/UserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,22 @@ public struct User: ProfileDetailRepresentable {
public let userLanguage: String?
public let userLanguageCode: String?
public let presence: Presence
}

public extension User {
var platform: UserPlatform {
presence.platform
}
var url: URL? {
URL(string: [Const.homeBaseUrl, "user", id].joined(separator: "/"))
}
}

public extension User {
struct DisplayName: Codable, Hashable {
public struct DisplayName: Codable, Hashable {
public let displayName: String
public let updatedAt: Date
}
}

public extension User {
enum State: String, Codable {
public enum State: String, Codable {
/// User is online in VRChat
case online
/// User is online, but not in VRChat
case active
/// User is offline
case offline
}
}

public extension User {
struct Presence: Codable, Hashable {
public struct Presence: Codable, Hashable {
public let groups: [String]
public let id: String
public let instance: String
Expand All @@ -81,6 +66,15 @@ public extension User {
}
}

public extension User {
var platform: UserPlatform {
presence.platform
}
var url: URL? {
URL(string: [Const.homeBaseUrl, "user", id].joined(separator: "/"))
}
}

extension User.Presence {
init() {
groups = []
Expand Down
4 changes: 3 additions & 1 deletion Sources/VRCKit/Models/UserTagModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ extension UserTags: Encodable {
public func encode(to encoder: any Encoder) throws {
var container = encoder.unkeyedContainer()
let tags = languageTags.map(\.rawValue)
try container.encode(tags)
for tag in tags {
try container.encode(tag)
}
}
}
6 changes: 2 additions & 4 deletions Sources/VRCKit/Models/WorldModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
public struct World: Codable, Identifiable, Hashable {
public let id: String
public let name: String
public let description: String
public let description: String?
public let featured: Bool
public let authorId: String
public let authorName: String
Expand All @@ -30,9 +30,7 @@ public struct World: Codable, Identifiable, Hashable {
public let visits: Int
public let popularity: Int
public let heat: Int
// MARK: Only world info params
// public let publicOccupants: Int
// public let privateOccupants: Int
public let favoriteGroup: String?
public let version: Int?

public enum ReleaseStatus: String, Codable {
Expand Down
1 change: 1 addition & 0 deletions Sources/VRCKit/PreviewServices/PreviewDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public final class PreviewDataProvider {
visits: 1,
popularity: 1,
heat: 1,
favoriteGroup: "",
version: 1
)
}
Expand Down
1 change: 0 additions & 1 deletion Sources/VRCKit/Protocols/WorldServiceProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by kiripoipoi on 2024/09/07.
//

// WorldServiceProtocol.swift
public protocol WorldServiceProtocol {
func fetchWorld(worldId: String) async throws -> World
func fetchFavoritedWorlds() async throws -> [World]
Expand Down
1 change: 1 addition & 0 deletions Sources/VRCKit/Services/AuthenticationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class AuthenticationService: APIService, AuthenticationServiceProtocol {

/// Verify 2FA With TOTP or Email OTP
public func verify2FA(verifyType: VerifyType, code: String) async throws -> Bool {
guard code.count == 6 else { throw VRCKitError.invalidRequest("Code must be 6 digits") }
let path = "\(authPath)/twofactorauth/\(verifyType.rawValue.lowercased())/verify"
let requestData = try Serializer.shared.encode(VerifyRequest(code: code))
let response = try await client.request(
Expand Down
3 changes: 1 addition & 2 deletions Sources/VRCKit/Services/WorldService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public class WorldService: APIService, WorldServiceProtocol {
}

public func fetchFavoritedWorlds() async throws -> [World] {
let path = "worlds/favorites"
let response = try await client.request(path: path, method: .get)
let response = try await client.request(path: "\(path)/favorites", method: .get)
let favoriteWorldWrapper: FavoriteWorldWrapper = try Serializer.shared.decode(response.data)
return favoriteWorldWrapper.worlds
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/VRCKit/Utils/CookieManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

public final class CookieManager {
private var domainURL: String?
Expand Down
4 changes: 3 additions & 1 deletion Sources/VRCKit/Utils/OptionalISO8601Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ extension OptionalISO8601Date: Encodable {
}
}

extension OptionalISO8601Date: Hashable {
extension OptionalISO8601Date: Equatable {
public static func == (lhs: OptionalISO8601Date, rhs: OptionalISO8601Date) -> Bool {
lhs.date == rhs.date
}
}

extension OptionalISO8601Date: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(date)
}
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
build:
image: swift:5.10.1
command: bash -c "swift build -c release"
volumes:
- ${PWD}:${PWD}
working_dir: ${PWD}
lint:
image: ghcr.io/realm/swiftlint:latest
volumes:
- ${PWD}:${PWD}
working_dir: ${PWD}

0 comments on commit c59c5e9

Please sign in to comment.