mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-10 23:33:29 +01:00
Add Network service and default sessions
Signed-off-by: Joseph Mattello <mail@joemattiello.com>
This commit is contained in:
committed by
Spidy123222
parent
79d677cf3c
commit
d6ea725329
81
AltStore/Services/NetworkService.swift
Normal file
81
AltStore/Services/NetworkService.swift
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// NetworkService.swift
|
||||
// SideStore
|
||||
//
|
||||
// Created by Joseph Mattiello on 11/6/22.
|
||||
// Copyright © 2022 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public protocol Services {
|
||||
var network: any NetworkService { get }
|
||||
}
|
||||
|
||||
public protocol NetworkService {
|
||||
var session: URLSession { get }
|
||||
var sessionNoCache: URLSession { get }
|
||||
var backgroundSession: URLSession { get }
|
||||
}
|
||||
|
||||
public struct DefaultServices: Services {
|
||||
public var network: NetworkService = AltNetworkService()
|
||||
}
|
||||
|
||||
let AppServices = DefaultServices()
|
||||
|
||||
final public class AltNetworkDelegate: NSObject, URLSessionTaskDelegate {
|
||||
public struct Options: OptionSet {
|
||||
public let rawValue: Int
|
||||
public init(rawValue: Int) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let redirect = Options(rawValue: 1 << 0)
|
||||
public static let all: Options = [.redirect]
|
||||
}
|
||||
|
||||
var options: Options = .all
|
||||
|
||||
// func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
|
||||
// if options.contains(.redirect) {
|
||||
// completionHandler(request)
|
||||
// } else {
|
||||
// completionHandler(nil)
|
||||
// }
|
||||
// }
|
||||
|
||||
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> URLRequest? {
|
||||
if options.contains(.redirect) {
|
||||
return request
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final class AltNetworkService: NetworkService {
|
||||
public let session: URLSession = {
|
||||
let configuration: URLSessionConfiguration = URLSessionConfiguration.default
|
||||
configuration.httpShouldSetCookies = true
|
||||
configuration.httpShouldUsePipelining = true
|
||||
let session = URLSession.init(configuration: configuration, delegate: AltNetworkDelegate(), delegateQueue: nil)
|
||||
return session
|
||||
}()
|
||||
|
||||
public let sessionNoCache: URLSession = {
|
||||
let configuration: URLSessionConfiguration = URLSessionConfiguration.default
|
||||
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
||||
configuration.urlCache = nil
|
||||
let session = URLSession.init(configuration: configuration, delegate: AltNetworkDelegate(), delegateQueue: nil)
|
||||
return session
|
||||
}()
|
||||
|
||||
static let backgroundSessionIdentifier = "SideStoreBackgroundSession"
|
||||
|
||||
public let backgroundSession: URLSession = {
|
||||
let configuration: URLSessionConfiguration = URLSessionConfiguration.background(withIdentifier: AltNetworkService.backgroundSessionIdentifier)
|
||||
let session = URLSession.init(configuration: configuration, delegate: AltNetworkDelegate(), delegateQueue: nil)
|
||||
return session
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user