Skip to content

Commit

Permalink
Merge pull request #54 from icapps/feature/add-encodable
Browse files Browse the repository at this point in the history
Add encodable to Request body
  • Loading branch information
icappsCedric committed Mar 29, 2024
2 parents 1ee475d + 11663da commit 51309a5
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 7 deletions.
14 changes: 13 additions & 1 deletion Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
BEBF72B2A088D7289F7B2876 /* Pods_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 56EC7F2EF94A0A4D1B403A27 /* Pods_Tests.framework */; };
F31234B32BB593E2000C4F3A /* MockURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F31234B22BB593E2000C4F3A /* MockURLProtocol.swift */; };
F31234B52BB59C45000C4F3A /* StubbedRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = F31234B42BB59C45000C4F3A /* StubbedRequest.swift */; };
F3253C382BB61739006B1BF4 /* MockedEncodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3253C372BB61739006B1BF4 /* MockedEncodable.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -50,6 +51,7 @@
9DEA80C221D97275002A56A7 /* MockedLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockedLogger.swift; sourceTree = "<group>"; };
F31234B22BB593E2000C4F3A /* MockURLProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockURLProtocol.swift; sourceTree = "<group>"; };
F31234B42BB59C45000C4F3A /* StubbedRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StubbedRequest.swift; sourceTree = "<group>"; };
F3253C372BB61739006B1BF4 /* MockedEncodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockedEncodable.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -86,6 +88,7 @@
607FACE81AFB9204008FA782 /* Tests */ = {
isa = PBXGroup;
children = (
F3253C362BB6170D006B1BF4 /* Stubs */,
9D0E402A21B3FC2D0088D678 /* Helpers */,
9D60FF2621B402BE0078F791 /* Mocks */,
9D21FEF621D8F061006EF131 /* Resources */,
Expand Down Expand Up @@ -139,7 +142,7 @@
9D21FF0721D971C6006EF131 /* MockedPublicKeyPinningService.swift */,
9D60FF2721B402CA0078F791 /* MockedRequest.swift */,
9D60FF2C21B42A560078F791 /* MockedSerializer.swift */,
F31234B42BB59C45000C4F3A /* StubbedRequest.swift */,
F3253C372BB61739006B1BF4 /* MockedEncodable.swift */,
);
path = Mocks;
sourceTree = "<group>";
Expand Down Expand Up @@ -189,6 +192,14 @@
name = Frameworks;
sourceTree = "<group>";
};
F3253C362BB6170D006B1BF4 /* Stubs */ = {
isa = PBXGroup;
children = (
F31234B42BB59C45000C4F3A /* StubbedRequest.swift */,
);
path = Stubs;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -338,6 +349,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F3253C382BB61739006B1BF4 /* MockedEncodable.swift in Sources */,
9D446A9C21B3EAC100FEA1F7 /* ServiceSpec.swift in Sources */,
9D21FF0621D97196006EF131 /* NetworkServiceSpec.swift in Sources */,
9DAD37C521B45A900042C19C /* RequestSpec.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Example/Tests/Helpers/SecTrust+Apple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ extension SecTrust {
SecTrustCreateWithCertificates(certificates, policy, &optionalSecTrust)
return optionalSecTrust!
}
// swiftlint:enable force_try
}
5 changes: 5 additions & 0 deletions Example/Tests/Mocks/MockedEncodable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

struct MockedEncodable: Encodable {
let hello: String
}
8 changes: 8 additions & 0 deletions Example/Tests/Specs/Request/RequestSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ class RequestSpec: QuickSpec {
let urlRequest = try? request.makeURLRequest(with: configuration)
expect(urlRequest?.httpBody) == data
}

it("should have an encodable body") {
let body = MockedEncodable(hello: "Jake")
let request = MockedRequest(url: URL(string: "request"), body: body)
let urlRequest = try? request.makeURLRequest(with: configuration)
let data = try? JSONEncoder().encode(body)
expect(urlRequest?.httpBody) == data
}
}

it("should have the correct cache policy") {
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions Sources/Logger/ConsoleLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ extension ConsoleLogger: Logger {
let url = urlRequest.url else { return }
os_log("☁️ %{public}@: %{public}@", log: OSLog.request, type: .info, method, url.absoluteString)
}

// swiftlint:disable function_body_length

public func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) {
guard
let method = urlRequest.httpMethod,
Expand Down Expand Up @@ -70,5 +69,4 @@ extension ConsoleLogger: Logger {
}
}
}
// swiftlint:enable function_body_length
}
9 changes: 7 additions & 2 deletions Sources/Request/Request+URLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ extension Request {
}

private func makeBody() throws -> Data? {
guard let body = body else { return nil }
guard let body else { return nil }
// When the body is of the data type we just return this raw data.
if let body = body as? Data { return body }
// In all other cases we try to parse the json.
// When the body implements Encodable we try to encode it to json.
if let body = body as? Encodable {
return try JSONEncoder().encode(body)
}

// In all other cases we try to encode it to json.
return try JSONSerialization.data(withJSONObject: body, options: [])
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public protocol Request {
var query: RequestQuery? { get }
/// Set the headers for this request.
var headers: RequestHeaders? { get }
/// Set the type of body with it's content. In Cara's case we support 2 major types of body:
/// Set the type of body with it's content. In Cara's case we support 3 major types of body:
/// - A raw `Data` object
/// - An `Any` object that can be parsed as a json.
/// - A type that implements `Encodable`
var body: Any? { get }
/// Set a cache policy for every request.
var cachePolicy: URLRequest.CachePolicy { get }
Expand Down

0 comments on commit 51309a5

Please sign in to comment.