Skip to content

Commit

Permalink
Merge pull request #8 from MFB-Technologies-Inc/async-beta
Browse files Browse the repository at this point in the history
Release async API and remove combine based API
  • Loading branch information
roanutil authored Mar 24, 2023
2 parents 78a253d + e8269bf commit 698fc3b
Show file tree
Hide file tree
Showing 53 changed files with 432 additions and 2,704 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ on:
pull_request:
branches:
- '*'
workflow_dispatch:

jobs:
library:
lint:
runs-on: macos-latest
environment: default
steps:
- uses: actions/checkout@v3
- name: Format lint
run: swiftformat --lint .
- name: Lint
run: swiftlint .
macos-test:
environment: default
strategy:
matrix:
os:
- macos-11
- macos-12
xcode:
- '12.4'
- '12.5.1'
- '13.2'
- '13.4'
- '14.0.1'
Expand All @@ -35,23 +43,15 @@ jobs:
xcode: '14.1'
- os: macos-11
xcode: '14.2'
- os: macos-12
xcode: '12.4'
- os: macos-12
xcode: '12.5.1'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Select Xcode ${{ matrix.xcode }}
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
- name: Format lint
run: swiftformat --lint .
- name: Lint
run: swiftlint .
- name: Run Tests
run: swift test --enable-code-coverage --parallel
- name: Swift Coverage Report
run: xcrun llvm-cov export -format="lcov" .build/debug/NetworkServicePackageTests.xctest/Contents/MacOS//NetworkServicePackageTests -instr-profile .build/debug/codecov/default.profdata > coverage_report.lcov
- uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true # optional (default = false)
fail_ci_if_error: true # optional (default = false)
195 changes: 53 additions & 142 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.5

import PackageDescription

Expand All @@ -10,146 +10,57 @@ let package = Package(
targets: Target.targets
)

#if swift(>=5.5)
extension Product {
static let products: [Product] = [
.library(
name: "NetworkService",
targets: ["NetworkService"]
),
.library(
name: "NetworkServiceTestHelper",
targets: ["NetworkServiceTestHelper"]
),
.library(
name: "NetworkServiceAsyncBeta",
targets: ["NetworkServiceAsyncBeta"]
),
.library(
name: "NetworkServiceTestHelperAsyncBeta",
targets: ["NetworkServiceTestHelperAsyncBeta"]
),
]
}
extension Product {
static let products: [Product] = [
.library(
name: "NetworkService",
targets: ["NetworkService"]
),
.library(
name: "NetworkServiceTestHelper",
targets: ["NetworkServiceTestHelper"]
),
]
}

extension Target {
static let targets: [Target] = [
.target(
name: "NetworkService",
dependencies: []
),
.testTarget(
name: "NetworkServiceTests",
dependencies: [
"NetworkService",
.product(name: "OHHTTPStubs", package: "OHHTTPStubs"),
.product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs"),
]
),
.target(name: "NetworkServiceAsyncBeta"),
.testTarget(
name: "NetworkServiceAsyncBetaTests",
dependencies: [
"NetworkServiceAsyncBeta",
.product(name: "OHHTTPStubs", package: "OHHTTPStubs"),
.product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs"),
]
),
.target(
name: "NetworkServiceTestHelper",
dependencies: [
"NetworkService",
.product(name: "CombineSchedulers", package: "combine-schedulers"),
]
),
.testTarget(
name: "NetworkServiceTestHelperTests",
dependencies: [
"NetworkServiceTestHelper",
.product(name: "CombineSchedulers", package: "combine-schedulers"),
]
),
.target(
name: "NetworkServiceTestHelperAsyncBeta",
dependencies: [
"NetworkServiceAsyncBeta",
.product(name: "CombineSchedulers", package: "combine-schedulers"),
]
),
.testTarget(
name: "NetworkServiceTestHelperAsyncBetaTests",
dependencies: [
"NetworkServiceTestHelperAsyncBeta",
.product(name: "CombineSchedulers", package: "combine-schedulers"),
]
),
]
}
extension Target {
static let targets: [Target] = [
.target(
name: "NetworkService",
dependencies: []
),
.testTarget(
name: "NetworkServiceTests",
dependencies: [
"NetworkService",
.product(name: "OHHTTPStubs", package: "OHHTTPStubs"),
.product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs"),
]
),
.target(
name: "NetworkServiceTestHelper",
dependencies: [
"NetworkService",
.product(name: "CombineSchedulers", package: "combine-schedulers"),
]
),
.testTarget(
name: "NetworkServiceTestHelperTests",
dependencies: [
"NetworkServiceTestHelper",
.product(name: "CombineSchedulers", package: "combine-schedulers"),
]
),
]
}

extension Package.Dependency {
static let dependencies: [Package.Dependency] = [
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.1.0"),
.package(
name: "combine-schedulers",
url: "https://github.com/pointfreeco/combine-schedulers.git",
.upToNextMajor(from: "0.6.0")
),
]
}

#else
extension Product {
static let products: [Product] = [
.library(
name: "NetworkService",
targets: ["NetworkService"]
),
.library(
name: "NetworkServiceTestHelper",
targets: ["NetworkServiceTestHelper"]
),
]
}

extension Target {
static let targets: [Target] = [
.target(
name: "NetworkService",
dependencies: []
),
.testTarget(
name: "NetworkServiceTests",
dependencies: [
"NetworkService",
.product(name: "OHHTTPStubs", package: "OHHTTPStubs"),
.product(name: "OHHTTPStubsSwift", package: "OHHTTPStubs"),
]
),
.target(
name: "NetworkServiceTestHelper",
dependencies: [
"NetworkService",
.product(name: "CombineSchedulers", package: "combine-schedulers"),
]
),
.testTarget(
name: "NetworkServiceTestHelperTests",
dependencies: [
"NetworkServiceTestHelper",
.product(name: "CombineSchedulers", package: "combine-schedulers"),
]
),
]
}

extension Package.Dependency {
static let dependencies: [Package.Dependency] = [
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.1.0"),
.package(
name: "combine-schedulers",
url: "https://github.com/pointfreeco/combine-schedulers.git",
.upToNextMinor(from: "0.5.3")
),
]
}
#endif
extension Package.Dependency {
static let dependencies: [Package.Dependency] = [
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.1.0"),
.package(
name: "combine-schedulers",
url: "https://github.com/pointfreeco/combine-schedulers.git",
.upToNextMajor(from: "0.6.0")
),
]
}
10 changes: 6 additions & 4 deletions Sources/NetworkService/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import Foundation
/// Provides methods for making network requests and processing the resulting responses
public final class NetworkService {
/// `NetworkService`'s error domain
public enum Failure: Error, Hashable {
case url(URLResponse)
case http(HTTPURLResponse)
case cocoa(NSError)
public enum Failure: Error, Hashable, Sendable {
case urlResponse(URLResponse)
case httpResponse(HTTPURLResponse)
case urlError(URLError)
case unknown(NSError)
}

public init() {}
}

// MARK: NetworkService+NetworkServiceClient

@available(swift 5.5)
extension NetworkService: NetworkServiceClient {}
71 changes: 37 additions & 34 deletions Sources/NetworkService/NetworkServiceClient+Delete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

import Combine
import Foundation

extension NetworkServiceClient {
Expand All @@ -19,40 +18,44 @@ extension NetworkServiceClient {
public func delete(
_ url: URL,
headers: [HTTPHeader] = []
) -> AnyPublisher<Data, Failure> {
var request = URLRequest(url: url)
request.method = .DELETE
headers.forEach { request.addValue($0) }
return start(request)
) async -> Result<Data, Failure> {
let request = URLRequest.build(url: url, headers: headers, method: .DELETE)
return await start(request)
}
}

/// Send a delete request to a `URL`
/// - Parameters:
/// - url: The destination for the request
/// - headers: HTTP headers for the request
/// - decoder: `TopLevelDecoder` for decoding the response body
/// - Returns: Type erased publisher with decoded output and `NetworkService`'s error domain for failure
public func delete<ResponseBody, Decoder>(
_ url: URL,
headers: [HTTPHeader] = [],
decoder: Decoder
) -> AnyPublisher<ResponseBody, Failure>
where ResponseBody: Decodable, Decoder: TopLevelDecoder, Decoder.Input == Data
{
delete(url, headers: headers)
.decode(with: decoder)
.mapToNetworkError()
.eraseToAnyPublisher()
}
#if canImport(Combine)
import Combine

/// Send a delete request to a `URL`
/// - Parameters:
/// - url: The destination for the request
/// - headers: HTTP headers for the request
/// - Returns: Type erased publisher with `TopLevelDecodable` output and `NetworkService`'s error domain for failure
public func delete<ResponseBody>(_ url: URL, headers: [HTTPHeader] = []) -> AnyPublisher<ResponseBody, Failure>
where ResponseBody: TopLevelDecodable
{
delete(url, headers: headers, decoder: ResponseBody.decoder)
extension NetworkServiceClient {
/// Send a delete request to a `URL`
/// - Parameters:
/// - url: The destination for the request
/// - headers: HTTP headers for the request
/// - decoder: `TopLevelDecoder` for decoding the response body
/// - Returns: Type erased publisher with decoded output and `NetworkService`'s error domain for failure
public func delete<ResponseBody, Decoder>(
_ url: URL,
headers: [HTTPHeader] = [],
decoder: Decoder
) async -> Result<ResponseBody, Failure>
where ResponseBody: Decodable, Decoder: TopLevelDecoder, Decoder.Input == Data
{
await delete(url, headers: headers)
.decode(with: decoder)
.mapToNetworkError()
}

/// Send a delete request to a `URL`
/// - Parameters:
/// - url: The destination for the request
/// - headers: HTTP headers for the request
/// - Returns: Type erased publisher with `TopLevelDecodable` output and `NetworkService`'s error domain for
/// failure
public func delete<ResponseBody>(_ url: URL, headers: [HTTPHeader] = []) async -> Result<ResponseBody, Failure>
where ResponseBody: TopLevelDecodable
{
await delete(url, headers: headers, decoder: ResponseBody.decoder)
}
}
}
#endif
Loading

0 comments on commit 698fc3b

Please sign in to comment.