Allow String baseURL

This commit is contained in:
Ailton Vieira
2023-06-11 22:40:03 -03:00
parent fb1d860f04
commit 57057c7dfa
3 changed files with 13 additions and 7 deletions

View File

@@ -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.
SwiftRequest is available under the MIT license. See the [LICENSE](LICENSE) for details.

View File

@@ -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)
}
}

View File

@@ -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)