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