title: Refresh Bearer Token and resume current API
body: <p>I need to resume my WebService if my bearer token expire by http code 401 ,
  below is my code.</p>
  <ul>
  <li>When a 401 error occurs that means the Access Token has expired and I need to send a Refresh Token over to my server to obtain a new Access Token.</li>
  <li>After getting a new Access Token I need to redo the previous request that got the 401 error.</li>
  </ul>
  <p><strong>Suppose I hit webservice1 and its give 401 http code , then new bearer token request will generate and the same API resume to work. How can I do it ?</strong></p>
  <pre><code>import UIKit
  import CryptoKit
  
  
  class SharedWebService: NSObject {
      static let sharedApiInstance = SharedWebService()
      func generalApiMethod (parameter : NSDictionary ,completion: @escaping ((NSMutableArray?) -&gt; Void))
      {
          var dashboarddata : NSMutableArray = NSMutableArray()
          let urlString = String(format:&quot;URL OF API HERE&quot;)
          let url = URL(string: urlString)
          guard let requestUrl = url else { fatalError() }
          var request = URLRequest(url: requestUrl)
          request.timeoutInterval = 60
        
          let bearerToken = &quot;current bearer token&quot;
          request.setValue(bearerToken, forHTTPHeaderField: &quot;Authorization&quot;)
          request.httpMethod = &quot;GET&quot;
          request.setValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Accept&quot;)
          URLSession.shared.dataTask(with: request) {
              (data, response, error) in
              if let httpResponse = response as? HTTPURLResponse {
                     print(httpResponse.statusCode)
                  if httpResponse.statusCode == 401
                  {
                     // Refresh bearerToken get here
                     let bearerToken = self.getBearerTokenDevice() //fetch api to get new bearer token 
                      return
                  }
                 }
              guard error == nil else {
                  print(error!.localizedDescription);
                  DispatchQueue.main.async{
                      completion(dashboarddata)
                  }
                  return
              }
              guard let jsonData = data else { print(&quot;Empty data&quot;); return }
              if String(data: jsonData, encoding:  String.Encoding.utf8) != nil
              {
                 
                  do {
                      let responseJSON = try? JSONSerialization.jsonObject(with: jsonData, options: [])
                      if let responseJSON = responseJSON as? [String: Any] {
                        
                         //Success case do here reponse return
                          completion(dashboarddata)
  
                      }
                  }
                  
                  
              }
          }.resume()
      }
      
      
      func getBearerTokenDevice()-&gt; String
      {
          //how to handle it
          
          return &quot;New bearer token from api fetch&quot;
      }
      
      
      
  }
  </code></pre>