[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:
2024-03-17 22:48:27 +00:00
committed by javier
parent 4359f53a19
commit 7c016b50d6
18 changed files with 850 additions and 3 deletions
@@ -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
}()
}
@@ -6,7 +6,7 @@
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
//
import ReviewsFoundation
import ReviewsFoundationKit
import XCTest
final class String_ConstantsTests: XCTestCase {
@@ -15,7 +15,7 @@ final class String_ConstantsTests: XCTestCase {
private var sut: String!
// MARK: Constants tests
func testEmpty() throws {
func testEmpty() {
// GIVEN
sut = .empty
@@ -0,0 +1,27 @@
//
// URL+ConstantsTests.swift
// ReviewsFoundationTest
//
// Created by Javier Cicchelli on 17/03/2024.
// Copyright © 2024 Röck+Cöde VoF. All rights reserved.
//
import Foundation
import ReviewsFoundationKit
import XCTest
final class URL_ConstantsTests: XCTestCase {
// MARK: Properties
private var sut: URL!
// MARK: Constants tests
func testBitBucket() {
sut = .bitBucket
// WHEN
// THEN
XCTAssertEqual(sut.absoluteString, "file:///dev/null")
}
}