Skip to content

Instantly share code, notes, and snippets.

View soujohnreis's full-sized avatar
:octocat:
Coding like there's not tomorrow

John Lenon Reis soujohnreis

:octocat:
Coding like there's not tomorrow
View GitHub Profile
@soujohnreis
soujohnreis / URLSession+JsonDecodableTask.swift
Last active September 24, 2021 13:14
URLSession Decodable
enum URLError: Error {
case noData, decodingError
}
extension URLSession {
/// A type safe URL loader that either completes with success value or error with Error
func jsonDecodableTask<T: Decodable>(with url: URLRequest, decoder: JSONDecoder = JSONDecoder(), completion: @escaping (Result<T, Error>) -> Void) -> URLSessionDataTask {
self.dataTask(with: url) { (data, response, error) in
DispatchQueue.main.async {
// MARK: PublicKeyPinner
import CommonCrypto
import CryptoKit
final class PublicKeyPinner {
/// Stored public key hashes
private let hashes: [String]
public init(hashes: [String]) {
self.hashes = hashes
let fromDate = Date()
let toDate = Date()
let calendar = Calendar.current
let components = DateComponents(hour:0 , minute: 0, second: 0)
guard let startDate = calendar.date(byAdding: .day, value: -1, to: from) else {
return
}
calendar.enumerateDates(startingAfter: startDate, matching: components, matchingPolicy: .nextTime) { date, strict, stop in
protocol ViewCodeProtocol {
func buildViewHierarchy()
func buildConstraints()
func buildAdditionalConfiguration()
func build()
}
extension ViewCodeProtocol {
func build() {
self.buildViewHierarchy()
@soujohnreis
soujohnreis / URLProtocolMock.swift
Last active September 4, 2023 10:22
Mock URLSession using URLProtocol
class URLProtocolMock: URLProtocol {
/// Dictionary maps URLs to tuples of error, data, and response
static var mockURLs = [URL?: (error: Error?, data: Data?, response: HTTPURLResponse?)]()
override class func canInit(with request: URLRequest) -> Bool {
// Handle all types of requests
return true
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {