This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
NewerOlder