mirror of
https://github.com/zhigang1992/swift-request.git
synced 2026-04-30 18:22:49 +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:
|
To make a request using SwiftRequest, you can do the following:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let baseURL = URL(string: "https://api.quotable.io")!
|
let service = QuoteService(baseURL: "https://api.quotable.io")
|
||||||
let service = QuoteService(baseURL: baseURL)
|
|
||||||
let (quotes, _) = try await service.getRandomQuotes(limit: 5)
|
let (quotes, _) = try await service.getRandomQuotes(limit: 5)
|
||||||
let (quote, _) = try await service.getQuote(by: "69Ldsxcdm-")
|
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
|
## 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 {
|
extension Service {
|
||||||
public init(baseURL: URL) {
|
public init(baseURL: URL, session: URLSession = .shared) {
|
||||||
self.init(baseURL: baseURL, session: .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
|
import Foundation
|
||||||
|
|
||||||
let baseURL = URL(string: "https://api.quotable.io")!
|
let service = QuoteService(baseURL: "https://api.quotable.io")
|
||||||
let service = QuoteService(baseURL: baseURL)
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let (quotes, _) = try await service.getRandomQuotes(limit: 3)
|
let (quotes, _) = try await service.getRandomQuotes(limit: 3)
|
||||||
|
|||||||
Reference in New Issue
Block a user