Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. {
  2. customer = (
  3. {
  4. "customer_comment" = No;
  5. "customer_id" = 128;
  6. "customer_mobile" = 12345678;
  7. "customer_name" = testing;
  8. date = "2019-05-28";
  9. deleted = 0;
  10. "device_token" = "";
  11. "device_type" = iOS;
  12. discount = "No Data";
  13. dob = "2019-05-28";
  14. email = "testing@gmail.com";
  15. gender = Male;
  16. "invoice_no" = 0;
  17. "item_id" = 0;
  18. "lucky_date" = "0000-00-00 00:00:00";
  19. "no_of_attempts" = 0;
  20. notifications = 1;
  21. "on_hold_credit" = 0;
  22. password = 123456;
  23. status = 1;
  24. "total_credits" = 0;
  25. "used_lucky" = No;
  26. "used_weekly" = No;
  27. "weekly_date" = "0000-00-00 00:00:00";
  28. }
  29. );
  30. value = 1;
  31.  
  32. struct MyAccount: Codable {
  33. let value: Int
  34. let customer: [Customer]
  35.  
  36. enum CodingKeys: String, CodingKey {
  37. case customerID = "customer_id"
  38. case customerName = "customer_name"
  39. case weeklyDate = "weekly_date"
  40. case usedWeekly = "used_weekly"
  41. case itemID = "item_id"
  42. case luckyDate = "lucky_date"
  43. case usedLucky = "used_lucky"
  44. case discount
  45. case invoiceNo = "invoice_no"
  46. case noOfAttempts = "no_of_attempts"
  47. case email
  48. case customerMobile = "customer_mobile"
  49. case password
  50. case customerComment = "customer_comment"
  51. case date
  52. case deviceToken = "device_token"
  53. case deviceType = "device_type"
  54. case notifications
  55. case totalCredits = "total_credits"
  56. case onHoldCredit = "on_hold_credit"
  57. case status
  58. case deleted
  59. case dob
  60. case gender
  61. }
  62.  
  63. public func get<T:Codable>(url:String , parameters:Dictionary<String, Any>? ,decodingType:T.Type, onCompletion:@escaping(T?) -> Void, onError:@escaping(String) -> Void) {
  64.  
  65. if Connectivity.isConnectedToInternet() {
  66. print("Yes! internet is available.")
  67. // do some tasks..
  68. AF.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (responseObject) in
  69. print("API RESULT >> (url) >> (responseObject)")
  70. if responseObject.result.isSuccess {
  71. decoderProtocol.decode(data: responseObject.data, decodingType: decodingType, onCompletion: { result in
  72. onCompletion(result)
  73. }, onError: {error in
  74. onError(error)
  75. })
  76. }
  77. else {
  78. onError(responseObject.result.description)
  79. }
  80. }
  81. }
  82. else {
  83. let window = UIApplication.shared.keyWindow
  84. window?.makeToast("No Internet Connection!")
  85. SVProgressHUD.dismiss()
  86. }
  87. }
  88.  
  89. static func decode<T:Codable>(data:Data?, decodingType:T.Type ,onCompletion:@escaping(T) -> Void , onError:@escaping(String) -> Void) {
  90. guard let dataObject = data else {
  91. onError("No Response")
  92. return
  93. }
  94. let decoder = JSONDecoder()
  95. do {
  96. let result = try decoder.decode(T.self, from: dataObject)
  97. onCompletion(result)
  98. }
  99. catch let err{
  100. print(err.localizedDescription)
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement