diff --git a/README.md b/README.md index c301c5b..870f5e1 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,7 @@ class QuoteService { To make a request using SwiftRequest, you can do the following: ```swift -let baseURL = URL(string: "https://api.quotable.io")! -let service = QuoteService(baseURL: baseURL) +let service = QuoteService(baseURL: "https://api.quotable.io") let (quotes, _) = try await service.getRandomQuotes(limit: 5) let (quote, _) = try await service.getQuote(by: "69Ldsxcdm-") ``` @@ -81,4 +80,4 @@ SwiftRequest provides several parameters that can be used in conjunction with th ## License -SwiftRequest is available under the MIT license. See the [LICENSE](LICENSE) for details. \ No newline at end of file +SwiftRequest is available under the MIT license. See the [LICENSE](LICENSE) for details. diff --git a/Sources/SwiftRequest/Service.swift b/Sources/SwiftRequest/Service.swift index 7a1747d..0f96c36 100644 --- a/Sources/SwiftRequest/Service.swift +++ b/Sources/SwiftRequest/Service.swift @@ -5,7 +5,15 @@ public protocol Service { } extension Service { - public init(baseURL: URL) { - self.init(baseURL: baseURL, session: .shared) + public init(baseURL: URL, session: URLSession = .shared) { + self.init(baseURL: baseURL, session: session) + } + + public init(baseURL: String, session: URLSession = .shared) { + guard let baseURL = URL(string: baseURL) else { + fatalError("Invalid baseURL") + } + + self.init(baseURL: baseURL, session: session) } } diff --git a/Sources/SwiftRequestClient/main.swift b/Sources/SwiftRequestClient/main.swift index 953a245..0c4b808 100644 --- a/Sources/SwiftRequestClient/main.swift +++ b/Sources/SwiftRequestClient/main.swift @@ -1,7 +1,6 @@ import Foundation -let baseURL = URL(string: "https://api.quotable.io")! -let service = QuoteService(baseURL: baseURL) +let service = QuoteService(baseURL: "https://api.quotable.io") do { let (quotes, _) = try await service.getRandomQuotes(limit: 3)