Skip to content

Instantly share code, notes, and snippets.

@LeoValentim
Last active July 31, 2017 14:11
Show Gist options
  • Save LeoValentim/19a1849b893497617e4d12e2a75eb97f to your computer and use it in GitHub Desktop.
Save LeoValentim/19a1849b893497617e4d12e2a75eb97f to your computer and use it in GitHub Desktop.
Webservice swift class for HTTP requests
//
// Webservice.swift
//
// Created by Mac on 29/07/17.
// Copyright © 2017 Leo Valentim. All rights reserved.
//
import SwiftyJSON
import Foundation
struct Webservice {
static func post(_ urlString: String, params: JSON, headers: [String:String], completion: @escaping (Data!) -> ()) {
DispatchQueue.global(qos: .userInitiated).async {
let request = NSMutableURLRequest(url: URL(string: urlString)!)
let session = URLSession.shared
let group = DispatchGroup()
var dataBody: Data!
var responseData: Data?
request.httpMethod = "POST"
for (Key, Value) in headers {
request.setValue(Value, forHTTPHeaderField: Key)
}
if (headers.filter{ h in h.value.lowercased() == "application/x-www-form-urlencoded" }).count > 0 {
var param = ""
for (Key, Value) in params {
if let val = Value.string {
let paramStringFormat = "\(Key)=\(val)"
param = (param != "" ? "\(param)&\(paramStringFormat)" : paramStringFormat)
}
}
dataBody = param.data(using: String.Encoding.utf8, allowLossyConversion: true)
} else {
dataBody = try? params.rawData(options: .prettyPrinted)
}
request.httpBody = dataBody!
group.enter()
session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
responseData = data
group.leave()
}).resume()
_ = group.wait(timeout: DispatchTime.distantFuture)
DispatchQueue.main.async {
completion(responseData)
}
}
}
static func get(_ urlString: String, headers: [String:String], completion: @escaping (Data!) -> ()) {
DispatchQueue.global(qos: .userInitiated).async {
let request = NSMutableURLRequest(url: URL(string: urlString)!)
let session = URLSession.shared
let group = DispatchGroup()
var responseData: Data?
request.httpMethod = "GET"
for (Key, Value) in headers {
request.setValue(Value, forHTTPHeaderField: Key)
}
group.enter()
session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
responseData = data
group.leave()
}).resume()
_ = group.wait(timeout: DispatchTime.distantFuture)
DispatchQueue.main.async {
completion(responseData)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment