mirror of
https://github.com/zhigang1992/swift-request.git
synced 2026-01-12 09:34:11 +08:00
Allow String baseURL
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user