mirror of
https://github.com/mas-cli/mas
synced 2024-11-25 21:10:24 +00:00
Merge pull request #53 from garnett/modern-code
Update small bits of code to make them more swift (3) like
This commit is contained in:
commit
37b17c7b5e
2 changed files with 15 additions and 14 deletions
|
@ -24,7 +24,7 @@ enum MASError: Error, CustomStringConvertible {
|
|||
case .notSignedIn:
|
||||
return "Not signed in"
|
||||
|
||||
case .signInFailed(error: let error):
|
||||
case .signInFailed(let error):
|
||||
if let error = error {
|
||||
return "Sign in failed: \(error.localizedDescription)"
|
||||
} else {
|
||||
|
@ -34,13 +34,14 @@ enum MASError: Error, CustomStringConvertible {
|
|||
case .alreadySignedIn:
|
||||
return "Already signed in"
|
||||
|
||||
case .purchaseFailed(error: let error):
|
||||
case .purchaseFailed(let error):
|
||||
if let error = error {
|
||||
return "Download request failed: \(error.localizedDescription)"
|
||||
} else {
|
||||
return "Download request failed"
|
||||
}
|
||||
case .downloadFailed(error: let error):
|
||||
|
||||
case .downloadFailed(let error):
|
||||
if let error = error {
|
||||
return "Download failed: \(error.localizedDescription)"
|
||||
} else {
|
||||
|
|
|
@ -17,15 +17,15 @@ public extension URLSession {
|
|||
/// Return data from synchronous URL request
|
||||
public static func requestSynchronousData(_ request: URLRequest) -> Data? {
|
||||
var data: Data? = nil
|
||||
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)
|
||||
let task = URLSession.shared.dataTask(with: request, completionHandler: {
|
||||
let semaphore = DispatchSemaphore(value: 0)
|
||||
let task = URLSession.shared.dataTask(with: request) {
|
||||
taskData, _, error -> () in
|
||||
data = taskData
|
||||
if data == nil, let error = error {print(error)}
|
||||
semaphore.signal();
|
||||
})
|
||||
semaphore.signal()
|
||||
}
|
||||
task.resume()
|
||||
let _ = semaphore.wait(timeout: DispatchTime.distantFuture)
|
||||
let _ = semaphore.wait(timeout: .distantFuture)
|
||||
return data
|
||||
}
|
||||
|
||||
|
@ -37,18 +37,18 @@ public extension URLSession {
|
|||
}
|
||||
|
||||
/// Return JSON synchronous from URL request
|
||||
public static func requestSynchronousJSON(_ request: URLRequest) -> AnyObject? {
|
||||
public static func requestSynchronousJSON(_ request: URLRequest) -> Any? {
|
||||
guard let data = URLSession.requestSynchronousData(request) else {return nil}
|
||||
return try! JSONSerialization.jsonObject(with: data, options: []) as AnyObject?
|
||||
return try! JSONSerialization.jsonObject(with: data, options: [])
|
||||
}
|
||||
|
||||
/// Return JSON synchronous from specified endpoint
|
||||
public static func requestSynchronousJSONWithURLString(_ requestString: String) -> AnyObject? {
|
||||
public static func requestSynchronousJSONWithURLString(_ requestString: String) -> Any? {
|
||||
guard let url = URL(string: requestString) else {return nil}
|
||||
let request = NSMutableURLRequest(url:url)
|
||||
var request = URLRequest(url:url)
|
||||
request.httpMethod = "GET"
|
||||
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
return URLSession.requestSynchronousJSON(request as URLRequest)
|
||||
return URLSession.requestSynchronousJSON(request)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,7 +56,7 @@ public extension String {
|
|||
|
||||
/// Return an URL encoded string
|
||||
func URLEncodedString() -> String? {
|
||||
let customAllowedSet = CharacterSet.urlQueryAllowed
|
||||
let customAllowedSet = CharacterSet.urlQueryAllowed
|
||||
return addingPercentEncoding(withAllowedCharacters: customAllowedSet)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue