title: Swift - Multiple URL Request - Code To Refactor and To Reuse
body: <p>I'm new to Swift and I am trying to refactor my URL Post requests. I have multiple URL POST requests inside the same View Controller like this. Everything works fine but it seems to me that there is a lot of repetitive code that could be reused. Particularly, I don't know how to pass/handle different Data Models that should be used in parseRequest1 and parseRequest2. I also read that there should be only one session used for URL requests within the same project. Any help would be greatly appreciate it!</p>
  
  <pre><code>func request1() {        
      let parameters = [...//some parameters to send]
      guard let url = URL(string: "https//www.....") else {return}
      var request = URLRequest(url: url)
      request.httpMethod = "POST"
      request.addValue("application/json", forHTTPHeaderField: "Content-Type")
      guard let parametersToSend = try? JSONSerialization.data(withJSONObject: parameters, options: []) 
         else {
           print("Error")
           return
         }
      request.httpBody = parametersToSend
      let session = URLSession.shared
      session.dataTask(with: request) { (data, response, error) in
         if let safeData = data {
            self.parseRequest1(data: safeData)
          }
     }.resume()
  }
  
  func parseRequest1(data: Data){
      let decoder = JSONDecoder()
      do{
        let decodedData = try decoder.decode(DataModelForRequest1.self, from: data)
          DispatchQueue.main.async {
             self.performAction1(request1Result)
          }
      } catch {
         print(error)
      }
  } 
  </code></pre>
  
  <p>Then I have another URL request request2 which is almost identical except the parameters, and model to be used for decoding and action inside parseRequest2.</p>
  
  <pre><code>func request2() {        
          let parameters = [...//some parameters to send]
          guard let url = URL(string: "https//www.....") else {return}
          var request = URLRequest(url: url)
          request.httpMethod = "POST"
          request.addValue("application/json", forHTTPHeaderField: "Content-Type")
          guard let parametersToSend = try? JSONSerialization.data(withJSONObject: parameters, options: []) 
             else {
               print("Error")
               return
             }
          request.httpBody = parametersToSend
          let session = URLSession.shared
          session.dataTask(with: request) { (data, response, error) in
             if let safeData = data {
                self.parseRequest2(data: safeData)
              }
         }.resume()
      }
  
      func parseRequest2(data: Data){
          let decoder = JSONDecoder()
          do{
            let decodedData = try decoder.decode(DataModelForRequest2.self, from: data)
              DispatchQueue.main.async {
                 self.performAction2(request2Result)
              }
          } catch {
             print(error)
          }
      } 
  </code></pre>