Rename networking code to CatFactsKit
This commit is contained in:
parent
233b3e9b4f
commit
e716c1cf03
@ -30,7 +30,7 @@
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<TestPlans>
|
||||
<TestPlanReference
|
||||
reference = "container:../Meow.xctestplan"
|
||||
reference = "container:Meow.xctestplan"
|
||||
default = "YES">
|
||||
</TestPlanReference>
|
||||
</TestPlans>
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
"testTargets" : [
|
||||
{
|
||||
"target" : {
|
||||
"containerPath" : "container:Meow",
|
||||
"containerPath" : "container:",
|
||||
"identifier" : "MeowTests",
|
||||
"name" : "MeowTests"
|
||||
}
|
||||
|
||||
@ -17,8 +17,8 @@ let package = Package(
|
||||
products: [
|
||||
// Products define the executables and libraries a package produces, making them visible to other packages.
|
||||
.library(
|
||||
name: "Meow",
|
||||
targets: ["Meow"]
|
||||
name: "CatFactsKit",
|
||||
targets: ["CatFactsKit"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
@ -28,13 +28,13 @@ let package = Package(
|
||||
// Targets are the basic building blocks of a package, defining a module or a test suite.
|
||||
// Targets can depend on other targets in this package and products from dependencies.
|
||||
.target(
|
||||
name: "Meow",
|
||||
name: "CatFactsKit",
|
||||
swiftSettings: commonSwiftSettings
|
||||
),
|
||||
.testTarget(
|
||||
name: "MeowTests",
|
||||
name: "CatFactsKitTests",
|
||||
dependencies: [
|
||||
"Meow",
|
||||
"CatFactsKit",
|
||||
"Mocker"
|
||||
],
|
||||
resources: [
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
//
|
||||
// Meow.swift
|
||||
// Meow
|
||||
// CatFacts.swift
|
||||
// CatFactsKit
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct Meow {
|
||||
public struct CatFacts {
|
||||
let baseURL: URL
|
||||
let urlSession: URLSession
|
||||
|
||||
@ -25,7 +25,7 @@ public struct Meow {
|
||||
|
||||
private func generateRequest(path: String, params: [String: CustomStringConvertible] = [:]) throws -> URLRequest {
|
||||
guard let url = URL(string: path, relativeTo: baseURL) else {
|
||||
throw MeowError.requestError("Couldn't generate URL from path: \(path), baseURL: \(baseURL)")
|
||||
throw CatFactsError.requestError("Couldn't generate URL from path: \(path), baseURL: \(baseURL)")
|
||||
}
|
||||
|
||||
let queryItems = params
|
||||
@ -39,11 +39,11 @@ public struct Meow {
|
||||
private func decodeRequest<T: Decodable>(request: URLRequest) async throws -> T {
|
||||
let (data, response) = try await urlSession.data(for: request)
|
||||
guard let response = response as? HTTPURLResponse else {
|
||||
throw MeowError.connectionError("Couldn't get HTTP response from request \(request)")
|
||||
throw CatFactsError.connectionError("Couldn't get HTTP response from request \(request)")
|
||||
}
|
||||
guard response.statusCode < 300 else {
|
||||
let body = String(data: data, encoding: .utf8)
|
||||
throw MeowError.responseError(response.statusCode, body ?? "unknown error")
|
||||
throw CatFactsError.responseError(response.statusCode, body ?? "unknown error")
|
||||
}
|
||||
return try JSONDecoder().decode(T.self, from: data)
|
||||
}
|
||||
@ -1,11 +1,11 @@
|
||||
//
|
||||
// MeowError.swift
|
||||
// Meow
|
||||
// CatFactsError.swift
|
||||
// CatFactsKit
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public enum MeowError: LocalizedError {
|
||||
public enum CatFactsError: LocalizedError {
|
||||
case requestError(String)
|
||||
case connectionError(String)
|
||||
case responseError(Int, String)
|
||||
@ -1,6 +1,6 @@
|
||||
//
|
||||
// FactsResponse.swift
|
||||
// Meow
|
||||
// CatFactsKit
|
||||
//
|
||||
|
||||
import Foundation
|
||||
@ -6,10 +6,10 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
import Mocker
|
||||
@testable import Meow
|
||||
@testable import CatFactsKit
|
||||
|
||||
@Suite("Meow Tests")
|
||||
struct MeowTests {
|
||||
@Suite("CatFacts Tests")
|
||||
struct CatFactsTests {
|
||||
private let baseURL = URL(string: "https://meow.meow")!
|
||||
let urlSession: URLSession = {
|
||||
let configuration = URLSessionConfiguration.default
|
||||
@ -30,12 +30,13 @@ struct MeowTests {
|
||||
mockPath("/", queryItems: ["count": count], data: [.get: Fixtures.facts])
|
||||
let facts = try await instance.getFacts(count: count)
|
||||
#expect(facts.count == count)
|
||||
#expect(facts[3] == "The average cat food meal is the equivalent to about five mice.")
|
||||
}
|
||||
|
||||
// MARK: - Private functionality
|
||||
|
||||
private func vendInstance() -> Meow {
|
||||
return Meow(baseURL: baseURL, urlSession: urlSession)
|
||||
private func vendInstance() -> CatFacts {
|
||||
return CatFacts(baseURL: baseURL, urlSession: urlSession)
|
||||
}
|
||||
|
||||
private func mockPath(_ path: String, headers: [String: String] = [:], queryItems: [String: CustomStringConvertible] = [:], data: [Mock.HTTPMethod: Data] = [:]) {
|
||||
Loading…
x
Reference in New Issue
Block a user