Skip to main content

Posts

Showing posts from May, 2023

Swift API Manager -Alamofire-Refresh Token-With TestCases

  import Foundation import KeychainAccess enum APIError : Error { case accessTokenExpired case networkError // Add more error cases as needed } class APIManager { private let keychain = Keychain (service: "com.example.app.refreshToken" ) private let refreshTokenKey = "refreshToken" private var accessToken: String ? func callAPI < T : Codable >( urlString : String , method : String , parameters : [ String : Any ] ? , completion : @escaping ( Result < T , APIError >) -> Void ) { guard let url = URL (string: urlString) else { completion(.failure(.networkError)) return } var request = URLRequest (url: url) request.httpMethod = method // Add access token to the request headers if available if let token = accessToken { request.setValue( "Bearer \(token) " , forHTTPHeaderField: "Aut...

SOLID Principles in Swift - ( Single Responsibility Principle(SRP), Open/Closed Principle, Liskov Substitution Principle, Interface Segregation, Dependency Inversion)

  SOLID Principles in Swift Single Responsibility Principle  Open/Closed Principle  Liskov Substitution Principle  Interface Segregation  Dependency Inversion S - Single Responsibility Principle(SRP)  Single Responsibility Principle (SRP) is that every class, module, or function in a program should have one responsibility(Task) in a program. Example:-  A Simple Class For multiple Responsibility  class SimpleHandler {           func simpleHandle() {                 let data = requestToDataAPI()                 let array = parseToResponse(data: data)                 saveToDatabase(array: array)         }           private func requestToDataAPI() -> Data {                 // Networ...