Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- customer = (
- {
- "customer_comment" = No;
- "customer_id" = 128;
- "customer_mobile" = 12345678;
- "customer_name" = testing;
- date = "2019-05-28";
- deleted = 0;
- "device_token" = "";
- "device_type" = iOS;
- discount = "No Data";
- dob = "2019-05-28";
- email = "testing@gmail.com";
- gender = Male;
- "invoice_no" = 0;
- "item_id" = 0;
- "lucky_date" = "0000-00-00 00:00:00";
- "no_of_attempts" = 0;
- notifications = 1;
- "on_hold_credit" = 0;
- password = 123456;
- status = 1;
- "total_credits" = 0;
- "used_lucky" = No;
- "used_weekly" = No;
- "weekly_date" = "0000-00-00 00:00:00";
- }
- );
- value = 1;
- struct MyAccount: Codable {
- let value: Int
- let customer: [Customer]
- enum CodingKeys: String, CodingKey {
- case customerID = "customer_id"
- case customerName = "customer_name"
- case weeklyDate = "weekly_date"
- case usedWeekly = "used_weekly"
- case itemID = "item_id"
- case luckyDate = "lucky_date"
- case usedLucky = "used_lucky"
- case discount
- case invoiceNo = "invoice_no"
- case noOfAttempts = "no_of_attempts"
- case email
- case customerMobile = "customer_mobile"
- case password
- case customerComment = "customer_comment"
- case date
- case deviceToken = "device_token"
- case deviceType = "device_type"
- case notifications
- case totalCredits = "total_credits"
- case onHoldCredit = "on_hold_credit"
- case status
- case deleted
- case dob
- case gender
- }
- public func get<T:Codable>(url:String , parameters:Dictionary<String, Any>? ,decodingType:T.Type, onCompletion:@escaping(T?) -> Void, onError:@escaping(String) -> Void) {
- if Connectivity.isConnectedToInternet() {
- print("Yes! internet is available.")
- // do some tasks..
- AF.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (responseObject) in
- print("API RESULT >> (url) >> (responseObject)")
- if responseObject.result.isSuccess {
- decoderProtocol.decode(data: responseObject.data, decodingType: decodingType, onCompletion: { result in
- onCompletion(result)
- }, onError: {error in
- onError(error)
- })
- }
- else {
- onError(responseObject.result.description)
- }
- }
- }
- else {
- let window = UIApplication.shared.keyWindow
- window?.makeToast("No Internet Connection!")
- SVProgressHUD.dismiss()
- }
- }
- static func decode<T:Codable>(data:Data?, decodingType:T.Type ,onCompletion:@escaping(T) -> Void , onError:@escaping(String) -> Void) {
- guard let dataObject = data else {
- onError("No Response")
- return
- }
- let decoder = JSONDecoder()
- do {
- let result = try decoder.decode(T.self, from: dataObject)
- onCompletion(result)
- }
- catch let err{
- print(err.localizedDescription)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement