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

Commit

Permalink
Added printing system called GPS
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaesung Lee committed Jun 4, 2023
1 parent a3aa994 commit 66bfb1c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
27 changes: 27 additions & 0 deletions Sources/Satellite/Satellite.GPS.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Satellite.GPS.swift
//
//
// Created by Jaesung Lee on 2023/06/04.
//

import Foundation

extension Satellite {
/// Start Global Printing System
public func _startGPS() {
self._isGPSEnabled = true
}

/// Start Global Printing System
public func _endGPT() {
self._isGPSEnabled = false
}

public func showGPT(_ stringValue: String, function: StaticString = #function, file: StaticString = #fileID, line: UInt = #line) {
guard _isGPSEnabled else { return }
let log = ("\n🛰️ Satellite: \(host)\t\(Date())\n📄file: \(file)\tline: \(line)\tfunction: \(function)\n📡 \(stringValue)\n")
print(log)
_gpsLogs.append(log)
}
}
37 changes: 29 additions & 8 deletions Sources/Satellite/Satellite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class Satellite {
/// The host domain such as `apple.com` or `icloud.com`.
public let host: String

var _isGPSEnabled: Bool = false
var _gpsLogs: [String] = []

/// The base URL that is a combination of ``scheme`` and ``host``.
/// `https://apple.com`
public var baseURL: String {
Expand Down Expand Up @@ -70,14 +73,21 @@ public class Satellite {
httpBody: httpBody
)
let (data, response) = try await URLSession.shared.data(for: urlRequest)
showGPT(String(data: data, encoding: .utf8) ?? "Unknown data")
guard let httpResponse = response as? HTTPURLResponse else {
throw Satellite.Error.responseHasNoData
let error = Satellite.Error.responseHasNoData
showGPT(error.description)
throw error
}
guard (200..<300) ~= httpResponse.statusCode else {
throw Satellite.Error.statusCode(httpResponse.statusCode)
let error = Satellite.Error.statusCode(httpResponse.statusCode)
showGPT(error.description)
throw error
}
guard let output = try? JSONDecoder().decode(ResponseType.self, from: data) else {
throw Satellite.Error.responseIsFailedDecoding
let error = Satellite.Error.responseIsFailedDecoding
showGPT(error.description)
throw error
}
return output
}
Expand Down Expand Up @@ -106,12 +116,17 @@ public class Satellite {
)
let publisher = URLSession.shared
.dataTaskPublisher(for: urlRequest)
.tryMap { (data, response) in
.tryMap { [weak self] (data, response) in
self?.showGPT(String(data: data, encoding: .utf8) ?? "Unknown data")
guard let httpResponse = response as? HTTPURLResponse else {
throw Satellite.Error.requestIsFailed
let error = Satellite.Error.requestIsFailed
self?.showGPT(error.description)
throw error
}
guard (200..<300) ~= httpResponse.statusCode else {
throw Satellite.Error.statusCode(httpResponse.statusCode)
let error = Satellite.Error.statusCode(httpResponse.statusCode)
self?.showGPT(error.description)
throw error
}
return data
}
Expand All @@ -127,14 +142,19 @@ public class Satellite {
httpHeaders: [String: String]?,
httpBody: (any Encodable)?
) throws -> URLRequest {
showGPT("\(baseURL)/\(path)")
guard var components = URLComponents(string: "\(baseURL)/\(path)") else {
throw Satellite.Error.urlIsInvalid
let error = Satellite.Error.urlIsInvalid
showGPT(error.description)
throw error
}
if let queryItems {
components.queryItems = queryItems
}
guard let url = components.url else {
throw Satellite.Error.urlIsInvalid
let error = Satellite.Error.urlIsInvalid
showGPT(error.description)
throw error
}
var urlRequest = URLRequest(url: url, timeoutInterval: 5.0)
urlRequest.httpMethod = httpMethod.rawValue
Expand All @@ -146,6 +166,7 @@ public class Satellite {
if let httpBody {
urlRequest.httpBody = try JSONEncoder().encode(httpBody)
}
showGPT(urlRequest.description)
return urlRequest
}
}
16 changes: 15 additions & 1 deletion Tests/SatelliteTests/SatelliteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,22 @@ final class SatelliteTests: XCTestCase {
XCTAssertFalse($0.text.isEmpty)
}
}

func test_globalPrintingSystem() async throws {
let satellite = Satellite(host: "cat-fact.herokuapp.com")
satellite._startGPS()
let _: [CatFact] = try await satellite.response(
for: "facts/random",
httpMethod: .get,
queryItems: [
URLQueryItem(name: "animal_type", value: "cat"),
URLQueryItem(name: "amount", value: "2")
]
)
XCTAssertFalse(satellite._gpsLogs.isEmpty)
}
}

struct CatFact: Codable {
let text: String
}
}

0 comments on commit 66bfb1c

Please sign in to comment.