 //
 
 import Foundation
+import IPaLog
 public typealias IPaURLResourceUISuccessHandler = ((URLResponse?,Any?) -> ())!
 public typealias IPaURLResourceUIFailHandler = ((Error) -> ())!
 open class IPaURLResourceUI : NSObject,URLSessionDelegate {
-    var baseURL:String! = ""
+    open var baseURL:String! = ""
     open var removeNSNull:Bool = true
     lazy var sessionConfiguration:URLSessionConfiguration = URLSessionConfiguration.default
     lazy var urlSession:Foundation.URLSession = Foundation.URLSession(configuration: self.sessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)

        
        return apiWithRequest(request as URLRequest,complete:complete,failure:failure)
    }
    func apiPost(_ api:String , param:[String:Any]?,complete:IPaURLResourceUISuccessHandler,failure:IPaURLResourceUIFailHandler) -> URLSessionDataTask
    {
        return apiPerform(api,method:"POST",paramInBody:param,complete:complete,failure:failure)
    }
    
    
    func apiPut(_ api:String, param:[String:Any]?,complete:IPaURLResourceUISuccessHandler,failure:IPaURLResourceUIFailHandler) -> URLSessionDataTask {
        
        return apiPerform(api ,method:"PUT", paramInBody:param,complete:complete,failure:failure)
    }
    func apiUpload(_ api:String,method:String,json:Any,complete:IPaURLResourceUISuccessHandler,failure:IPaURLResourceUIFailHandler) {
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions(rawValue: 0))
            _ = apiUpload(api, method: method, headerParam: ["content-type":"application/json"], data: jsonData, complete: complete, failure: failure)

        task.resume()
        return task
    }
    open func apiUpload(_ api:String,method:String,multiPartFormData:IPaURLMultipartFormData,complete:IPaURLResourceUISuccessHandler,failure:IPaURLResourceUIFailHandler) -> URLSessionUploadTask {
        let contentType = "multipart/form-data boundary=\(multiPartFormData.boundary)"
        multiPartFormData.endOfBodyData()
        let data = multiPartFormData.data
        return apiUpload(api, method: method, headerParam: ["content-type":contentType], data: data as Data, complete: complete, failure: failure)
    }
    open func apiPut(_ api:String,contentType:String,postData:Data,complete:IPaURLResourceUISuccessHandler,failure:IPaURLResourceUIFailHandler) -> URLSessionUploadTask {
        return apiUpload(api, method: "PUT", headerParam: ["content-type":contentType], data: postData, complete: complete, failure: failure)
    }
    open func apiPost(_ api:String,contentType:String,postData:Data,complete:IPaURLResourceUISuccessHandler,failure:IPaURLResourceUIFailHandler) -> URLSessionUploadTask {
        return apiUpload(api, method: "POST", headerParam: ["content-type":contentType], data: postData, complete: complete, failure: failure)
    }
    open func apiDelete(_ api:String,contentType:String,postData:Data,complete:IPaURLResourceUISuccessHandler,failure:IPaURLResourceUIFailHandler) -> URLSessionUploadTask {
        return apiUpload(api, method: "DELETE", headerParam: ["content-type":contentType], data: postData, complete: complete, failure: failure)
    }
    
