Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: 3lvis/Networking
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6.11.2
Choose a base ref
...
head repository: 3lvis/Networking
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 5 commits
  • 11 files changed
  • 2 contributors

Commits on Jul 19, 2024

  1. Fix Void return for fake

    3lvis committed Jul 19, 2024
    Copy the full SHA
    d7dad91 View commit details
  2. Fix/test faking for new methods

    3lvis committed Jul 19, 2024
    Copy the full SHA
    dba6e3a View commit details

Commits on Aug 4, 2024

  1. Add newPatch

    3lvis committed Aug 4, 2024
    Copy the full SHA
    5aaa89f View commit details

Commits on Aug 5, 2024

  1. Improve Fake handling, add status code to faking with a file. (#280)

    * Improve fake handling
    
    * Improve fake handling, add status code to faking with file
    
    * Remove dumb comparison
    
    * Rename none to data
    3lvis authored Aug 5, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    28f479e View commit details

Commits on Oct 13, 2024

  1. Now supporting get parameters (#281)

    3lvis authored Oct 13, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    127ac61 View commit details
15 changes: 13 additions & 2 deletions Sources/Networking/JSON.swift
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ enum ParsingError: Error {
}

enum JSON: Equatable {
case none
case data(Data)

case dictionary(Data, [String: Any])

@@ -30,6 +30,17 @@ enum JSON: Equatable {
}
}

var data: Data {
switch self {
case let .dictionary(data, _):
return data
case let .array(data, _):
return data
case let .data(data):
return data
}
}

init(_ data: Data) throws {
let body = try JSONSerialization.jsonObject(with: data, options: [])

@@ -38,7 +49,7 @@ enum JSON: Equatable {
} else if let array = body as? [[String: Any]] {
self = .array(data, array)
} else {
self = JSON.none
self = .data(data)
}
}

38 changes: 26 additions & 12 deletions Sources/Networking/Networking+HTTPRequests.swift
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@ public extension Networking {
/// - path: The path for the faked GET request.
/// - fileName: The name of the file, whose contents will be registered as a reponse.
/// - bundle: The Bundle where the file is located.
func fakeGET(_ path: String, fileName: String, bundle: Bundle = Bundle.main, delay: Double = 0) {
registerFake(requestType: .get, path: path, fileName: fileName, bundle: bundle, delay: delay)
func fakeGET(_ path: String, fileName: String, bundle: Bundle = Bundle.main, statusCode: Int = 200, delay: Double = 0) {
registerFake(requestType: .get, path: path, fileName: fileName, bundle: bundle, statusCode: statusCode, delay: delay)
}

/// Cancels the GET request for the specified path. This causes the request to complete with error code URLError.cancelled.
@@ -41,8 +41,8 @@ public extension Networking {
await cancelRequest(.data, requestType: .get, url: url)
}

func newGet<T: Decodable>(_ path: String) async -> Result<T, NetworkingError> {
return await handle(.get, path: path, parameters: nil)
func newGet<T: Decodable>(_ path: String, parameters: Any? = nil) async -> Result<T, NetworkingError> {
return await handle(.get, path: path, parameters: parameters)
}

func newPost<T: Decodable>(_ path: String, parameters: [String: Any]) async -> Result<T, NetworkingError> {
@@ -73,6 +73,20 @@ public extension Networking {
}
}

func newPatch<T: Decodable>(_ path: String, parameters: [String: Any]) async -> Result<T, NetworkingError> {
return await handle(.patch, path: path, parameters: parameters)
}

func newPatch(_ path: String, parameters: [String: Any]) async -> Result<Void, NetworkingError> {
let result: Result<Data, NetworkingError> = await handle(.patch, path: path, parameters: parameters)
switch result {
case .success:
return .success(())
case .failure(let error):
return .failure(error)
}
}

func newDelete(_ path: String) async -> Result<Void, NetworkingError> {
let result: Result<Data, NetworkingError> = await handle(.delete, path: path, parameters: nil)
switch result {
@@ -113,8 +127,8 @@ public extension Networking {
/// - path: The path for the faked PATCH request.
/// - fileName: The name of the file, whose contents will be registered as a reponse.
/// - bundle: The Bundle where the file is located.
func fakePATCH(_ path: String, fileName: String, bundle: Bundle = Bundle.main, delay: Double = 0) {
registerFake(requestType: .patch, path: path, fileName: fileName, bundle: bundle, delay: delay)
func fakePATCH(_ path: String, fileName: String, bundle: Bundle = Bundle.main, statusCode: Int = 200, delay: Double = 0) {
registerFake(requestType: .patch, path: path, fileName: fileName, bundle: bundle, statusCode: statusCode, delay: delay)
}

/// Cancels the PATCH request for the specified path. This causes the request to complete with error code URLError.cancelled.
@@ -155,8 +169,8 @@ public extension Networking {
/// - path: The path for the faked PUT request.
/// - fileName: The name of the file, whose contents will be registered as a reponse.
/// - bundle: The Bundle where the file is located.
func fakePUT(_ path: String, fileName: String, bundle: Bundle = Bundle.main, delay: Double = 0) {
registerFake(requestType: .put, path: path, fileName: fileName, bundle: bundle, delay: delay)
func fakePUT(_ path: String, fileName: String, bundle: Bundle = Bundle.main, statusCode: Int = 200, delay: Double = 0) {
registerFake(requestType: .put, path: path, fileName: fileName, bundle: bundle, statusCode: statusCode, delay: delay)
}

/// Cancels the PUT request for the specified path. This causes the request to complete with error code URLError.cancelled.
@@ -207,8 +221,8 @@ public extension Networking {
/// - path: The path for the faked POST request.
/// - fileName: The name of the file, whose contents will be registered as a reponse.
/// - bundle: The Bundle where the file is located.
func fakePOST(_ path: String, fileName: String, bundle: Bundle = Bundle.main, delay: Double = 0) {
registerFake(requestType: .post, path: path, fileName: fileName, bundle: bundle, delay: delay)
func fakePOST(_ path: String, fileName: String, bundle: Bundle = Bundle.main, statusCode: Int = 200, delay: Double = 0) {
registerFake(requestType: .post, path: path, fileName: fileName, bundle: bundle, statusCode: statusCode, delay: delay)
}

/// Cancels the POST request for the specified path. This causes the request to complete with error code URLError.cancelled.
@@ -249,8 +263,8 @@ public extension Networking {
/// - path: The path for the faked DELETE request.
/// - fileName: The name of the file, whose contents will be registered as a reponse.
/// - bundle: The Bundle where the file is located.
func fakeDELETE(_ path: String, fileName: String, bundle: Bundle = Bundle.main, delay: Double = 0) {
registerFake(requestType: .delete, path: path, fileName: fileName, bundle: bundle, delay: delay)
func fakeDELETE(_ path: String, fileName: String, bundle: Bundle = Bundle.main, statusCode: Int = 200, delay: Double = 0) {
registerFake(requestType: .delete, path: path, fileName: fileName, bundle: bundle, statusCode: statusCode, delay: delay)
}

/// Cancels the DELETE request for the specified path. This causes the request to complete with error code URLError.cancelled.
Loading