Skip to content
This repository was archived by the owner on May 2, 2024. It is now read-only.

Commit 66bfb1c

Browse files
author
Jaesung Lee
committed
Added printing system called GPS
1 parent a3aa994 commit 66bfb1c

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Satellite.GPS.swift
3+
//
4+
//
5+
// Created by Jaesung Lee on 2023/06/04.
6+
//
7+
8+
import Foundation
9+
10+
extension Satellite {
11+
/// Start Global Printing System
12+
public func _startGPS() {
13+
self._isGPSEnabled = true
14+
}
15+
16+
/// Start Global Printing System
17+
public func _endGPT() {
18+
self._isGPSEnabled = false
19+
}
20+
21+
public func showGPT(_ stringValue: String, function: StaticString = #function, file: StaticString = #fileID, line: UInt = #line) {
22+
guard _isGPSEnabled else { return }
23+
let log = ("\n🛰️ Satellite: \(host)\t\(Date())\n📄file: \(file)\tline: \(line)\tfunction: \(function)\n📡 \(stringValue)\n")
24+
print(log)
25+
_gpsLogs.append(log)
26+
}
27+
}

Sources/Satellite/Satellite.swift

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class Satellite {
3232
/// The host domain such as `apple.com` or `icloud.com`.
3333
public let host: String
3434

35+
var _isGPSEnabled: Bool = false
36+
var _gpsLogs: [String] = []
37+
3538
/// The base URL that is a combination of ``scheme`` and ``host``.
3639
/// `https://apple.com`
3740
public var baseURL: String {
@@ -70,14 +73,21 @@ public class Satellite {
7073
httpBody: httpBody
7174
)
7275
let (data, response) = try await URLSession.shared.data(for: urlRequest)
76+
showGPT(String(data: data, encoding: .utf8) ?? "Unknown data")
7377
guard let httpResponse = response as? HTTPURLResponse else {
74-
throw Satellite.Error.responseHasNoData
78+
let error = Satellite.Error.responseHasNoData
79+
showGPT(error.description)
80+
throw error
7581
}
7682
guard (200..<300) ~= httpResponse.statusCode else {
77-
throw Satellite.Error.statusCode(httpResponse.statusCode)
83+
let error = Satellite.Error.statusCode(httpResponse.statusCode)
84+
showGPT(error.description)
85+
throw error
7886
}
7987
guard let output = try? JSONDecoder().decode(ResponseType.self, from: data) else {
80-
throw Satellite.Error.responseIsFailedDecoding
88+
let error = Satellite.Error.responseIsFailedDecoding
89+
showGPT(error.description)
90+
throw error
8191
}
8292
return output
8393
}
@@ -106,12 +116,17 @@ public class Satellite {
106116
)
107117
let publisher = URLSession.shared
108118
.dataTaskPublisher(for: urlRequest)
109-
.tryMap { (data, response) in
119+
.tryMap { [weak self] (data, response) in
120+
self?.showGPT(String(data: data, encoding: .utf8) ?? "Unknown data")
110121
guard let httpResponse = response as? HTTPURLResponse else {
111-
throw Satellite.Error.requestIsFailed
122+
let error = Satellite.Error.requestIsFailed
123+
self?.showGPT(error.description)
124+
throw error
112125
}
113126
guard (200..<300) ~= httpResponse.statusCode else {
114-
throw Satellite.Error.statusCode(httpResponse.statusCode)
127+
let error = Satellite.Error.statusCode(httpResponse.statusCode)
128+
self?.showGPT(error.description)
129+
throw error
115130
}
116131
return data
117132
}
@@ -127,14 +142,19 @@ public class Satellite {
127142
httpHeaders: [String: String]?,
128143
httpBody: (any Encodable)?
129144
) throws -> URLRequest {
145+
showGPT("\(baseURL)/\(path)")
130146
guard var components = URLComponents(string: "\(baseURL)/\(path)") else {
131-
throw Satellite.Error.urlIsInvalid
147+
let error = Satellite.Error.urlIsInvalid
148+
showGPT(error.description)
149+
throw error
132150
}
133151
if let queryItems {
134152
components.queryItems = queryItems
135153
}
136154
guard let url = components.url else {
137-
throw Satellite.Error.urlIsInvalid
155+
let error = Satellite.Error.urlIsInvalid
156+
showGPT(error.description)
157+
throw error
138158
}
139159
var urlRequest = URLRequest(url: url, timeoutInterval: 5.0)
140160
urlRequest.httpMethod = httpMethod.rawValue
@@ -146,6 +166,7 @@ public class Satellite {
146166
if let httpBody {
147167
urlRequest.httpBody = try JSONEncoder().encode(httpBody)
148168
}
169+
showGPT(urlRequest.description)
149170
return urlRequest
150171
}
151172
}

Tests/SatelliteTests/SatelliteTests.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,22 @@ final class SatelliteTests: XCTestCase {
2828
XCTAssertFalse($0.text.isEmpty)
2929
}
3030
}
31+
32+
func test_globalPrintingSystem() async throws {
33+
let satellite = Satellite(host: "cat-fact.herokuapp.com")
34+
satellite._startGPS()
35+
let _: [CatFact] = try await satellite.response(
36+
for: "facts/random",
37+
httpMethod: .get,
38+
queryItems: [
39+
URLQueryItem(name: "animal_type", value: "cat"),
40+
URLQueryItem(name: "amount", value: "2")
41+
]
42+
)
43+
XCTAssertFalse(satellite._gpsLogs.isEmpty)
44+
}
3145
}
3246

3347
struct CatFact: Codable {
3448
let text: String
35-
}
49+
}

0 commit comments

Comments
 (0)