Skip to content

Commit

Permalink
1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 25, 2024
1 parent 875f8b9 commit 6a833c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.1.0")
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.1.1")
],
targets: [
.target(
Expand Down
30 changes: 16 additions & 14 deletions Sources/SwiftAPIClient/Modifiers/BackgroundModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ public extension APIClient {

private struct BackgroundTaskMiddleware: HTTPClientMiddleware {

func execute<T>(
request: URLRequest,
configs: APIClient.Configs,
next: (URLRequest, APIClient.Configs) async throws -> (T, HTTPURLResponse)
) async throws -> (T, HTTPURLResponse) {
func execute<T>(
request: HTTPRequest,
body: RequestBody?,
configs: APIClient.Configs,
next: (HTTPRequest, RequestBody?, APIClient.Configs) async throws -> (T, HTTPResponse)
) async throws -> (T, HTTPResponse) {
let id = await UIApplication.shared.beginBackgroundTask(
withName: "Background Task for \(request.url?.absoluteString ?? "")"
)
guard id != .invalid else {
return try await next(request, configs)
return try await next(request, body, configs)
}
do {
let result = try await next(request, configs)
let result = try await next(request, body, configs)
await UIApplication.shared.endBackgroundTask(id)
return result
} catch {
Expand All @@ -42,19 +43,20 @@ private struct BackgroundTaskMiddleware: HTTPClientMiddleware {

private struct RetryOnEnterForegroundMiddleware: HTTPClientMiddleware {

func execute<T>(
request: URLRequest,
configs: APIClient.Configs,
next: (URLRequest, APIClient.Configs) async throws -> (T, HTTPURLResponse)
) async throws -> (T, HTTPURLResponse) {
func makeRequest() async throws -> (T, HTTPURLResponse) {
func execute<T>(
request: HTTPRequest,
body: RequestBody?,
configs: APIClient.Configs,
next: (HTTPRequest, RequestBody?, APIClient.Configs) async throws -> (T, HTTPResponse)
) async throws -> (T, HTTPResponse) {
func makeRequest() async throws -> (T, HTTPResponse) {
let wasInBackground = WasInBackgroundService()
var isInBackground = await UIApplication.shared.applicationState == .background
if !isInBackground {
await wasInBackground.start()
}
do {
return try await next(request, configs)
return try await next(request, body, configs)
} catch {
isInBackground = await UIApplication.shared.applicationState == .background
if !isInBackground, await wasInBackground.wasInBackground {
Expand Down

0 comments on commit 6a833c3

Please sign in to comment.