Skip to content

Mocked Types #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let package = Package(
],
products: [
.library(name: "WebPush", targets: ["WebPush"]),
.library(name: "WebPushTesting", targets: ["WebPush", "WebPushTesting"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", "3.10.0"..<"5.0.0"),
Expand All @@ -33,10 +34,21 @@ let package = Package(
.product(name: "NIOHTTP1", package: "swift-nio"),
]
),
.target(
name: "WebPushTesting",
dependencies: [
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "Logging", package: "swift-log"),
.target(name: "WebPush"),
]
),
.testTarget(name: "WebPushTests", dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "Logging", package: "swift-log"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
.target(name: "WebPush"),
.target(name: "WebPushTesting"),
]),
]
)
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# WebPush

<p align="center">
<a href="https://swiftpackageindex.com/mochidev/WebPush">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2FWebPush%2Fbadge%3Ftype%3Dswift-versions" />
<a href="https://swiftpackageindex.com/mochidev/swift-webpush">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2Fswift-webpush%2Fbadge%3Ftype%3Dswift-versions" />
</a>
<a href="https://swiftpackageindex.com/mochidev/WebPush">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2FWebPush%2Fbadge%3Ftype%3Dplatforms" />
<a href="https://swiftpackageindex.com/mochidev/swift-webpush">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2Fswift-webpush%2Fbadge%3Ftype%3Dplatforms" />
</a>
<a href="https://github.com/mochidev/WebPush/actions?query=workflow%3A%22Test+WebPush%22">
<img src="https://github.com/mochidev/WebPush/workflows/Test%20WebPush/badge.svg" alt="Test Status" />
Expand Down Expand Up @@ -40,6 +40,9 @@ targets: [
"WebPush",
]
)
.testTarget(name: "MyPackageTests", dependencies: [
"WebPushTesting",
]
]
```

Expand Down
23 changes: 23 additions & 0 deletions Sources/WebPush/Helpers/HTTPClientProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// HTTPClientProtocol.swift
// swift-webpush
//
// Created by Dimitri Bouniol on 2024-12-11.
// Copyright © 2024 Mochi Development, Inc. All rights reserved.
//

import AsyncHTTPClient
import Logging
import NIOCore

package protocol HTTPClientProtocol: Sendable {
func execute(
_ request: HTTPClientRequest,
deadline: NIODeadline,
logger: Logger?
) async throws -> HTTPClientResponse

func syncShutdown() throws
}

extension HTTPClient: HTTPClientProtocol {}
16 changes: 8 additions & 8 deletions Sources/WebPush/Helpers/PrintLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import Foundation
import Logging

struct PrintLogHandler: LogHandler {
package struct PrintLogHandler: LogHandler {
private let label: String

var logLevel: Logger.Level = .info
var metadataProvider: Logger.MetadataProvider?
package var logLevel: Logger.Level = .info
package var metadataProvider: Logger.MetadataProvider?

init(
package init(
label: String,
logLevel: Logger.Level = .info,
metadataProvider: Logger.MetadataProvider? = nil
Expand All @@ -26,13 +26,13 @@ struct PrintLogHandler: LogHandler {
}

private var prettyMetadata: String?
var metadata = Logger.Metadata() {
package var metadata = Logger.Metadata() {
didSet {
self.prettyMetadata = self.prettify(self.metadata)
}
}

subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
package subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
self.metadata[metadataKey]
}
Expand All @@ -41,7 +41,7 @@ struct PrintLogHandler: LogHandler {
}
}

func log(
package func log(
level: Logger.Level,
message: Logger.Message,
metadata explicitMetadata: Logger.Metadata?,
Expand All @@ -66,7 +66,7 @@ struct PrintLogHandler: LogHandler {
print("\(self.timestamp()) [\(level)] \(self.label):\(prettyMetadata.map { " \($0)" } ?? "") [\(source)] \(message)")
}

internal static func prepareMetadata(
private static func prepareMetadata(
base: Logger.Metadata,
provider: Logger.MetadataProvider?,
explicit: Logger.Metadata?
Expand Down
9 changes: 9 additions & 0 deletions Sources/WebPush/Subscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ public struct Subscriber: SubscriberProtocol, Codable, Hashable, Sendable {
self.userAgentKeyMaterial = userAgentKeyMaterial
self.vapidKeyID = vapidKeyID
}

/// Cast an object that conforms to ``SubscriberProtocol`` to a ``Subscriber``.
public init(_ subscriber: some SubscriberProtocol) {
self.init(
endpoint: subscriber.endpoint,
userAgentKeyMaterial: subscriber.userAgentKeyMaterial,
vapidKeyID: subscriber.vapidKeyID
)
}
}

extension Subscriber: Identifiable {
Expand Down
Loading
Loading