Skip to content

Commit

Permalink
Merge branch 'release/0.5.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
antonio-war committed Sep 19, 2024
2 parents 178024c + 6c023c4 commit 0b89b78
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
29 changes: 21 additions & 8 deletions Sources/SwiftyNetworking/PropertyWrappers/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ import SwiftUI
@propertyWrapper
public struct Request<Model: Decodable>: DynamicProperty {
public typealias Method = SwiftyNetworkingRequest.Method
@State private var model: Model?
@State private var error: Error?
@State private var model: Model? = nil
@State private var error: Error? = nil
@State private var fetching: Bool = false

let client: SwiftyNetworkingClient
let url: URL?
let method: Method
let headers: [String: String]
let body: Data?
let cachePolicy: CachePolicy
let timeout: TimeInterval
let decoder: JSONDecoder
let client: SwiftyNetworkingClient = SwiftyNetworkingClient()

public init(
client: SwiftyNetworkingClient = SwiftyNetworkingClient(),
url: URL?,
method: Method = .get,
headers: [String : String] = [:],
Expand All @@ -33,6 +35,7 @@ public struct Request<Model: Decodable>: DynamicProperty {
timeout: TimeInterval = 60,
decoder: JSONDecoder = JSONDecoder()
) {
self.client = client
self.url = url
self.method = method
self.headers = headers
Expand All @@ -43,6 +46,7 @@ public struct Request<Model: Decodable>: DynamicProperty {
}

public init(
client: SwiftyNetworkingClient = SwiftyNetworkingClient(),
url: String,
method: Method = .get,
headers: [String : String] = [:],
Expand All @@ -52,6 +56,7 @@ public struct Request<Model: Decodable>: DynamicProperty {
decoder: JSONDecoder = JSONDecoder()
) {
self.init(
client: client,
url: URL(
string: url
),
Expand All @@ -68,9 +73,14 @@ public struct Request<Model: Decodable>: DynamicProperty {
model
}

private var shouldUpdate: Bool {
model == nil && error == nil && fetching == false
}

@MainActor
func fetch() async {
do {
self.fetching = true
let request = try SwiftyNetworkingRequest(
url: url,
method: method,
Expand All @@ -80,19 +90,22 @@ public struct Request<Model: Decodable>: DynamicProperty {
timeout: timeout
)
self.model = try await client.send(request, decoding: Model.self, using: decoder)
self.fetching = false
} catch {
self.error = error
self.fetching = false
}
}

public func update() {
guard shouldUpdate else {
return
}

Task {
self.model = nil
self.error = nil
await fetch()
}
}

func reset() {
self.model = nil
self.error = nil
}
}
24 changes: 24 additions & 0 deletions Tests/SwiftyNetworkingTests/PropertyWrappers/RequestTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// RequestTests.swift
//
//
// Created by Antonio Guerra on 18/09/24.
//

import SwiftyNetworking
import XCTest

final class RequestTests: XCTestCase {

func testInitFromURL() {
@Request(url: URL(string: "https://jsonplaceholder.typicode.com/users"))
var users: [JsonPlaceholderUser]?
XCTAssertNil(users)
}

func testInitFromString() {
@Request(url: "https://jsonplaceholder.typicode.com/users")
var users: [JsonPlaceholderUser]?
XCTAssertNil(users)
}
}

0 comments on commit 0b89b78

Please sign in to comment.