Skip to content

Instantly share code, notes, and snippets.

@Kmohamed
Created March 2, 2019 14:50
Show Gist options
  • Save Kmohamed/6e0057ef369723b0748755263b83545d to your computer and use it in GitHub Desktop.
Save Kmohamed/6e0057ef369723b0748755263b83545d to your computer and use it in GitHub Desktop.
open func executeGETRequest(api:String, completionBlock:@escaping (Data?) -> Void) {
let environment = ProcessInfo.processInfo.environment
var baseUrl:String = ""
if let _ = environment["isUITest"] {
// Running in a UI test
baseUrl = ProcessInfo.processInfo.environment["BASEURL"]!
} else {
baseUrl = "https://api.example.com"
}
guard let gitUrl = URL(string: baseUrl + api) else { return }
let session = URLSession(configuration: URLSessionConfiguration.default)
let dataTask = session.dataTask(with: gitUrl) { (data, response, error) in
guard let data = data else { return }
do {
if let returnData = String(data: data, encoding: .utf8) {
print(returnData)
} else {
print("empty")
}
}
if let err = error {
print("Err", err)
}
completionBlock(data)
}
dataTask.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment