Created
July 17, 2021 17:44
-
-
Save vialyx/b3b54a0ebea82fff32c2cd6512aa25e3 to your computer and use it in GitHub Desktop.
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
// | |
// ViewModel.swift | |
// UnitTesting | |
// | |
// Created by Maksim Vialykh on 7/17/21. | |
// | |
import Foundation | |
protocol ViewModel: AnyObject { | |
var onError: ((Error) -> Void)? { get set } | |
func signup(username: String?, password: String?) | |
} | |
enum SignupError: Error { | |
case emptyUsername | |
case emptyPassword | |
} | |
class DefaultViewModel: ViewModel { | |
var onError: ((Error) -> Void)? | |
private let client: NetworkClient | |
private let router: Router | |
init(client: NetworkClient, router: Router) { | |
self.client = client | |
self.router = router | |
} | |
func signup(username: String?, password: String?) { | |
guard let username = username else { | |
onError?(SignupError.emptyUsername) | |
return | |
} | |
guard let password = password else { | |
onError?(SignupError.emptyPassword) | |
return | |
} | |
/* | |
{ | |
"id": 0, | |
"username": "string", | |
"firstName": "string", | |
"lastName": "string", | |
"email": "string", | |
"password": "string", | |
"phone": "string", | |
"userStatus": 0 | |
} | |
https://petstore.swagger.io/#/user/createUser | |
*/ | |
struct User: Encodable { | |
let id = 0 | |
var username = "" | |
let firstName = "" | |
let lastName = "" | |
let email = "" | |
var password = "" | |
let phone = "" | |
let userStatus = "" | |
init(username: String, password: String) { | |
self.username = username | |
self.password = password | |
} | |
} | |
var request = URLRequest(url: URL(string: "https://petstore.swagger.io/v2/user")!) | |
let user = User(username: username, password: password) | |
request.httpMethod = "POST" | |
request.httpBody = try! JSONEncoder().encode(user) | |
client.send(request: request) { result in | |
switch result { | |
case .success(let data): | |
// serialize data | |
print("response: \(String(data: data, encoding: .utf8))") | |
DispatchQueue.main.async { [weak self] in | |
self?.router.openMainView() | |
} | |
case .failure(let error): | |
DispatchQueue.main.async { [weak self] in | |
self?.onError?(error) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment