Untitled

mail@pastecode.io avatar
unknown
swift
3 years ago
2.2 kB
4
Indexable
Never
//
//  JSONTools.swift
//  ECE564_HW
//
//  Created by Tarun Amasa on 2/7/22.
//  Copyright © 2022 ECE564. All rights reserved.
//

import Foundation
import UIKit

let token: String = "7ADFBC8F8F17208F66FAE5A6FE1B90C1"
let username: String = "tra23"
let urlString: String = "http://kitura-fall-2021.vm.duke.edu:5640/b64entries"
let loginString = "\(username):\(token)"
let kiturl = URL(string: urlString)

func loadServerData()->Void{
    print(loginString)
    
    let request = NSMutableURLRequest(url: kiturl!)
    
    request.httpMethod = "GET"
    guard let loginData = loginString.data(using: String.Encoding.utf8) else {
                return
            }
            let base64LoginString = loginData.base64EncodedString()
    
            request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
            request.addValue("application/json", forHTTPHeaderField: "Accept")
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if let error = error {
                print("Error with fetching films: \(error)")
                return
              }
              
              guard let httpResponse = response as? HTTPURLResponse,
                    (200...299).contains(httpResponse.statusCode) else {
                print("Error with the response, unexpected status code:")
                return
                        
              }
        
        guard let data = data else {
                print("URLSession dataTask error:", error ?? "nil")
                return
            }

            do {
                let newEntries = try JSONDecoder().decode(CodableDukePerson.self, from: data)
                
//                for codable in newEntries! {
//
//                                newPerson = codableToDuke(codable: codable)
//
//
//                            }
            } catch {
                print("JSONSerialization error:", error)
            }
        
              
    })
    dataTask.resume()
    print("No way")
}