-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
258 additions
and
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
github "dreymonde/Shallows" ~> 0.3.1 | ||
github "dreymonde/Shallows" ~> 0.4.0 | ||
github "dreymonde/Avenues" | ||
github "dreymonde/Alba" |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
github "dreymonde/Alba" "0.4.0" | ||
github "dreymonde/Avenues" "0.1.4" | ||
github "dreymonde/Shallows" "0.3.3" | ||
github "dreymonde/Shallows" "0.4.1" |
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 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,73 @@ | ||
// | ||
// APIPath.swift | ||
// WebAPI | ||
// | ||
// Created by Олег on 08.08.17. | ||
// | ||
// | ||
|
||
import Foundation | ||
|
||
public struct APIPath : RawRepresentable, Hashable, ExpressibleByStringLiteral { | ||
|
||
public static let separator = "/" | ||
|
||
public typealias RawValue = String | ||
|
||
public var rawValue: String | ||
|
||
public var components: [String] { | ||
return rawValue.components(separatedBy: APIPath.separator) | ||
} | ||
|
||
public init(rawValue: String) { | ||
self.rawValue = rawValue | ||
} | ||
|
||
public init(components: [String]) { | ||
self.rawValue = components.joined(separator: APIPath.separator) | ||
} | ||
|
||
public var hashValue: Int { | ||
return rawValue.hashValue | ||
} | ||
|
||
public init(stringLiteral value: String) { | ||
self.rawValue = value | ||
} | ||
|
||
public init(unicodeScalarLiteral value: String) { | ||
self.rawValue = value | ||
} | ||
|
||
public init(extendedGraphemeClusterLiteral value: String) { | ||
self.rawValue = value | ||
} | ||
|
||
} | ||
|
||
extension APIPath : ExpressibleByArrayLiteral { | ||
|
||
public typealias Element = APIPath | ||
|
||
public init(arrayLiteral elements: Element...) { | ||
self.init(components: elements.map({ $0.rawValue })) | ||
} | ||
|
||
} | ||
|
||
extension APIPath { | ||
|
||
public static func + (lhs: APIPath, rhs: APIPath) -> APIPath { | ||
return APIPath(components: [lhs.rawValue, rhs.rawValue]) | ||
} | ||
|
||
} | ||
|
||
extension URL { | ||
|
||
public func appendingPath(_ path: APIPath) -> URL { | ||
return appendingPathComponent(path.rawValue) | ||
} | ||
|
||
} |
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 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 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 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,90 @@ | ||
import Foundation | ||
import Shallows | ||
|
||
public struct WebAPI : ReadableCacheProtocol { | ||
|
||
private let underlying: ReadOnlyCache<APIPath, Data> | ||
|
||
public init(networkProvider: ReadOnlyCache<URL, Data>, | ||
baseURL: URL) { | ||
self.underlying = networkProvider | ||
.mapKeys({ baseURL.appendingPath($0) }) | ||
} | ||
|
||
public func retrieve(forKey key: APIPath, completion: @escaping (Result<Data>) -> ()) { | ||
underlying.retrieve(forKey: key, completion: completion) | ||
} | ||
|
||
} | ||
|
||
extension WebAPI { | ||
|
||
public init(urlSession: URLSession, baseURL: URL) { | ||
let networkProvider = urlSession.asReadOnlyCache() | ||
.droppingResponse() | ||
.usingURLKeys() | ||
self.init(networkProvider: networkProvider, baseURL: baseURL) | ||
} | ||
|
||
} | ||
|
||
extension URLSession : ReadableCacheProtocol { | ||
|
||
public enum Key { | ||
case url(URL) | ||
case urlRequest(URLRequest) | ||
} | ||
|
||
public enum CacheError : Error { | ||
case taskError(Error) | ||
case responseIsNotHTTP(URLResponse?) | ||
case noData | ||
} | ||
|
||
public func retrieve(forKey request: Key, completion: @escaping (Result<(HTTPURLResponse, Data)>) -> ()) { | ||
let completion: (Data?, URLResponse?, Error?) -> () = { (data, response, error) in | ||
if let error = error { | ||
completion(.failure(CacheError.taskError(error))) | ||
return | ||
} | ||
guard let httpResponse = response as? HTTPURLResponse else { | ||
completion(.failure(CacheError.responseIsNotHTTP(response))) | ||
return | ||
} | ||
guard let data = data else { | ||
completion(.failure(CacheError.noData)) | ||
return | ||
} | ||
completion(.success(httpResponse, data)) | ||
} | ||
let task: URLSessionTask | ||
switch request { | ||
case .url(let url): | ||
task = self.dataTask(with: url, completionHandler: completion) | ||
case .urlRequest(let request): | ||
task = self.dataTask(with: request, completionHandler: completion) | ||
} | ||
task.resume() | ||
} | ||
|
||
} | ||
|
||
extension ReadOnlyCache where Key == URLSession.Key { | ||
|
||
public func usingURLKeys() -> ReadOnlyCache<URL, Value> { | ||
return mapKeys({ .url($0) }) | ||
} | ||
|
||
public func usingURLRequestKeys() -> ReadOnlyCache<URLRequest, Value> { | ||
return mapKeys({ .urlRequest($0) }) | ||
} | ||
|
||
} | ||
|
||
extension ReadOnlyCache where Value == (HTTPURLResponse, Data) { | ||
|
||
public func droppingResponse() -> ReadOnlyCache<Key, Data> { | ||
return mapValues({ $0.1 }) | ||
} | ||
|
||
} |
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
Oops, something went wrong.