Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- struct ErrorModel {
- let code: Int
- let title: String
- let message: String
- }
- struct User {
- let firstName: String
- let lastName: String
- let phoneNumber: String
- let emailAddress: String
- }
- protocol AuthService {
- func authenticateUser(emailAddress: String, password: String, completionHandler: (User?) -> Void)
- }
- protocol AuthServiceComponent {
- static var authService: AuthService { get }
- static func createAuthService() -> AuthService
- }
- struct TestAuthService: AuthService {
- func authenticateUser(emailAddress: String, password: String, completionHandler: (User?) -> Void) {
- if emailAddress == "nishant.shrestha@hotmail.com" && password == "password" {
- let user = User(firstName: "Nishant", lastName: "Shrestha", phoneNumber: "0478578673", emailAddress: emailAddress)
- completionHandler(user)
- } else {
- completionHandler(nil)
- }
- }
- }
- protocol TestAuthServiceComponent: AuthServiceComponent {}
- extension TestAuthServiceComponent {
- static func createAuthService() -> AuthService {
- return TestAuthService()
- }
- }
- enum TestContext: TestAuthServiceComponent {
- public static let authService = TestContext.createAuthService()
- }
- let testEmailAddress = "nishant.shrestha@hotmail.com"
- let testPassword = "password"
- TestContext.authService.authenticateUser(emailAddress: testEmailAddress, password: testPassword, completionHandler: { (user) in
- guard let user = user else { return }
- print("test authentication successful.")
- print(user)
- print("----")
- })
- struct AlamofireAuthService: AuthService {
- func authenticateUser(emailAddress: String, password: String, completionHandler: (User?) -> Void) {
- let params = [
- "email_address": emailAddress,
- "password": password
- ]
- // TODO: Implement network request
- let user = User(firstName: "Nishant", lastName: "Shrestha", phoneNumber: "0478578673", emailAddress: emailAddress)
- completionHandler(user)
- }
- }
- protocol AlamofireAuthServiceComponent: AuthServiceComponent {}
- extension AlamofireAuthServiceComponent {
- static func createAuthService() -> AuthService {
- return AlamofireAuthService()
- }
- }
- enum ProductionContext: AlamofireAuthServiceComponent {
- public static let authService = ProductionContext.createAuthService()
- }
- ProductionContext.authService.authenticateUser(emailAddress: "nishant.shrestha@hotmail.com", password: "password") { (user) in
- if let user = user {
- print("production authentication succesful")
- print(user)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement