mirror of
https://github.com/mas-cli/mas
synced 2024-11-23 03:53:09 +00:00
37 lines
1,021 B
Swift
37 lines
1,021 B
Swift
//
|
|
// NetworkManager.swift
|
|
// MasKit
|
|
//
|
|
// Created by Ben Chatelain on 1/5/19.
|
|
// Copyright © 2019 mas-cli. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import PromiseKit
|
|
|
|
/// Network abstraction
|
|
class NetworkManager {
|
|
private let session: NetworkSession
|
|
|
|
/// Designated initializer
|
|
///
|
|
/// - Parameter session: A networking session.
|
|
init(session: NetworkSession = URLSession(configuration: .ephemeral)) {
|
|
self.session = session
|
|
|
|
// Older releases allowed URLSession to write a cache. We clean it up here.
|
|
do {
|
|
let url = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Library/Caches/com.mphys.mas-cli")
|
|
try FileManager.default.removeItem(at: url)
|
|
} catch {}
|
|
}
|
|
|
|
/// Loads data asynchronously.
|
|
///
|
|
/// - Parameters:
|
|
/// - url: URL to load data from.
|
|
/// - Returns: A Promise for the Data of the response.
|
|
func loadData(from url: URL) -> Promise<Data> {
|
|
session.loadData(from: url)
|
|
}
|
|
}
|