This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import Foundation | |
let baseURL = "http://localhost:8080" | |
enum APIError: Error { | |
case invalidBody | |
case invalidEndpoint | |
case invalidURL | |
case emptyData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
// With Combine we return a DataTaskPublisher instead of using the completion handler of the DataTask | |
func postUser(user: User) throws -> URLSession.DataTaskPublisher { | |
let headers = [ | |
"Content-Type": "application/json", | |
"cache-control": "no-cache", | |
] | |
let encoder = JSONEncoder() | |
guard let postData = try? encoder.encode(user) else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In a real world App we want to decode our JSON data response into a Codable Object | |
struct CreateUserResponse: Codable { | |
let id: Int | |
let email: String | |
let name: String | |
} | |
// Decoding CreateUserResponse inside a DataTask API client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Refactoring of the URLRequest with dataTask avoiding Throws using fatalError | |
func buildCreateUserURLRequest(user: User) -> URLRequest { | |
let headers = [ | |
"Content-Type": "application/json", | |
"cache-control": "no-cache", | |
] | |
let encoder = JSONEncoder() | |
guard let postData = try? encoder.encode(user) else { | |
fatalError("APIError.invalidBody") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Login | |
func buildLoginRequest(email: String, password: String) throws -> URLRequest { | |
let loginString = String(format: "%@:%@", email, password) | |
let loginData: Data = loginString.data(using: .utf8)! | |
let base64LoginString = loginData.base64EncodedString() | |
let headers = [ | |
"Content-Type": "application/json", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Completing the API with Combine and dataTaskPublisher | |
// POST - Login | |
func buildLoginRequest(email: String, password: String) -> URLRequest { | |
let loginString = String(format: "%@:%@", email, password) | |
let loginData: Data = loginString.data(using: .utf8)! | |
let base64LoginString = loginData.base64EncodedString() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Let's use our brand new Publishers | |
let todoList = [Todo(id: nil, title: "Learn Composite"), | |
Todo(id: nil, title: "Learn SwiftUI")] | |
// use login to get the Bearer Token | |
let cancellableLogin = login(email: "[email protected]", password: "password2") | |
.sink(receiveCompletion: { (completion) in | |
switch completion { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use of the dataTaskPublisher API to implement Publisher with the decoded data | |
struct Token: Codable { | |
let string: String | |
} | |
// We'll use the following function to validate our dataTaskPublisher output in the pipeline | |
func validate(_ data: Data, _ response: URLResponse) throws -> Data { | |
guard let httpResponse = response as? HTTPURLResponse else { | |
throw APIError.invalidResponse | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Combining login and postTodo in a single call | |
func post(email: String, password: String, todo: Todo) -> AnyPublisher<Todo, Error>? { | |
return login(email: email, password: password) | |
.map { token -> String in | |
return token.string | |
} | |
.flatMap { (token) -> AnyPublisher<Todo, Error> in | |
return postTodo(authToken: token, todo: todoList[1]) | |
} | |
.eraseToAnyPublisher() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import Foundation | |
enum APIError: Error { | |
case invalidBody | |
case invalidEndpoint | |
case invalidURL | |
case emptyData | |
case invalidJSON | |
case invalidResponse |
OlderNewer