Setup Voice Verification

Uploading Api details to matches enrolled user's audio files with target user's audio file

    func uploadRecordings(fileURLs: [URL], completion: @escaping (Result<Response, Error>) -> Void) {
        let endpoint = "Your API"
        let parameters = [
            "sourceone": fileURLs[0],  // Input One in .wav formate
            "sourcetwo": fileURLs[1], // Input Two in .wav formate
            "sourcethree": fileURLs[2], // Input Three in .wav formate
            "target": fileURLs[3] // Input Four in .wav formate
        ]
        
        guard let url = URL(string: endpoint) else {
            completion(.failure(NSError(domain: "Invalid URL", code: -1, userInfo: nil)))
            return
        }
        
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        
        let boundary = "Boundary-\(UUID().uuidString)"
        request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
        
        var body = Data()
        
        for (key, fileURL) in parameters {
            do {
                let fileData = try Data(contentsOf: fileURL)
                body.append("--\(boundary)\r\n".data(using: .utf8)!)
                body.append("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(fileURL.lastPathComponent)\"\r\n".data(using: .utf8)!)
                body.append("Content-Type: audio/wav\r\n\r\n".data(using: .utf8)!)
                body.append(fileData)
                body.append("\r\n".data(using: .utf8)!)
            } catch {
                print("Error reading file \(fileURL.lastPathComponent): \(error)")
                completion(.failure(error))
                return
            }
        }
        
        body.append("--\(boundary)--\r\n".data(using: .utf8)!)
        request.httpBody = body
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            if let error = error {
                completion(.failure(error))
                return
            }
            
            guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
                let statusCode = (response as? HTTPURLResponse)?.statusCode ?? -1
                let error = NSError(domain: "HTTPError", code: statusCode, userInfo: nil)
                completion(.failure(error))
                return
            }
            
            guard let data = data else {
                completion(.failure(NSError(domain: "NoData", code: -1, userInfo: nil)))
                return
            }
            
            do {
                let decodedResponse = try JSONDecoder().decode(Response.self, from: data)
                completion(.success(decodedResponse))
            } catch let decodeError {
                completion(.failure(decodeError))
            }
        }
        
        task.resume()
    }

Last updated