[Library] iTunes library (#5)
This PR contains the work done to implement the `iTunesService` service that fetches the reviews from the **Apple App Store**. Reviewed-on: rock-n-code/app-reviews#5 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #5.
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// MockURLProtocol.swift
|
||||
// ReviewsFoundationKit
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public class MockURLProtocol: URLProtocol {
|
||||
|
||||
// MARK: Properties
|
||||
public static var response: Response?
|
||||
|
||||
// MARK: Functions
|
||||
public override class func canInit(with task: URLSessionTask) -> Bool {
|
||||
true
|
||||
}
|
||||
|
||||
public override class func canInit(with request: URLRequest) -> Bool {
|
||||
true
|
||||
}
|
||||
|
||||
public override class func canonicalRequest(for request: URLRequest) -> URLRequest {
|
||||
request
|
||||
}
|
||||
|
||||
public override func startLoading() {
|
||||
guard
|
||||
let url = request.url,
|
||||
let response = Self.response
|
||||
else {
|
||||
client?.urlProtocolDidFinishLoading(self)
|
||||
return
|
||||
}
|
||||
|
||||
if let data = try? response.data {
|
||||
client?.urlProtocol(self, didLoad: data)
|
||||
}
|
||||
|
||||
if let httpResponse = HTTPURLResponse(
|
||||
url: url,
|
||||
statusCode: response.statusCode,
|
||||
httpVersion: nil,
|
||||
headerFields: nil
|
||||
) {
|
||||
client?.urlProtocol(
|
||||
self,
|
||||
didReceive: httpResponse,
|
||||
cacheStoragePolicy: .allowedInMemoryOnly
|
||||
)
|
||||
}
|
||||
|
||||
client?.urlProtocolDidFinishLoading(self)
|
||||
}
|
||||
|
||||
public override func stopLoading() {}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Structs
|
||||
|
||||
extension MockURLProtocol {
|
||||
public struct Response {
|
||||
|
||||
// MARK: Constants
|
||||
public let statusCode: Int
|
||||
public let object: (any Encodable)?
|
||||
|
||||
// MARK: Initialisers
|
||||
public init(
|
||||
statusCode: Int,
|
||||
object: (any Codable)? = nil
|
||||
) {
|
||||
self.statusCode = statusCode
|
||||
self.object = object
|
||||
}
|
||||
|
||||
// MARK: Computed
|
||||
var data: Data? {
|
||||
get throws {
|
||||
guard let object else { return nil }
|
||||
|
||||
return try JSONEncoder.default.encode(object)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// JSONDecoder+Constants.swift
|
||||
// ReviewsiTunesKit
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension JSONDecoder {
|
||||
|
||||
// MARK: Constants
|
||||
public static let `default` = {
|
||||
let decoder = JSONDecoder()
|
||||
|
||||
decoder.dateDecodingStrategy = .iso8601
|
||||
|
||||
return decoder
|
||||
}()
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// JSONEncoder+Constants.swift
|
||||
// ReviewsiTunesKit
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension JSONEncoder {
|
||||
|
||||
// MARK: Constants
|
||||
public static let `default` = {
|
||||
let encoder = JSONEncoder()
|
||||
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
|
||||
return encoder
|
||||
}()
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// URL+Constants.swift
|
||||
// ReviewsFoundationKit
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension URL {
|
||||
|
||||
// MARK: Constants
|
||||
public static let bitBucket: URL = {
|
||||
if #available(iOS 16.0, *) {
|
||||
.init(filePath: .FilePath.bitBucket)
|
||||
} else {
|
||||
.init(fileURLWithPath: .FilePath.bitBucket)
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
private extension String {
|
||||
enum FilePath {
|
||||
static let bitBucket = "/dev/null"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// URLSessionConfiguration+Constants.swift
|
||||
// ReviewsFoundationKit
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/03/2024.
|
||||
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension URLSessionConfiguration {
|
||||
|
||||
// MARK: Constants
|
||||
public static let mock = {
|
||||
let configuration: URLSessionConfiguration = .ephemeral
|
||||
|
||||
configuration.protocolClasses = [MockURLProtocol.self]
|
||||
|
||||
return configuration
|
||||
}()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user