From 2caf48448010fc1594584327e0b3395f49bb7389 Mon Sep 17 00:00:00 2001 From: Sacha DSO Date: Mon, 13 Nov 2023 16:36:09 -1000 Subject: [PATCH] Update README.md Updates demo code showcasing native vs Networking --- README.md | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fec5cc1..a99bc1d 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,30 @@ Networking brings together `URLSession`, `async`-`await` (or `Combine` ), `Decodable` and `Generics` to simplify connecting to a JSON api. -```swift -struct Api: NetworkingService { +## Demo time 🍿 - let network = NetworkingClient(baseURL: "https://jsonplaceholder.typicode.com") +Networking turns this: +```swift +func nativePost() async throws -> User { + let config = URLSessionConfiguration.default + let urlSession = URLSession(configuration: config, delegate: nil, delegateQueue: nil) + var urlRequest = URLRequest(url: URL(string: "https://jsonplaceholder.typicode.com/users")!) + urlRequest.httpMethod = "POST" + urlRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") + urlRequest.httpBody = "firstname=Alan&lastname=Turing".data(using: .utf8) + let (data, _) = try await urlSession.data(for: urlRequest) + let decoder = JSONDecoder() + let user = try decoder.decode(User.self, from: data) + return user +} +``` - func fetchPost() async throws -> Post { - try await get("/posts/1") - } +into: +```swift +let network = NetworkingClient(baseURL: "https://jsonplaceholder.typicode.com") + +func post() async throws -> User { + try await network.post("/users", params: ["username" : "John"]) } ```