Skip to content

Commit

Permalink
finishing prep
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Aug 20, 2024
1 parent a7809f9 commit ae75d0f
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public import SublimationTunnel
/// a `NgrokServerError` if the save operation fails.
///
/// - SeeAlso: `KVdbTunnelClient`
public struct URLSessionClient<Key: Sendable>: TunnelClient {
internal struct URLSessionClient<Key: Sendable>: TunnelClient {
private let session: URLSession

/// Initializes a new `URLSessionClient` with the specified session.
///
/// - Parameter session: The URLSession to use for network requests.
/// Defaults to an ephemeral session.
public init(session: URLSession = .ephemeral()) { self.session = session }
internal init(session: URLSession = .ephemeral()) { self.session = session }

Check warning on line 54 in Packages/SublimationNgrok/Sources/SublimationKVdb/URLSessionClient.swift

View check run for this annotation

Codecov / codecov/patch

Packages/SublimationNgrok/Sources/SublimationKVdb/URLSessionClient.swift#L54

Added line #L54 was not covered by tests

/// Retrieves the value associated with a key from a specific bucket.
///
Expand All @@ -62,7 +62,7 @@ public struct URLSessionClient<Key: Sendable>: TunnelClient {
/// - Returns: The URL value associated with the key.
///
/// - Throws: A `NgrokServerError` if the retrieval operation fails.
public func getValue(ofKey key: Key, fromBucket bucketName: String) async throws -> URL {
internal func getValue(ofKey key: Key, fromBucket bucketName: String) async throws -> URL {

Check warning on line 65 in Packages/SublimationNgrok/Sources/SublimationKVdb/URLSessionClient.swift

View check run for this annotation

Codecov / codecov/patch

Packages/SublimationNgrok/Sources/SublimationKVdb/URLSessionClient.swift#L65

Added line #L65 was not covered by tests
let url = KVdb.construct(URL.self, forKey: key, atBucket: bucketName)

let data = try await session.data(from: url).0
Expand All @@ -81,7 +81,8 @@ public struct URLSessionClient<Key: Sendable>: TunnelClient {
/// - bucketName: The name of the bucket to save the value in.
///
/// - Throws: A `NgrokServerError` if the save operation fails.
public func saveValue(_ value: URL, withKey key: Key, inBucket bucketName: String) async throws {
internal func saveValue(_ value: URL, withKey key: Key, inBucket bucketName: String) async throws
{
let url = KVdb.construct(URL.self, forKey: key, atBucket: bucketName)
var request = URLRequest(url: url)
request.httpBody = value.absoluteString.data(using: .utf8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,5 @@ extension NgrokCLIAPIConfiguration {
/// using a `Vapor.Application`.
///
/// - Parameter application: The Vapor application to use for configuration.

public init(application: any Application) { self.init(serverApplication: application) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import SublimationTunnel

extension NgrokCLIAPIServer {
/// Runs the server.
public func begin(isConnectionRefused: @escaping (ClientError) -> Bool) async {
private func begin(isConnectionRefused: @escaping (ClientError) -> Bool) async {

Check warning on line 37 in Packages/SublimationNgrok/Sources/SublimationNgrok/NgrokCLIAPIServer+TunnelServer.swift

View check run for this annotation

Codecov / codecov/patch

Packages/SublimationNgrok/Sources/SublimationNgrok/NgrokCLIAPIServer+TunnelServer.swift#L37

Added line #L37 was not covered by tests
let start = Date()
let newTunnel: any Tunnel
do { newTunnel = try await self.newTunnel(isConnectionRefused: isConnectionRefused) }
Expand All @@ -56,7 +56,11 @@ extension NgrokCLIAPIServer {
internal private(set) var isStarted = false
func started() { isStarted = true }
}
/// Terminate the `NgrokProcess`.
public func shutdown() { self.process.terminate() }
/// Starts the process for Ngrok if nessecary and creates a new tunnel.
/// - Parameter isConnectionRefused: Closure to test whether the ``ClientError`` is because the server still needs to be started.
/// - Throws: If there's an issue starting the server.
public func run(isConnectionRefused: @escaping @Sendable (ClientError) -> Bool) async throws {
try await withThrowingTaskGroup(
of: Void.self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension NgrokClient {
{ try await self.listTunnels().first },
isConnectionRefused: isConnectionRefused
)
switch networkResult { case let .connectionRefused(error): return .error(error)
switch networkResult { case .connectionRefused(let error): return .error(error)

Check warning on line 44 in Packages/SublimationNgrok/Sources/SublimationNgrok/NgrokClient.swift

View check run for this annotation

Codecov / codecov/patch

Packages/SublimationNgrok/Sources/SublimationNgrok/NgrokClient.swift#L44

Added line #L44 was not covered by tests

default: return .network(networkResult)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public enum NetworkResult<T, ConnectionErrorType: Error> {
}

extension NetworkResult {
public init(error: any Error, isConnectionRefused: @escaping (ConnectionErrorType) -> Bool) {
package init(error: any Error, isConnectionRefused: @escaping (ConnectionErrorType) -> Bool) {

Check warning on line 50 in Packages/SublimationNgrok/Sources/SublimationTunnel/NetworkResult.swift

View check run for this annotation

Codecov / codecov/patch

Packages/SublimationNgrok/Sources/SublimationTunnel/NetworkResult.swift#L50

Added line #L50 was not covered by tests
guard let error = error as? ConnectionErrorType else {
self = .failure(error)
return
Expand All @@ -61,15 +61,15 @@ extension NetworkResult {
self = .failure(error)
}

public init(
package init(
_ closure: @escaping () async throws -> T,
isConnectionRefused: @escaping (ConnectionErrorType) -> Bool
) async {
do { self = try await .success(closure()) }
catch { self = .init(error: error, isConnectionRefused: isConnectionRefused) }
}

public func get() throws -> T? {
package func get() throws -> T? {

Check warning on line 72 in Packages/SublimationNgrok/Sources/SublimationTunnel/NetworkResult.swift

View check run for this annotation

Codecov / codecov/patch

Packages/SublimationNgrok/Sources/SublimationTunnel/NetworkResult.swift#L72

Added line #L72 was not covered by tests
switch self { case .connectionRefused: return nil

case .failure(let error): throw error

Check warning on line 75 in Packages/SublimationNgrok/Sources/SublimationTunnel/NetworkResult.swift

View check run for this annotation

Codecov / codecov/patch

Packages/SublimationNgrok/Sources/SublimationTunnel/NetworkResult.swift#L75

Added line #L75 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

public import Foundation

/// A tunnel with a name and public url for the server.
public protocol Tunnel: Sendable {
var name: String { get }
var publicURL: URL { get }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,39 @@ public import Foundation
public import FoundationNetworking
#endif

/// A repository for managing writable ``Tunnel`` objects using a ``TunnelClient``.
public final class TunnelClientRepository<Key: Sendable>: WritableTunnelRepository {
private let client: any TunnelClient<Key>
private let bucketName: String
/// Create a ``TunnelClientRepository`` using the ``TunnelClient`` and the name of the bucket for the key value pair.
/// - Parameters:
/// - client: The ``TunnelClient`` to communicate with.
/// - bucketName: The bucket name for the key value pair.
public init(client: any TunnelClient<Key>, bucketName: String) {
self.client = client
self.bucketName = bucketName
}

/// Retrieves a tunnel for the specified key.
///
/// - Parameter key: The key used to retrieve the tunnel.
///
/// - Throws: An error if the tunnel cannot be retrieved.
///
/// - Returns: The URL of the retrieved tunnel, if available.
public func tunnel(forKey key: Key) async throws -> URL? {
try await client.getValue(ofKey: key, fromBucket: bucketName)
}

/// Saves a URL with a key.
///
/// - Parameters:
/// - url: The URL to save.
/// - key: The key to associate with the URL.
///
/// - Throws: An error if the save operation fails.
///
/// - Note: This method is asynchronous.
public func saveURL(_ url: URL, withKey key: Key) async throws {
try await client.saveValue(url, withKey: key, inBucket: bucketName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@ public protocol TunnelRepository<Key>: Sendable {
/// - Throws: An error if the tunnel cannot be retrieved.
///
/// - Returns: The URL of the retrieved tunnel, if available.

func tunnel(forKey key: Key) async throws -> URL?
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public protocol TunnelServer: Sendable {
@available(*, deprecated) func start(
isConnectionRefused: @escaping @Sendable (ConnectionErrorType) -> Bool
)
/// Runs TunnelServeer.
/// - Parameter isConnectionRefused: Closure to test whether the ``ConnectionErrorType`` is because the server still needs to be started.
/// - Throws: If there's an issue starting the server.
func run(isConnectionRefused: @escaping @Sendable (ConnectionErrorType) -> Bool) async throws
/// Shutdown the ``TunnelServer``.
func shutdown()
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ public import SublimationCore
public typealias RepositoryClientFactory<Key> = (@Sendable @escaping () -> any Application) ->
any TunnelClient<Key>

/// A `Sublimatory` which uses creates and saves a``Tunnel``.
public actor TunnelSublimatory<
WritableTunnelRepositoryFactoryType: WritableTunnelRepositoryFactory,
TunnelServerFactoryType: TunnelServerFactory
>: Sublimatory, TunnelServerDelegate {

/// `Key type
public typealias Key = WritableTunnelRepositoryFactoryType.TunnelRepositoryType.Key
/// Type of Error which can be thrown by the Network Client.
public typealias ConnectionErrorType = TunnelServerFactoryType.Configuration.Server
.ConnectionErrorType
private let factory: TunnelServerFactoryType
Expand Down Expand Up @@ -164,7 +167,7 @@ public actor TunnelSublimatory<
public nonisolated func server(_: any TunnelServer, errorDidOccur error: any Error) {
Task { await self.onError(error) }
}
func shutdownServer() { server.shutdown() }
private func shutdownServer() { server.shutdown() }

Check warning on line 170 in Packages/SublimationNgrok/Sources/SublimationTunnel/TunnelSublimatory.swift

View check run for this annotation

Codecov / codecov/patch

Packages/SublimationNgrok/Sources/SublimationTunnel/TunnelSublimatory.swift#L170

Added line #L170 was not covered by tests
public nonisolated func shutdown() { Task { await self.shutdownServer() } }
public func run() async throws {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public import Foundation

/// A repository for managing writable tunnels.
/// A repository for managing writable ``Tunnel`` objects.
///
/// This protocol extends the `TunnelRepository` protocol
/// and adds the ability to save a URL with a key.
Expand Down

0 comments on commit ae75d0f

Please sign in to comment.