-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added URL session. Added sorts. Added transforms.
- Loading branch information
1 parent
c7dc54f
commit 324f9fe
Showing
5 changed files
with
123 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
Sources/SwiftBoost/Foundation/Extensions/MutableCollectionExtension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Foundation | ||
|
||
public extension MutableCollection where Self: RandomAccessCollection { | ||
|
||
func sorted<Value>( | ||
by keyPath: KeyPath<Element, Value>, | ||
using valuesAreInIncreasingOrder: (Value, Value) throws -> Bool | ||
) rethrows -> [Element] { | ||
try sorted { | ||
try valuesAreInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath]) | ||
} | ||
} | ||
|
||
func sorted<Value: Comparable>( | ||
by keyPath: KeyPath<Element, Value>, | ||
order: MutableCollectionOrder<Value> | ||
) -> [Element] { | ||
sorted(by: keyPath, using: order.operator) | ||
} | ||
} | ||
|
||
public enum MutableCollectionOrder<Value: Comparable> { | ||
|
||
// MARK: - Cases | ||
|
||
/// Represents ascending order. In this case, the associated operator function is `<`. | ||
case ascending | ||
/// Represents descending order. In this case, the associated operator function is `>`. | ||
case descending | ||
|
||
// MARK: - Properties | ||
|
||
public var `operator`: (Value, Value) -> Bool { | ||
switch self { | ||
case .ascending: | ||
return (<) | ||
case .descending: | ||
return (>) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
public extension URL { | ||
|
||
static var empty: URL { | ||
return URL(string: "https://apple.com")! | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
Sources/SwiftBoost/Foundation/Extensions/URLSessionExtension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Foundation | ||
|
||
public extension URLSession { | ||
|
||
enum AppError: Error { | ||
|
||
case invalidURL(String) | ||
case networkError(Error) | ||
case noResponse | ||
case decodingError(Error) | ||
|
||
public func errorMessage() -> String { | ||
switch self { | ||
case .invalidURL(let str): | ||
return "badURL: \(str)" | ||
case .networkError(let error): | ||
return "networkError: \(error)" | ||
case .noResponse: | ||
return "no network response" | ||
case .decodingError(let error): | ||
return "decoding error: \(error)" | ||
} | ||
} | ||
} | ||
|
||
enum HTTPMethod { | ||
|
||
case get | ||
|
||
var id: String { | ||
switch self { | ||
case .get: return "get" | ||
} | ||
} | ||
} | ||
|
||
static func request( | ||
url: String, | ||
method: HTTPMethod, | ||
completion: @escaping (AppError?, Data?, HTTPURLResponse?) ->Void) | ||
{ | ||
guard let url = URL(string: url) else { | ||
completion(AppError.invalidURL(url), nil, nil) | ||
return | ||
} | ||
|
||
var request = URLRequest(url: url) | ||
request.httpMethod = method.id | ||
URLSession.shared.dataTask(with: request) { (data, response, error) in | ||
guard let response = response as? HTTPURLResponse else { | ||
completion(AppError.noResponse, nil, nil) | ||
return | ||
} | ||
|
||
if let error = error { | ||
completion(AppError.networkError(error), nil, response) | ||
} else if let data = data { | ||
completion(nil, data, response) | ||
} | ||
}.resume() | ||
} | ||
} |