Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics Decodable And Encodable JSON #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Helper4Swift/Classes/Extensions/GenericRESTClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// ViewController.swift
// GenericsDecodableAndEncodableJSON
//
// Created by X901 on 11/08/2018.
// Copyright © 2018 X901. All rights reserved.
//

import Foundation

protocol GenericRESTClient {
func getGenericData<T: Decodable>(urlString: String, completion: @escaping (T) -> ())
func postGenericData<T:Encodable>(urlString: String, parameter: T, completion: @escaping (Data?, URLResponse?, Error?) -> ())
}

extension GenericRESTClient {

func getGenericData<T: Decodable>(urlString: String, completion: @escaping (T) -> ()) {

let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if let error = error {
print("Failed to fetch data:", error)
return
}


guard let data = data else { return }

do {
let obj = try JSONDecoder().decode(T.self, from: data)
completion(obj)
} catch let jsonErr {
print("Failed to decode json:", jsonErr)
}
}.resume()
}

func postGenericData<T:Encodable>(urlString: String, parameter: T, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {

let url = URL(string: urlString)
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

do {
let jsonBody = try JSONEncoder().encode(parameter)

request.httpBody = jsonBody





} catch let jsonErr {
print("Failed to encode json:", jsonErr)
}


let session = URLSession.shared
_ = session.dataTask(with: request) { (data,response, error) in



completion(data,response, error)




}.resume()

}

}