Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Updates demo code showcasing native vs Networking
  • Loading branch information
s4cha authored Nov 14, 2023
1 parent 9364dc9 commit 2caf484
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
```

Expand Down

0 comments on commit 2caf484

Please sign in to comment.